auth_x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/module.h>
  4. #include <linux/random.h>
  5. #include "auth_x.h"
  6. #include "auth_x_protocol.h"
  7. #include "crypto.h"
  8. #include "auth.h"
  9. #include "decode.h"
  10. struct kmem_cache *ceph_x_ticketbuf_cachep;
  11. #define TEMP_TICKET_BUF_LEN 256
  12. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
  13. static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
  14. {
  15. struct ceph_x_info *xi = ac->private;
  16. int need;
  17. ceph_x_validate_tickets(ac, &need);
  18. dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
  19. ac->want_keys, need, xi->have_keys);
  20. return (ac->want_keys & xi->have_keys) == ac->want_keys;
  21. }
  22. static int ceph_x_encrypt_buflen(int ilen)
  23. {
  24. return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
  25. sizeof(u32);
  26. }
  27. static int ceph_x_encrypt(struct ceph_crypto_key *secret,
  28. void *ibuf, int ilen, void *obuf, size_t olen)
  29. {
  30. struct ceph_x_encrypt_header head = {
  31. .struct_v = 1,
  32. .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
  33. };
  34. size_t len = olen - sizeof(u32);
  35. int ret;
  36. ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
  37. &head, sizeof(head), ibuf, ilen);
  38. if (ret)
  39. return ret;
  40. ceph_encode_32(&obuf, len);
  41. return len + sizeof(u32);
  42. }
  43. static int ceph_x_decrypt(struct ceph_crypto_key *secret,
  44. void **p, void *end, void *obuf, size_t olen)
  45. {
  46. struct ceph_x_encrypt_header head;
  47. size_t head_len = sizeof(head);
  48. int len, ret;
  49. len = ceph_decode_32(p);
  50. if (*p + len > end)
  51. return -EINVAL;
  52. dout("ceph_x_decrypt len %d\n", len);
  53. ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
  54. *p, len);
  55. if (ret)
  56. return ret;
  57. if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
  58. return -EPERM;
  59. *p += len;
  60. return olen;
  61. }
  62. /*
  63. * get existing (or insert new) ticket handler
  64. */
  65. struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
  66. int service)
  67. {
  68. struct ceph_x_ticket_handler *th;
  69. struct ceph_x_info *xi = ac->private;
  70. struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
  71. while (*p) {
  72. parent = *p;
  73. th = rb_entry(parent, struct ceph_x_ticket_handler, node);
  74. if (service < th->service)
  75. p = &(*p)->rb_left;
  76. else if (service > th->service)
  77. p = &(*p)->rb_right;
  78. else
  79. return th;
  80. }
  81. /* add it */
  82. th = kzalloc(sizeof(*th), GFP_NOFS);
  83. if (!th)
  84. return ERR_PTR(-ENOMEM);
  85. th->service = service;
  86. rb_link_node(&th->node, parent, p);
  87. rb_insert_color(&th->node, &xi->ticket_handlers);
  88. return th;
  89. }
  90. static void remove_ticket_handler(struct ceph_auth_client *ac,
  91. struct ceph_x_ticket_handler *th)
  92. {
  93. struct ceph_x_info *xi = ac->private;
  94. dout("remove_ticket_handler %p %d\n", th, th->service);
  95. rb_erase(&th->node, &xi->ticket_handlers);
  96. ceph_crypto_key_destroy(&th->session_key);
  97. if (th->ticket_blob)
  98. ceph_buffer_put(th->ticket_blob);
  99. kfree(th);
  100. }
  101. static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
  102. struct ceph_crypto_key *secret,
  103. void *buf, void *end)
  104. {
  105. struct ceph_x_info *xi = ac->private;
  106. int num;
  107. void *p = buf;
  108. int ret;
  109. char *dbuf;
  110. char *ticket_buf;
  111. u8 struct_v;
  112. dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
  113. if (!dbuf)
  114. return -ENOMEM;
  115. ret = -ENOMEM;
  116. ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
  117. GFP_NOFS | GFP_ATOMIC);
  118. if (!ticket_buf)
  119. goto out_dbuf;
  120. ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
  121. struct_v = ceph_decode_8(&p);
  122. if (struct_v != 1)
  123. goto bad;
  124. num = ceph_decode_32(&p);
  125. dout("%d tickets\n", num);
  126. while (num--) {
  127. int type;
  128. u8 struct_v;
  129. struct ceph_x_ticket_handler *th;
  130. void *dp, *dend;
  131. int dlen;
  132. char is_enc;
  133. struct timespec validity;
  134. struct ceph_crypto_key old_key;
  135. void *tp, *tpend;
  136. ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
  137. type = ceph_decode_32(&p);
  138. dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
  139. struct_v = ceph_decode_8(&p);
  140. if (struct_v != 1)
  141. goto bad;
  142. th = get_ticket_handler(ac, type);
  143. if (IS_ERR(th)) {
  144. ret = PTR_ERR(th);
  145. goto out;
  146. }
  147. /* blob for me */
  148. dlen = ceph_x_decrypt(secret, &p, end, dbuf,
  149. TEMP_TICKET_BUF_LEN);
  150. if (dlen <= 0) {
  151. ret = dlen;
  152. goto out;
  153. }
  154. dout(" decrypted %d bytes\n", dlen);
  155. dend = dbuf + dlen;
  156. dp = dbuf;
  157. struct_v = ceph_decode_8(&dp);
  158. if (struct_v != 1)
  159. goto bad;
  160. memcpy(&old_key, &th->session_key, sizeof(old_key));
  161. ret = ceph_crypto_key_decode(&th->session_key, &dp, dend);
  162. if (ret)
  163. goto out;
  164. ceph_decode_copy(&dp, &th->validity, sizeof(th->validity));
  165. ceph_decode_timespec(&validity, &th->validity);
  166. th->expires = get_seconds() + validity.tv_sec;
  167. th->renew_after = th->expires - (validity.tv_sec / 4);
  168. dout(" expires=%lu renew_after=%lu\n", th->expires,
  169. th->renew_after);
  170. /* ticket blob for service */
  171. ceph_decode_8_safe(&p, end, is_enc, bad);
  172. tp = ticket_buf;
  173. if (is_enc) {
  174. /* encrypted */
  175. dout(" encrypted ticket\n");
  176. dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
  177. TEMP_TICKET_BUF_LEN);
  178. if (dlen < 0) {
  179. ret = dlen;
  180. goto out;
  181. }
  182. dlen = ceph_decode_32(&tp);
  183. } else {
  184. /* unencrypted */
  185. ceph_decode_32_safe(&p, end, dlen, bad);
  186. ceph_decode_need(&p, end, dlen, bad);
  187. ceph_decode_copy(&p, ticket_buf, dlen);
  188. }
  189. tpend = tp + dlen;
  190. dout(" ticket blob is %d bytes\n", dlen);
  191. ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
  192. struct_v = ceph_decode_8(&tp);
  193. th->secret_id = ceph_decode_64(&tp);
  194. ret = ceph_decode_buffer(&th->ticket_blob, &tp, tpend);
  195. if (ret)
  196. goto out;
  197. dout(" got ticket service %d (%s) secret_id %lld len %d\n",
  198. type, ceph_entity_type_name(type), th->secret_id,
  199. (int)th->ticket_blob->vec.iov_len);
  200. xi->have_keys |= th->service;
  201. }
  202. ret = 0;
  203. out:
  204. kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
  205. out_dbuf:
  206. kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
  207. return ret;
  208. bad:
  209. ret = -EINVAL;
  210. goto out;
  211. }
  212. static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
  213. struct ceph_x_ticket_handler *th,
  214. struct ceph_x_authorizer *au)
  215. {
  216. int maxlen;
  217. struct ceph_x_authorize_a *msg_a;
  218. struct ceph_x_authorize_b msg_b;
  219. void *p, *end;
  220. int ret;
  221. int ticket_blob_len =
  222. (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
  223. dout("build_authorizer for %s %p\n",
  224. ceph_entity_type_name(th->service), au);
  225. maxlen = sizeof(*msg_a) + sizeof(msg_b) +
  226. ceph_x_encrypt_buflen(ticket_blob_len);
  227. dout(" need len %d\n", maxlen);
  228. if (au->buf && au->buf->alloc_len < maxlen) {
  229. ceph_buffer_put(au->buf);
  230. au->buf = NULL;
  231. }
  232. if (!au->buf) {
  233. au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
  234. if (!au->buf)
  235. return -ENOMEM;
  236. }
  237. au->service = th->service;
  238. msg_a = au->buf->vec.iov_base;
  239. msg_a->struct_v = 1;
  240. msg_a->global_id = cpu_to_le64(ac->global_id);
  241. msg_a->service_id = cpu_to_le32(th->service);
  242. msg_a->ticket_blob.struct_v = 1;
  243. msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
  244. msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
  245. if (ticket_blob_len) {
  246. memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
  247. th->ticket_blob->vec.iov_len);
  248. }
  249. dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
  250. le64_to_cpu(msg_a->ticket_blob.secret_id));
  251. p = msg_a + 1;
  252. p += ticket_blob_len;
  253. end = au->buf->vec.iov_base + au->buf->vec.iov_len;
  254. get_random_bytes(&au->nonce, sizeof(au->nonce));
  255. msg_b.struct_v = 1;
  256. msg_b.nonce = cpu_to_le64(au->nonce);
  257. ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
  258. p, end - p);
  259. if (ret < 0)
  260. goto out_buf;
  261. p += ret;
  262. au->buf->vec.iov_len = p - au->buf->vec.iov_base;
  263. dout(" built authorizer nonce %llx len %d\n", au->nonce,
  264. (int)au->buf->vec.iov_len);
  265. BUG_ON(au->buf->vec.iov_len > maxlen);
  266. return 0;
  267. out_buf:
  268. ceph_buffer_put(au->buf);
  269. au->buf = NULL;
  270. return ret;
  271. }
  272. static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
  273. void **p, void *end)
  274. {
  275. ceph_decode_need(p, end, 1 + sizeof(u64), bad);
  276. ceph_encode_8(p, 1);
  277. ceph_encode_64(p, th->secret_id);
  278. if (th->ticket_blob) {
  279. const char *buf = th->ticket_blob->vec.iov_base;
  280. u32 len = th->ticket_blob->vec.iov_len;
  281. ceph_encode_32_safe(p, end, len, bad);
  282. ceph_encode_copy_safe(p, end, buf, len, bad);
  283. } else {
  284. ceph_encode_32_safe(p, end, 0, bad);
  285. }
  286. return 0;
  287. bad:
  288. return -ERANGE;
  289. }
  290. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
  291. {
  292. int want = ac->want_keys;
  293. struct ceph_x_info *xi = ac->private;
  294. int service;
  295. *pneed = ac->want_keys & ~(xi->have_keys);
  296. for (service = 1; service <= want; service <<= 1) {
  297. struct ceph_x_ticket_handler *th;
  298. if (!(ac->want_keys & service))
  299. continue;
  300. if (*pneed & service)
  301. continue;
  302. th = get_ticket_handler(ac, service);
  303. if (!th) {
  304. *pneed |= service;
  305. continue;
  306. }
  307. if (get_seconds() >= th->renew_after)
  308. *pneed |= service;
  309. if (get_seconds() >= th->expires)
  310. xi->have_keys &= ~service;
  311. }
  312. }
  313. static int ceph_x_build_request(struct ceph_auth_client *ac,
  314. void *buf, void *end)
  315. {
  316. struct ceph_x_info *xi = ac->private;
  317. int need;
  318. struct ceph_x_request_header *head = buf;
  319. int ret;
  320. struct ceph_x_ticket_handler *th =
  321. get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  322. ceph_x_validate_tickets(ac, &need);
  323. dout("build_request want %x have %x need %x\n",
  324. ac->want_keys, xi->have_keys, need);
  325. if (need & CEPH_ENTITY_TYPE_AUTH) {
  326. struct ceph_x_authenticate *auth = (void *)(head + 1);
  327. void *p = auth + 1;
  328. struct ceph_x_challenge_blob tmp;
  329. char tmp_enc[40];
  330. u64 *u;
  331. if (p > end)
  332. return -ERANGE;
  333. dout(" get_auth_session_key\n");
  334. head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
  335. /* encrypt and hash */
  336. get_random_bytes(&auth->client_challenge, sizeof(u64));
  337. tmp.client_challenge = auth->client_challenge;
  338. tmp.server_challenge = cpu_to_le64(xi->server_challenge);
  339. ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
  340. tmp_enc, sizeof(tmp_enc));
  341. if (ret < 0)
  342. return ret;
  343. auth->struct_v = 1;
  344. auth->key = 0;
  345. for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
  346. auth->key ^= *u;
  347. dout(" server_challenge %llx client_challenge %llx key %llx\n",
  348. xi->server_challenge, le64_to_cpu(auth->client_challenge),
  349. le64_to_cpu(auth->key));
  350. /* now encode the old ticket if exists */
  351. ret = ceph_x_encode_ticket(th, &p, end);
  352. if (ret < 0)
  353. return ret;
  354. return p - buf;
  355. }
  356. if (need) {
  357. void *p = head + 1;
  358. struct ceph_x_service_ticket_request *req;
  359. if (p > end)
  360. return -ERANGE;
  361. head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
  362. BUG_ON(!th);
  363. ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
  364. if (ret)
  365. return ret;
  366. ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
  367. xi->auth_authorizer.buf->vec.iov_len);
  368. req = p;
  369. req->keys = cpu_to_le32(need);
  370. p += sizeof(*req);
  371. return p - buf;
  372. }
  373. return 0;
  374. }
  375. static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
  376. void *buf, void *end)
  377. {
  378. struct ceph_x_info *xi = ac->private;
  379. struct ceph_x_reply_header *head = buf;
  380. struct ceph_x_ticket_handler *th;
  381. int len = end - buf;
  382. int op;
  383. int ret;
  384. if (result)
  385. return result; /* XXX hmm? */
  386. if (xi->starting) {
  387. /* it's a hello */
  388. struct ceph_x_server_challenge *sc = buf;
  389. if (len != sizeof(*sc))
  390. return -EINVAL;
  391. xi->server_challenge = le64_to_cpu(sc->server_challenge);
  392. dout("handle_reply got server challenge %llx\n",
  393. xi->server_challenge);
  394. xi->starting = false;
  395. xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
  396. return -EAGAIN;
  397. }
  398. op = le32_to_cpu(head->op);
  399. result = le32_to_cpu(head->result);
  400. dout("handle_reply op %d result %d\n", op, result);
  401. switch (op) {
  402. case CEPHX_GET_AUTH_SESSION_KEY:
  403. /* verify auth key */
  404. ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
  405. buf + sizeof(*head), end);
  406. break;
  407. case CEPHX_GET_PRINCIPAL_SESSION_KEY:
  408. th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  409. BUG_ON(!th);
  410. ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
  411. buf + sizeof(*head), end);
  412. break;
  413. default:
  414. return -EINVAL;
  415. }
  416. if (ret)
  417. return ret;
  418. if (ac->want_keys == xi->have_keys)
  419. return 0;
  420. return -EAGAIN;
  421. }
  422. static int ceph_x_create_authorizer(
  423. struct ceph_auth_client *ac, int peer_type,
  424. struct ceph_authorizer **a,
  425. void **buf, size_t *len,
  426. void **reply_buf, size_t *reply_len)
  427. {
  428. struct ceph_x_authorizer *au;
  429. struct ceph_x_ticket_handler *th;
  430. int ret;
  431. th = get_ticket_handler(ac, peer_type);
  432. if (IS_ERR(th))
  433. return PTR_ERR(th);
  434. au = kzalloc(sizeof(*au), GFP_NOFS);
  435. if (!au)
  436. return -ENOMEM;
  437. ret = ceph_x_build_authorizer(ac, th, au);
  438. if (ret) {
  439. kfree(au);
  440. return ret;
  441. }
  442. *a = (struct ceph_authorizer *)au;
  443. *buf = au->buf->vec.iov_base;
  444. *len = au->buf->vec.iov_len;
  445. *reply_buf = au->reply_buf;
  446. *reply_len = sizeof(au->reply_buf);
  447. return 0;
  448. }
  449. static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
  450. struct ceph_authorizer *a, size_t len)
  451. {
  452. struct ceph_x_authorizer *au = (void *)a;
  453. struct ceph_x_ticket_handler *th;
  454. int ret = 0;
  455. struct ceph_x_authorize_reply reply;
  456. void *p = au->reply_buf;
  457. void *end = p + sizeof(au->reply_buf);
  458. th = get_ticket_handler(ac, au->service);
  459. if (!th)
  460. return -EIO; /* hrm! */
  461. ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
  462. if (ret < 0)
  463. return ret;
  464. if (ret != sizeof(reply))
  465. return -EPERM;
  466. if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
  467. ret = -EPERM;
  468. else
  469. ret = 0;
  470. dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
  471. au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
  472. return ret;
  473. }
  474. static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
  475. struct ceph_authorizer *a)
  476. {
  477. struct ceph_x_authorizer *au = (void *)a;
  478. ceph_buffer_put(au->buf);
  479. kfree(au);
  480. }
  481. static void ceph_x_reset(struct ceph_auth_client *ac)
  482. {
  483. struct ceph_x_info *xi = ac->private;
  484. dout("reset\n");
  485. xi->starting = true;
  486. xi->server_challenge = 0;
  487. }
  488. static void ceph_x_destroy(struct ceph_auth_client *ac)
  489. {
  490. struct ceph_x_info *xi = ac->private;
  491. struct rb_node *p;
  492. dout("ceph_x_destroy %p\n", ac);
  493. ceph_crypto_key_destroy(&xi->secret);
  494. while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
  495. struct ceph_x_ticket_handler *th =
  496. rb_entry(p, struct ceph_x_ticket_handler, node);
  497. remove_ticket_handler(ac, th);
  498. }
  499. kmem_cache_destroy(ceph_x_ticketbuf_cachep);
  500. kfree(ac->private);
  501. ac->private = NULL;
  502. }
  503. static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
  504. int peer_type)
  505. {
  506. struct ceph_x_ticket_handler *th;
  507. th = get_ticket_handler(ac, peer_type);
  508. if (th && !IS_ERR(th))
  509. remove_ticket_handler(ac, th);
  510. }
  511. static const struct ceph_auth_client_ops ceph_x_ops = {
  512. .is_authenticated = ceph_x_is_authenticated,
  513. .build_request = ceph_x_build_request,
  514. .handle_reply = ceph_x_handle_reply,
  515. .create_authorizer = ceph_x_create_authorizer,
  516. .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
  517. .destroy_authorizer = ceph_x_destroy_authorizer,
  518. .invalidate_authorizer = ceph_x_invalidate_authorizer,
  519. .reset = ceph_x_reset,
  520. .destroy = ceph_x_destroy,
  521. };
  522. int ceph_x_init(struct ceph_auth_client *ac)
  523. {
  524. struct ceph_x_info *xi;
  525. int ret;
  526. dout("ceph_x_init %p\n", ac);
  527. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  528. if (!xi)
  529. return -ENOMEM;
  530. ret = -ENOMEM;
  531. ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
  532. TEMP_TICKET_BUF_LEN, 8,
  533. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  534. NULL);
  535. if (!ceph_x_ticketbuf_cachep)
  536. goto done_nomem;
  537. ret = -EINVAL;
  538. if (!ac->secret) {
  539. pr_err("no secret set (for auth_x protocol)\n");
  540. goto done_nomem;
  541. }
  542. ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
  543. if (ret)
  544. goto done_nomem;
  545. xi->starting = true;
  546. xi->ticket_handlers = RB_ROOT;
  547. ac->protocol = CEPH_AUTH_CEPHX;
  548. ac->private = xi;
  549. ac->ops = &ceph_x_ops;
  550. return 0;
  551. done_nomem:
  552. kfree(xi);
  553. if (ceph_x_ticketbuf_cachep)
  554. kmem_cache_destroy(ceph_x_ticketbuf_cachep);
  555. return ret;
  556. }