2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/module.h>
6 #include <linux/random.h>
7 #include <linux/slab.h>
9 #include <linux/ceph/decode.h>
10 #include <linux/ceph/auth.h>
14 #include "auth_x_protocol.h"
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_should_authenticate(struct ceph_auth_client *ac)
33 struct ceph_x_info *xi = ac->private;
36 ceph_x_validate_tickets(ac, &need);
37 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
38 ac->want_keys, need, xi->have_keys);
42 static int ceph_x_encrypt_buflen(int ilen)
44 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
48 static int ceph_x_encrypt(struct ceph_crypto_key *secret,
49 void *ibuf, int ilen, void *obuf, size_t olen)
51 struct ceph_x_encrypt_header head = {
53 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
55 size_t len = olen - sizeof(u32);
58 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
59 &head, sizeof(head), ibuf, ilen);
62 ceph_encode_32(&obuf, len);
63 return len + sizeof(u32);
66 static int ceph_x_decrypt(struct ceph_crypto_key *secret,
67 void **p, void *end, void *obuf, size_t olen)
69 struct ceph_x_encrypt_header head;
70 size_t head_len = sizeof(head);
73 len = ceph_decode_32(p);
77 dout("ceph_x_decrypt len %d\n", len);
78 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
82 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
89 * get existing (or insert new) ticket handler
91 static struct ceph_x_ticket_handler *
92 get_ticket_handler(struct ceph_auth_client *ac, int service)
94 struct ceph_x_ticket_handler *th;
95 struct ceph_x_info *xi = ac->private;
96 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
100 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
101 if (service < th->service)
103 else if (service > th->service)
110 th = kzalloc(sizeof(*th), GFP_NOFS);
112 return ERR_PTR(-ENOMEM);
113 th->service = service;
114 rb_link_node(&th->node, parent, p);
115 rb_insert_color(&th->node, &xi->ticket_handlers);
119 static void remove_ticket_handler(struct ceph_auth_client *ac,
120 struct ceph_x_ticket_handler *th)
122 struct ceph_x_info *xi = ac->private;
124 dout("remove_ticket_handler %p %d\n", th, th->service);
125 rb_erase(&th->node, &xi->ticket_handlers);
126 ceph_crypto_key_destroy(&th->session_key);
128 ceph_buffer_put(th->ticket_blob);
132 static int process_one_ticket(struct ceph_auth_client *ac,
133 struct ceph_crypto_key *secret,
135 void *dbuf, void *ticket_buf)
137 struct ceph_x_info *xi = ac->private;
139 u8 tkt_struct_v, blob_struct_v;
140 struct ceph_x_ticket_handler *th;
144 struct timespec validity;
145 struct ceph_crypto_key old_key;
147 struct ceph_timespec new_validity;
148 struct ceph_crypto_key new_session_key;
149 struct ceph_buffer *new_ticket_blob;
150 unsigned long new_expires, new_renew_after;
154 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
156 type = ceph_decode_32(p);
157 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
159 tkt_struct_v = ceph_decode_8(p);
160 if (tkt_struct_v != 1)
163 th = get_ticket_handler(ac, type);
170 dlen = ceph_x_decrypt(secret, p, end, dbuf,
171 TEMP_TICKET_BUF_LEN);
176 dout(" decrypted %d bytes\n", dlen);
180 tkt_struct_v = ceph_decode_8(&dp);
181 if (tkt_struct_v != 1)
184 memcpy(&old_key, &th->session_key, sizeof(old_key));
185 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
189 ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
190 ceph_decode_timespec(&validity, &new_validity);
191 new_expires = get_seconds() + validity.tv_sec;
192 new_renew_after = new_expires - (validity.tv_sec / 4);
193 dout(" expires=%lu renew_after=%lu\n", new_expires,
196 /* ticket blob for service */
197 ceph_decode_8_safe(p, end, is_enc, bad);
201 dout(" encrypted ticket\n");
202 dlen = ceph_x_decrypt(&old_key, p, end, ticket_buf,
203 TEMP_TICKET_BUF_LEN);
208 dlen = ceph_decode_32(&tp);
211 ceph_decode_32_safe(p, end, dlen, bad);
212 ceph_decode_need(p, end, dlen, bad);
213 ceph_decode_copy(p, ticket_buf, dlen);
216 dout(" ticket blob is %d bytes\n", dlen);
217 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
218 blob_struct_v = ceph_decode_8(&tp);
219 new_secret_id = ceph_decode_64(&tp);
220 ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
224 /* all is well, update our ticket */
225 ceph_crypto_key_destroy(&th->session_key);
227 ceph_buffer_put(th->ticket_blob);
228 th->session_key = new_session_key;
229 th->ticket_blob = new_ticket_blob;
230 th->validity = new_validity;
231 th->secret_id = new_secret_id;
232 th->expires = new_expires;
233 th->renew_after = new_renew_after;
234 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
235 type, ceph_entity_type_name(type), th->secret_id,
236 (int)th->ticket_blob->vec.iov_len);
237 xi->have_keys |= th->service;
247 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
248 struct ceph_crypto_key *secret,
249 void *buf, void *end)
258 dbuf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
263 ticket_buf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
267 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
268 if (reply_struct_v != 1)
271 ceph_decode_32_safe(&p, end, num, bad);
272 dout("%d tickets\n", num);
275 ret = process_one_ticket(ac, secret, &p, end,
293 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
294 struct ceph_x_ticket_handler *th,
295 struct ceph_x_authorizer *au)
298 struct ceph_x_authorize_a *msg_a;
299 struct ceph_x_authorize_b msg_b;
302 int ticket_blob_len =
303 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
305 dout("build_authorizer for %s %p\n",
306 ceph_entity_type_name(th->service), au);
308 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
309 ceph_x_encrypt_buflen(ticket_blob_len);
310 dout(" need len %d\n", maxlen);
311 if (au->buf && au->buf->alloc_len < maxlen) {
312 ceph_buffer_put(au->buf);
316 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
320 au->service = th->service;
321 au->secret_id = th->secret_id;
323 msg_a = au->buf->vec.iov_base;
325 msg_a->global_id = cpu_to_le64(ac->global_id);
326 msg_a->service_id = cpu_to_le32(th->service);
327 msg_a->ticket_blob.struct_v = 1;
328 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
329 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
330 if (ticket_blob_len) {
331 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
332 th->ticket_blob->vec.iov_len);
334 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
335 le64_to_cpu(msg_a->ticket_blob.secret_id));
338 p += ticket_blob_len;
339 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
341 get_random_bytes(&au->nonce, sizeof(au->nonce));
343 msg_b.nonce = cpu_to_le64(au->nonce);
344 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
349 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
350 dout(" built authorizer nonce %llx len %d\n", au->nonce,
351 (int)au->buf->vec.iov_len);
352 BUG_ON(au->buf->vec.iov_len > maxlen);
356 ceph_buffer_put(au->buf);
361 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
364 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
366 ceph_encode_64(p, th->secret_id);
367 if (th->ticket_blob) {
368 const char *buf = th->ticket_blob->vec.iov_base;
369 u32 len = th->ticket_blob->vec.iov_len;
371 ceph_encode_32_safe(p, end, len, bad);
372 ceph_encode_copy_safe(p, end, buf, len, bad);
374 ceph_encode_32_safe(p, end, 0, bad);
382 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
384 int want = ac->want_keys;
385 struct ceph_x_info *xi = ac->private;
388 *pneed = ac->want_keys & ~(xi->have_keys);
390 for (service = 1; service <= want; service <<= 1) {
391 struct ceph_x_ticket_handler *th;
393 if (!(ac->want_keys & service))
396 if (*pneed & service)
399 th = get_ticket_handler(ac, service);
406 if (get_seconds() >= th->renew_after)
408 if (get_seconds() >= th->expires)
409 xi->have_keys &= ~service;
414 static int ceph_x_build_request(struct ceph_auth_client *ac,
415 void *buf, void *end)
417 struct ceph_x_info *xi = ac->private;
419 struct ceph_x_request_header *head = buf;
421 struct ceph_x_ticket_handler *th =
422 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
427 ceph_x_validate_tickets(ac, &need);
429 dout("build_request want %x have %x need %x\n",
430 ac->want_keys, xi->have_keys, need);
432 if (need & CEPH_ENTITY_TYPE_AUTH) {
433 struct ceph_x_authenticate *auth = (void *)(head + 1);
435 struct ceph_x_challenge_blob tmp;
442 dout(" get_auth_session_key\n");
443 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
445 /* encrypt and hash */
446 get_random_bytes(&auth->client_challenge, sizeof(u64));
447 tmp.client_challenge = auth->client_challenge;
448 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
449 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
450 tmp_enc, sizeof(tmp_enc));
456 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
457 auth->key ^= *(__le64 *)u;
458 dout(" server_challenge %llx client_challenge %llx key %llx\n",
459 xi->server_challenge, le64_to_cpu(auth->client_challenge),
460 le64_to_cpu(auth->key));
462 /* now encode the old ticket if exists */
463 ret = ceph_x_encode_ticket(th, &p, end);
472 struct ceph_x_service_ticket_request *req;
476 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
478 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
481 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
482 xi->auth_authorizer.buf->vec.iov_len);
485 req->keys = cpu_to_le32(need);
493 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
494 void *buf, void *end)
496 struct ceph_x_info *xi = ac->private;
497 struct ceph_x_reply_header *head = buf;
498 struct ceph_x_ticket_handler *th;
504 return result; /* XXX hmm? */
508 struct ceph_x_server_challenge *sc = buf;
510 if (len != sizeof(*sc))
512 xi->server_challenge = le64_to_cpu(sc->server_challenge);
513 dout("handle_reply got server challenge %llx\n",
514 xi->server_challenge);
515 xi->starting = false;
516 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
520 op = le16_to_cpu(head->op);
521 result = le32_to_cpu(head->result);
522 dout("handle_reply op %d result %d\n", op, result);
524 case CEPHX_GET_AUTH_SESSION_KEY:
525 /* verify auth key */
526 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
527 buf + sizeof(*head), end);
530 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
531 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
534 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
535 buf + sizeof(*head), end);
543 if (ac->want_keys == xi->have_keys)
548 static int ceph_x_create_authorizer(
549 struct ceph_auth_client *ac, int peer_type,
550 struct ceph_auth_handshake *auth)
552 struct ceph_x_authorizer *au;
553 struct ceph_x_ticket_handler *th;
556 th = get_ticket_handler(ac, peer_type);
560 au = kzalloc(sizeof(*au), GFP_NOFS);
564 ret = ceph_x_build_authorizer(ac, th, au);
570 auth->authorizer = (struct ceph_authorizer *) au;
571 auth->authorizer_buf = au->buf->vec.iov_base;
572 auth->authorizer_buf_len = au->buf->vec.iov_len;
573 auth->authorizer_reply_buf = au->reply_buf;
574 auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
579 static int ceph_x_update_authorizer(
580 struct ceph_auth_client *ac, int peer_type,
581 struct ceph_auth_handshake *auth)
583 struct ceph_x_authorizer *au;
584 struct ceph_x_ticket_handler *th;
586 th = get_ticket_handler(ac, peer_type);
590 au = (struct ceph_x_authorizer *)auth->authorizer;
591 if (au->secret_id < th->secret_id) {
592 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
593 au->service, au->secret_id, th->secret_id);
594 return ceph_x_build_authorizer(ac, th, au);
599 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
600 struct ceph_authorizer *a, size_t len)
602 struct ceph_x_authorizer *au = (void *)a;
603 struct ceph_x_ticket_handler *th;
605 struct ceph_x_authorize_reply reply;
606 void *p = au->reply_buf;
607 void *end = p + sizeof(au->reply_buf);
609 th = get_ticket_handler(ac, au->service);
612 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
615 if (ret != sizeof(reply))
618 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
622 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
623 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
627 static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
628 struct ceph_authorizer *a)
630 struct ceph_x_authorizer *au = (void *)a;
632 ceph_buffer_put(au->buf);
637 static void ceph_x_reset(struct ceph_auth_client *ac)
639 struct ceph_x_info *xi = ac->private;
643 xi->server_challenge = 0;
646 static void ceph_x_destroy(struct ceph_auth_client *ac)
648 struct ceph_x_info *xi = ac->private;
651 dout("ceph_x_destroy %p\n", ac);
652 ceph_crypto_key_destroy(&xi->secret);
654 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
655 struct ceph_x_ticket_handler *th =
656 rb_entry(p, struct ceph_x_ticket_handler, node);
657 remove_ticket_handler(ac, th);
660 if (xi->auth_authorizer.buf)
661 ceph_buffer_put(xi->auth_authorizer.buf);
667 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
670 struct ceph_x_ticket_handler *th;
672 th = get_ticket_handler(ac, peer_type);
674 memset(&th->validity, 0, sizeof(th->validity));
678 static const struct ceph_auth_client_ops ceph_x_ops = {
680 .is_authenticated = ceph_x_is_authenticated,
681 .should_authenticate = ceph_x_should_authenticate,
682 .build_request = ceph_x_build_request,
683 .handle_reply = ceph_x_handle_reply,
684 .create_authorizer = ceph_x_create_authorizer,
685 .update_authorizer = ceph_x_update_authorizer,
686 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
687 .destroy_authorizer = ceph_x_destroy_authorizer,
688 .invalidate_authorizer = ceph_x_invalidate_authorizer,
689 .reset = ceph_x_reset,
690 .destroy = ceph_x_destroy,
694 int ceph_x_init(struct ceph_auth_client *ac)
696 struct ceph_x_info *xi;
699 dout("ceph_x_init %p\n", ac);
701 xi = kzalloc(sizeof(*xi), GFP_NOFS);
707 pr_err("no secret set (for auth_x protocol)\n");
711 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
713 pr_err("cannot clone key: %d\n", ret);
718 xi->ticket_handlers = RB_ROOT;
720 ac->protocol = CEPH_AUTH_CEPHX;
722 ac->ops = &ceph_x_ops;