2 #include "ceph_debug.h"
5 #include <linux/module.h>
6 #include <linux/random.h>
9 #include "auth_x_protocol.h"
14 struct kmem_cache *ceph_x_ticketbuf_cachep;
16 #define TEMP_TICKET_BUF_LEN 256
18 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
20 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
22 struct ceph_x_info *xi = ac->private;
25 ceph_x_validate_tickets(ac, &need);
26 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
27 ac->want_keys, need, xi->have_keys);
28 return (ac->want_keys & xi->have_keys) == ac->want_keys;
31 static int ceph_x_encrypt_buflen(int ilen)
33 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
37 static int ceph_x_encrypt(struct ceph_crypto_key *secret,
38 void *ibuf, int ilen, void *obuf, size_t olen)
40 struct ceph_x_encrypt_header head = {
42 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
44 size_t len = olen - sizeof(u32);
47 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
48 &head, sizeof(head), ibuf, ilen);
51 ceph_encode_32(&obuf, len);
52 return len + sizeof(u32);
55 static int ceph_x_decrypt(struct ceph_crypto_key *secret,
56 void **p, void *end, void *obuf, size_t olen)
58 struct ceph_x_encrypt_header head;
59 size_t head_len = sizeof(head);
62 len = ceph_decode_32(p);
66 dout("ceph_x_decrypt len %d\n", len);
67 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
71 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
78 * get existing (or insert new) ticket handler
80 struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
83 struct ceph_x_ticket_handler *th;
84 struct ceph_x_info *xi = ac->private;
85 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
89 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
90 if (service < th->service)
92 else if (service > th->service)
99 th = kzalloc(sizeof(*th), GFP_NOFS);
101 return ERR_PTR(-ENOMEM);
102 th->service = service;
103 rb_link_node(&th->node, parent, p);
104 rb_insert_color(&th->node, &xi->ticket_handlers);
108 static void remove_ticket_handler(struct ceph_auth_client *ac,
109 struct ceph_x_ticket_handler *th)
111 struct ceph_x_info *xi = ac->private;
113 dout("remove_ticket_handler %p %d\n", th, th->service);
114 rb_erase(&th->node, &xi->ticket_handlers);
115 ceph_crypto_key_destroy(&th->session_key);
117 ceph_buffer_put(th->ticket_blob);
121 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
122 struct ceph_crypto_key *secret,
123 void *buf, void *end)
125 struct ceph_x_info *xi = ac->private;
133 dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
138 ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
139 GFP_NOFS | GFP_ATOMIC);
143 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
144 struct_v = ceph_decode_8(&p);
147 num = ceph_decode_32(&p);
148 dout("%d tickets\n", num);
152 struct ceph_x_ticket_handler *th;
156 struct timespec validity;
157 struct ceph_crypto_key old_key;
159 struct ceph_timespec new_validity;
160 struct ceph_crypto_key new_session_key;
161 struct ceph_buffer *new_ticket_blob;
162 unsigned long new_expires, new_renew_after;
165 ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
167 type = ceph_decode_32(&p);
168 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
170 struct_v = ceph_decode_8(&p);
174 th = get_ticket_handler(ac, type);
181 dlen = ceph_x_decrypt(secret, &p, end, dbuf,
182 TEMP_TICKET_BUF_LEN);
187 dout(" decrypted %d bytes\n", dlen);
191 struct_v = ceph_decode_8(&dp);
195 memcpy(&old_key, &th->session_key, sizeof(old_key));
196 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
200 ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
201 ceph_decode_timespec(&validity, &new_validity);
202 new_expires = get_seconds() + validity.tv_sec;
203 new_renew_after = new_expires - (validity.tv_sec / 4);
204 dout(" expires=%lu renew_after=%lu\n", new_expires,
207 /* ticket blob for service */
208 ceph_decode_8_safe(&p, end, is_enc, bad);
212 dout(" encrypted ticket\n");
213 dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
214 TEMP_TICKET_BUF_LEN);
219 dlen = ceph_decode_32(&tp);
222 ceph_decode_32_safe(&p, end, dlen, bad);
223 ceph_decode_need(&p, end, dlen, bad);
224 ceph_decode_copy(&p, ticket_buf, dlen);
227 dout(" ticket blob is %d bytes\n", dlen);
228 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
229 struct_v = ceph_decode_8(&tp);
230 new_secret_id = ceph_decode_64(&tp);
231 ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
235 /* all is well, update our ticket */
236 ceph_crypto_key_destroy(&th->session_key);
238 ceph_buffer_put(th->ticket_blob);
239 th->session_key = new_session_key;
240 th->ticket_blob = new_ticket_blob;
241 th->validity = new_validity;
242 th->secret_id = new_secret_id;
243 th->expires = new_expires;
244 th->renew_after = new_renew_after;
245 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
246 type, ceph_entity_type_name(type), th->secret_id,
247 (int)th->ticket_blob->vec.iov_len);
248 xi->have_keys |= th->service;
253 kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
255 kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
263 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
264 struct ceph_x_ticket_handler *th,
265 struct ceph_x_authorizer *au)
268 struct ceph_x_authorize_a *msg_a;
269 struct ceph_x_authorize_b msg_b;
272 int ticket_blob_len =
273 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
275 dout("build_authorizer for %s %p\n",
276 ceph_entity_type_name(th->service), au);
278 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
279 ceph_x_encrypt_buflen(ticket_blob_len);
280 dout(" need len %d\n", maxlen);
281 if (au->buf && au->buf->alloc_len < maxlen) {
282 ceph_buffer_put(au->buf);
286 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
290 au->service = th->service;
292 msg_a = au->buf->vec.iov_base;
294 msg_a->global_id = cpu_to_le64(ac->global_id);
295 msg_a->service_id = cpu_to_le32(th->service);
296 msg_a->ticket_blob.struct_v = 1;
297 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
298 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
299 if (ticket_blob_len) {
300 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
301 th->ticket_blob->vec.iov_len);
303 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
304 le64_to_cpu(msg_a->ticket_blob.secret_id));
307 p += ticket_blob_len;
308 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
310 get_random_bytes(&au->nonce, sizeof(au->nonce));
312 msg_b.nonce = cpu_to_le64(au->nonce);
313 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
318 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
319 dout(" built authorizer nonce %llx len %d\n", au->nonce,
320 (int)au->buf->vec.iov_len);
321 BUG_ON(au->buf->vec.iov_len > maxlen);
325 ceph_buffer_put(au->buf);
330 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
333 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
335 ceph_encode_64(p, th->secret_id);
336 if (th->ticket_blob) {
337 const char *buf = th->ticket_blob->vec.iov_base;
338 u32 len = th->ticket_blob->vec.iov_len;
340 ceph_encode_32_safe(p, end, len, bad);
341 ceph_encode_copy_safe(p, end, buf, len, bad);
343 ceph_encode_32_safe(p, end, 0, bad);
351 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
353 int want = ac->want_keys;
354 struct ceph_x_info *xi = ac->private;
357 *pneed = ac->want_keys & ~(xi->have_keys);
359 for (service = 1; service <= want; service <<= 1) {
360 struct ceph_x_ticket_handler *th;
362 if (!(ac->want_keys & service))
365 if (*pneed & service)
368 th = get_ticket_handler(ac, service);
375 if (get_seconds() >= th->renew_after)
377 if (get_seconds() >= th->expires)
378 xi->have_keys &= ~service;
383 static int ceph_x_build_request(struct ceph_auth_client *ac,
384 void *buf, void *end)
386 struct ceph_x_info *xi = ac->private;
388 struct ceph_x_request_header *head = buf;
390 struct ceph_x_ticket_handler *th =
391 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
393 ceph_x_validate_tickets(ac, &need);
395 dout("build_request want %x have %x need %x\n",
396 ac->want_keys, xi->have_keys, need);
398 if (need & CEPH_ENTITY_TYPE_AUTH) {
399 struct ceph_x_authenticate *auth = (void *)(head + 1);
401 struct ceph_x_challenge_blob tmp;
408 dout(" get_auth_session_key\n");
409 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
411 /* encrypt and hash */
412 get_random_bytes(&auth->client_challenge, sizeof(u64));
413 tmp.client_challenge = auth->client_challenge;
414 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
415 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
416 tmp_enc, sizeof(tmp_enc));
422 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
424 dout(" server_challenge %llx client_challenge %llx key %llx\n",
425 xi->server_challenge, le64_to_cpu(auth->client_challenge),
426 le64_to_cpu(auth->key));
428 /* now encode the old ticket if exists */
429 ret = ceph_x_encode_ticket(th, &p, end);
438 struct ceph_x_service_ticket_request *req;
442 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
445 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
448 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
449 xi->auth_authorizer.buf->vec.iov_len);
452 req->keys = cpu_to_le32(need);
460 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
461 void *buf, void *end)
463 struct ceph_x_info *xi = ac->private;
464 struct ceph_x_reply_header *head = buf;
465 struct ceph_x_ticket_handler *th;
471 return result; /* XXX hmm? */
475 struct ceph_x_server_challenge *sc = buf;
477 if (len != sizeof(*sc))
479 xi->server_challenge = le64_to_cpu(sc->server_challenge);
480 dout("handle_reply got server challenge %llx\n",
481 xi->server_challenge);
482 xi->starting = false;
483 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
487 op = le32_to_cpu(head->op);
488 result = le32_to_cpu(head->result);
489 dout("handle_reply op %d result %d\n", op, result);
491 case CEPHX_GET_AUTH_SESSION_KEY:
492 /* verify auth key */
493 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
494 buf + sizeof(*head), end);
497 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
498 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
500 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
501 buf + sizeof(*head), end);
509 if (ac->want_keys == xi->have_keys)
514 static int ceph_x_create_authorizer(
515 struct ceph_auth_client *ac, int peer_type,
516 struct ceph_authorizer **a,
517 void **buf, size_t *len,
518 void **reply_buf, size_t *reply_len)
520 struct ceph_x_authorizer *au;
521 struct ceph_x_ticket_handler *th;
524 th = get_ticket_handler(ac, peer_type);
528 au = kzalloc(sizeof(*au), GFP_NOFS);
532 ret = ceph_x_build_authorizer(ac, th, au);
538 *a = (struct ceph_authorizer *)au;
539 *buf = au->buf->vec.iov_base;
540 *len = au->buf->vec.iov_len;
541 *reply_buf = au->reply_buf;
542 *reply_len = sizeof(au->reply_buf);
546 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
547 struct ceph_authorizer *a, size_t len)
549 struct ceph_x_authorizer *au = (void *)a;
550 struct ceph_x_ticket_handler *th;
552 struct ceph_x_authorize_reply reply;
553 void *p = au->reply_buf;
554 void *end = p + sizeof(au->reply_buf);
556 th = get_ticket_handler(ac, au->service);
558 return -EIO; /* hrm! */
559 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
562 if (ret != sizeof(reply))
565 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
569 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
570 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
574 static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
575 struct ceph_authorizer *a)
577 struct ceph_x_authorizer *au = (void *)a;
579 ceph_buffer_put(au->buf);
584 static void ceph_x_reset(struct ceph_auth_client *ac)
586 struct ceph_x_info *xi = ac->private;
590 xi->server_challenge = 0;
593 static void ceph_x_destroy(struct ceph_auth_client *ac)
595 struct ceph_x_info *xi = ac->private;
598 dout("ceph_x_destroy %p\n", ac);
599 ceph_crypto_key_destroy(&xi->secret);
601 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
602 struct ceph_x_ticket_handler *th =
603 rb_entry(p, struct ceph_x_ticket_handler, node);
604 remove_ticket_handler(ac, th);
607 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
613 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
616 struct ceph_x_ticket_handler *th;
618 th = get_ticket_handler(ac, peer_type);
619 if (th && !IS_ERR(th))
620 remove_ticket_handler(ac, th);
624 static const struct ceph_auth_client_ops ceph_x_ops = {
625 .is_authenticated = ceph_x_is_authenticated,
626 .build_request = ceph_x_build_request,
627 .handle_reply = ceph_x_handle_reply,
628 .create_authorizer = ceph_x_create_authorizer,
629 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
630 .destroy_authorizer = ceph_x_destroy_authorizer,
631 .invalidate_authorizer = ceph_x_invalidate_authorizer,
632 .reset = ceph_x_reset,
633 .destroy = ceph_x_destroy,
637 int ceph_x_init(struct ceph_auth_client *ac)
639 struct ceph_x_info *xi;
642 dout("ceph_x_init %p\n", ac);
643 xi = kzalloc(sizeof(*xi), GFP_NOFS);
648 ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
649 TEMP_TICKET_BUF_LEN, 8,
650 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
652 if (!ceph_x_ticketbuf_cachep)
656 pr_err("no secret set (for auth_x protocol)\n");
660 ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
665 xi->ticket_handlers = RB_ROOT;
667 ac->protocol = CEPH_AUTH_CEPHX;
669 ac->ops = &ceph_x_ops;
674 if (ceph_x_ticketbuf_cachep)
675 kmem_cache_destroy(ceph_x_ticketbuf_cachep);