auth_x.c 16 KB

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