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>
11 #include <linux/ceph/libceph.h>
12 #include <linux/ceph/messenger.h>
16 #include "auth_x_protocol.h"
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_offset(void)
44 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
47 static int ceph_x_encrypt_buflen(int ilen)
49 return ceph_x_encrypt_offset() + ilen + 16;
52 static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
53 int buf_len, int plaintext_len)
55 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
60 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
62 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
63 plaintext_len + sizeof(struct ceph_x_encrypt_header),
68 ceph_encode_32(&buf, ciphertext_len);
69 return sizeof(u32) + ciphertext_len;
72 static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
74 struct ceph_x_encrypt_header *hdr = *p + sizeof(u32);
75 int ciphertext_len, plaintext_len;
78 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
79 ceph_decode_need(p, end, ciphertext_len, e_inval);
81 ret = ceph_crypt(secret, false, *p, end - *p, ciphertext_len,
86 if (hdr->struct_v != 1 || le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC)
90 return plaintext_len - sizeof(struct ceph_x_encrypt_header);
97 * get existing (or insert new) ticket handler
99 static struct ceph_x_ticket_handler *
100 get_ticket_handler(struct ceph_auth_client *ac, int service)
102 struct ceph_x_ticket_handler *th;
103 struct ceph_x_info *xi = ac->private;
104 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
108 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
109 if (service < th->service)
111 else if (service > th->service)
118 th = kzalloc(sizeof(*th), GFP_NOFS);
120 return ERR_PTR(-ENOMEM);
121 th->service = service;
122 rb_link_node(&th->node, parent, p);
123 rb_insert_color(&th->node, &xi->ticket_handlers);
127 static void remove_ticket_handler(struct ceph_auth_client *ac,
128 struct ceph_x_ticket_handler *th)
130 struct ceph_x_info *xi = ac->private;
132 dout("remove_ticket_handler %p %d\n", th, th->service);
133 rb_erase(&th->node, &xi->ticket_handlers);
134 ceph_crypto_key_destroy(&th->session_key);
136 ceph_buffer_put(th->ticket_blob);
140 static int process_one_ticket(struct ceph_auth_client *ac,
141 struct ceph_crypto_key *secret,
144 struct ceph_x_info *xi = ac->private;
146 u8 tkt_struct_v, blob_struct_v;
147 struct ceph_x_ticket_handler *th;
151 struct timespec validity;
154 struct ceph_crypto_key new_session_key = { 0 };
155 struct ceph_buffer *new_ticket_blob;
156 unsigned long new_expires, new_renew_after;
160 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
162 type = ceph_decode_32(p);
163 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
165 tkt_struct_v = ceph_decode_8(p);
166 if (tkt_struct_v != 1)
169 th = get_ticket_handler(ac, type);
176 dp = *p + ceph_x_encrypt_offset();
177 ret = ceph_x_decrypt(secret, p, end);
180 dout(" decrypted %d bytes\n", ret);
183 tkt_struct_v = ceph_decode_8(&dp);
184 if (tkt_struct_v != 1)
187 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
191 ceph_decode_timespec(&validity, dp);
192 dp += sizeof(struct ceph_timespec);
193 new_expires = get_seconds() + validity.tv_sec;
194 new_renew_after = new_expires - (validity.tv_sec / 4);
195 dout(" expires=%lu renew_after=%lu\n", new_expires,
198 /* ticket blob for service */
199 ceph_decode_8_safe(p, end, is_enc, bad);
202 tp = *p + ceph_x_encrypt_offset();
203 ret = ceph_x_decrypt(&th->session_key, p, end);
206 dout(" encrypted ticket, decrypted %d bytes\n", ret);
214 ceph_decode_32_safe(ptp, tpend, dlen, bad);
215 dout(" ticket blob is %d bytes\n", dlen);
216 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
217 blob_struct_v = ceph_decode_8(ptp);
218 if (blob_struct_v != 1)
221 new_secret_id = ceph_decode_64(ptp);
222 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
226 /* all is well, update our ticket */
227 ceph_crypto_key_destroy(&th->session_key);
229 ceph_buffer_put(th->ticket_blob);
230 th->session_key = new_session_key;
231 th->ticket_blob = new_ticket_blob;
232 th->secret_id = new_secret_id;
233 th->expires = new_expires;
234 th->renew_after = new_renew_after;
236 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
237 type, ceph_entity_type_name(type), th->secret_id,
238 (int)th->ticket_blob->vec.iov_len);
239 xi->have_keys |= th->service;
245 ceph_crypto_key_destroy(&new_session_key);
249 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
250 struct ceph_crypto_key *secret,
251 void *buf, void *end)
258 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
259 if (reply_struct_v != 1)
262 ceph_decode_32_safe(&p, end, num, bad);
263 dout("%d tickets\n", num);
266 ret = process_one_ticket(ac, secret, &p, end);
277 static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
279 ceph_crypto_key_destroy(&au->session_key);
281 ceph_buffer_put(au->buf);
286 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
287 struct ceph_x_ticket_handler *th,
288 struct ceph_x_authorizer *au)
291 struct ceph_x_authorize_a *msg_a;
292 struct ceph_x_authorize_b *msg_b;
295 int ticket_blob_len =
296 (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);
301 ceph_crypto_key_destroy(&au->session_key);
302 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
306 maxlen = sizeof(*msg_a) + ticket_blob_len +
307 ceph_x_encrypt_buflen(sizeof(*msg_b));
308 dout(" need len %d\n", maxlen);
309 if (au->buf && au->buf->alloc_len < maxlen) {
310 ceph_buffer_put(au->buf);
314 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 msg_b = p + ceph_x_encrypt_offset();
343 get_random_bytes(&au->nonce, sizeof(au->nonce));
344 msg_b->nonce = cpu_to_le64(au->nonce);
345 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
351 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
352 dout(" built authorizer nonce %llx len %d\n", au->nonce,
353 (int)au->buf->vec.iov_len);
357 ceph_x_authorizer_cleanup(au);
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 bool need_key(struct ceph_x_ticket_handler *th)
387 return get_seconds() >= th->renew_after;
390 static bool have_key(struct ceph_x_ticket_handler *th)
393 if (get_seconds() >= th->expires)
394 th->have_key = false;
400 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
402 int want = ac->want_keys;
403 struct ceph_x_info *xi = ac->private;
406 *pneed = ac->want_keys & ~(xi->have_keys);
408 for (service = 1; service <= want; service <<= 1) {
409 struct ceph_x_ticket_handler *th;
411 if (!(ac->want_keys & service))
414 if (*pneed & service)
417 th = get_ticket_handler(ac, service);
426 xi->have_keys &= ~service;
430 static int ceph_x_build_request(struct ceph_auth_client *ac,
431 void *buf, void *end)
433 struct ceph_x_info *xi = ac->private;
435 struct ceph_x_request_header *head = buf;
437 struct ceph_x_ticket_handler *th =
438 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
443 ceph_x_validate_tickets(ac, &need);
445 dout("build_request want %x have %x need %x\n",
446 ac->want_keys, xi->have_keys, need);
448 if (need & CEPH_ENTITY_TYPE_AUTH) {
449 struct ceph_x_authenticate *auth = (void *)(head + 1);
451 void *enc_buf = xi->auth_authorizer.enc_buf;
452 struct ceph_x_challenge_blob *blob = enc_buf +
453 ceph_x_encrypt_offset();
459 dout(" get_auth_session_key\n");
460 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
462 /* encrypt and hash */
463 get_random_bytes(&auth->client_challenge, sizeof(u64));
464 blob->client_challenge = auth->client_challenge;
465 blob->server_challenge = cpu_to_le64(xi->server_challenge);
466 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
473 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
474 auth->key ^= *(__le64 *)u;
475 dout(" server_challenge %llx client_challenge %llx key %llx\n",
476 xi->server_challenge, le64_to_cpu(auth->client_challenge),
477 le64_to_cpu(auth->key));
479 /* now encode the old ticket if exists */
480 ret = ceph_x_encode_ticket(th, &p, end);
489 struct ceph_x_service_ticket_request *req;
493 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
495 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
498 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
499 xi->auth_authorizer.buf->vec.iov_len);
502 req->keys = cpu_to_le32(need);
510 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
511 void *buf, void *end)
513 struct ceph_x_info *xi = ac->private;
514 struct ceph_x_reply_header *head = buf;
515 struct ceph_x_ticket_handler *th;
521 return result; /* XXX hmm? */
525 struct ceph_x_server_challenge *sc = buf;
527 if (len != sizeof(*sc))
529 xi->server_challenge = le64_to_cpu(sc->server_challenge);
530 dout("handle_reply got server challenge %llx\n",
531 xi->server_challenge);
532 xi->starting = false;
533 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
537 op = le16_to_cpu(head->op);
538 result = le32_to_cpu(head->result);
539 dout("handle_reply op %d result %d\n", op, result);
541 case CEPHX_GET_AUTH_SESSION_KEY:
542 /* verify auth key */
543 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
544 buf + sizeof(*head), end);
547 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
548 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
551 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
552 buf + sizeof(*head), end);
560 if (ac->want_keys == xi->have_keys)
565 static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
567 struct ceph_x_authorizer *au = (void *)a;
569 ceph_x_authorizer_cleanup(au);
573 static int ceph_x_create_authorizer(
574 struct ceph_auth_client *ac, int peer_type,
575 struct ceph_auth_handshake *auth)
577 struct ceph_x_authorizer *au;
578 struct ceph_x_ticket_handler *th;
581 th = get_ticket_handler(ac, peer_type);
585 au = kzalloc(sizeof(*au), GFP_NOFS);
589 au->base.destroy = ceph_x_destroy_authorizer;
591 ret = ceph_x_build_authorizer(ac, th, au);
597 auth->authorizer = (struct ceph_authorizer *) au;
598 auth->authorizer_buf = au->buf->vec.iov_base;
599 auth->authorizer_buf_len = au->buf->vec.iov_len;
600 auth->authorizer_reply_buf = au->enc_buf;
601 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
602 auth->sign_message = ac->ops->sign_message;
603 auth->check_message_signature = ac->ops->check_message_signature;
608 static int ceph_x_update_authorizer(
609 struct ceph_auth_client *ac, int peer_type,
610 struct ceph_auth_handshake *auth)
612 struct ceph_x_authorizer *au;
613 struct ceph_x_ticket_handler *th;
615 th = get_ticket_handler(ac, peer_type);
619 au = (struct ceph_x_authorizer *)auth->authorizer;
620 if (au->secret_id < th->secret_id) {
621 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
622 au->service, au->secret_id, th->secret_id);
623 return ceph_x_build_authorizer(ac, th, au);
628 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
629 struct ceph_authorizer *a)
631 struct ceph_x_authorizer *au = (void *)a;
632 void *p = au->enc_buf;
633 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
636 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
639 if (ret != sizeof(*reply))
642 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
646 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
647 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
651 static void ceph_x_reset(struct ceph_auth_client *ac)
653 struct ceph_x_info *xi = ac->private;
657 xi->server_challenge = 0;
660 static void ceph_x_destroy(struct ceph_auth_client *ac)
662 struct ceph_x_info *xi = ac->private;
665 dout("ceph_x_destroy %p\n", ac);
666 ceph_crypto_key_destroy(&xi->secret);
668 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
669 struct ceph_x_ticket_handler *th =
670 rb_entry(p, struct ceph_x_ticket_handler, node);
671 remove_ticket_handler(ac, th);
674 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
680 static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
682 struct ceph_x_ticket_handler *th;
684 th = get_ticket_handler(ac, peer_type);
686 th->have_key = false;
689 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
693 * We are to invalidate a service ticket in the hopes of
694 * getting a new, hopefully more valid, one. But, we won't get
695 * it unless our AUTH ticket is good, so invalidate AUTH ticket
696 * as well, just in case.
698 invalidate_ticket(ac, peer_type);
699 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
702 static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
705 void *enc_buf = au->enc_buf;
712 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
715 sigblock->len = cpu_to_le32(4*sizeof(u32));
716 sigblock->header_crc = msg->hdr.crc;
717 sigblock->front_crc = msg->footer.front_crc;
718 sigblock->middle_crc = msg->footer.middle_crc;
719 sigblock->data_crc = msg->footer.data_crc;
720 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
725 *psig = *(__le64 *)(enc_buf + sizeof(u32));
729 static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
730 struct ceph_msg *msg)
735 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
738 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
743 msg->footer.sig = sig;
744 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
748 static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
749 struct ceph_msg *msg)
754 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
757 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
761 if (sig_check == msg->footer.sig)
763 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
764 dout("ceph_x_check_message_signature %p has signature %llx "
765 "expect %llx\n", msg, msg->footer.sig, sig_check);
767 dout("ceph_x_check_message_signature %p sender did not set "
768 "CEPH_MSG_FOOTER_SIGNED\n", msg);
772 static const struct ceph_auth_client_ops ceph_x_ops = {
774 .is_authenticated = ceph_x_is_authenticated,
775 .should_authenticate = ceph_x_should_authenticate,
776 .build_request = ceph_x_build_request,
777 .handle_reply = ceph_x_handle_reply,
778 .create_authorizer = ceph_x_create_authorizer,
779 .update_authorizer = ceph_x_update_authorizer,
780 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
781 .invalidate_authorizer = ceph_x_invalidate_authorizer,
782 .reset = ceph_x_reset,
783 .destroy = ceph_x_destroy,
784 .sign_message = ceph_x_sign_message,
785 .check_message_signature = ceph_x_check_message_signature,
789 int ceph_x_init(struct ceph_auth_client *ac)
791 struct ceph_x_info *xi;
794 dout("ceph_x_init %p\n", ac);
796 xi = kzalloc(sizeof(*xi), GFP_NOFS);
802 pr_err("no secret set (for auth_x protocol)\n");
806 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
808 pr_err("cannot clone key: %d\n", ret);
813 xi->ticket_handlers = RB_ROOT;
815 ac->protocol = CEPH_AUTH_CEPHX;
817 ac->ops = &ceph_x_ops;