1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/ceph_debug.h>
6 #include <linux/module.h>
7 #include <linux/random.h>
8 #include <linux/slab.h>
10 #include <linux/ceph/decode.h>
11 #include <linux/ceph/auth.h>
12 #include <linux/ceph/libceph.h>
13 #include <linux/ceph/messenger.h>
17 #include "auth_x_protocol.h"
19 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
21 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
23 struct ceph_x_info *xi = ac->private;
26 ceph_x_validate_tickets(ac, &need);
27 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
28 ac->want_keys, need, xi->have_keys);
29 return (ac->want_keys & xi->have_keys) == ac->want_keys;
32 static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
34 struct ceph_x_info *xi = ac->private;
37 ceph_x_validate_tickets(ac, &need);
38 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
39 ac->want_keys, need, xi->have_keys);
43 static int ceph_x_encrypt_offset(void)
45 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
48 static int ceph_x_encrypt_buflen(int ilen)
50 return ceph_x_encrypt_offset() + ilen + 16;
53 static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
54 int buf_len, int plaintext_len)
56 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
61 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
63 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
64 plaintext_len + sizeof(struct ceph_x_encrypt_header),
69 ceph_encode_32(&buf, ciphertext_len);
70 return sizeof(u32) + ciphertext_len;
73 static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
75 struct ceph_x_encrypt_header *hdr = *p + sizeof(u32);
76 int ciphertext_len, plaintext_len;
79 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
80 ceph_decode_need(p, end, ciphertext_len, e_inval);
82 ret = ceph_crypt(secret, false, *p, end - *p, ciphertext_len,
87 if (hdr->struct_v != 1 || le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC)
91 return plaintext_len - sizeof(struct ceph_x_encrypt_header);
98 * get existing (or insert new) ticket handler
100 static struct ceph_x_ticket_handler *
101 get_ticket_handler(struct ceph_auth_client *ac, int service)
103 struct ceph_x_ticket_handler *th;
104 struct ceph_x_info *xi = ac->private;
105 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
109 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
110 if (service < th->service)
112 else if (service > th->service)
119 th = kzalloc(sizeof(*th), GFP_NOFS);
121 return ERR_PTR(-ENOMEM);
122 th->service = service;
123 rb_link_node(&th->node, parent, p);
124 rb_insert_color(&th->node, &xi->ticket_handlers);
128 static void remove_ticket_handler(struct ceph_auth_client *ac,
129 struct ceph_x_ticket_handler *th)
131 struct ceph_x_info *xi = ac->private;
133 dout("remove_ticket_handler %p %d\n", th, th->service);
134 rb_erase(&th->node, &xi->ticket_handlers);
135 ceph_crypto_key_destroy(&th->session_key);
137 ceph_buffer_put(th->ticket_blob);
141 static int process_one_ticket(struct ceph_auth_client *ac,
142 struct ceph_crypto_key *secret,
145 struct ceph_x_info *xi = ac->private;
147 u8 tkt_struct_v, blob_struct_v;
148 struct ceph_x_ticket_handler *th;
152 struct timespec validity;
155 struct ceph_crypto_key new_session_key = { 0 };
156 struct ceph_buffer *new_ticket_blob;
157 unsigned long new_expires, new_renew_after;
161 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
163 type = ceph_decode_32(p);
164 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
166 tkt_struct_v = ceph_decode_8(p);
167 if (tkt_struct_v != 1)
170 th = get_ticket_handler(ac, type);
177 dp = *p + ceph_x_encrypt_offset();
178 ret = ceph_x_decrypt(secret, p, end);
181 dout(" decrypted %d bytes\n", ret);
184 tkt_struct_v = ceph_decode_8(&dp);
185 if (tkt_struct_v != 1)
188 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
192 ceph_decode_timespec(&validity, dp);
193 dp += sizeof(struct ceph_timespec);
194 new_expires = get_seconds() + validity.tv_sec;
195 new_renew_after = new_expires - (validity.tv_sec / 4);
196 dout(" expires=%lu renew_after=%lu\n", new_expires,
199 /* ticket blob for service */
200 ceph_decode_8_safe(p, end, is_enc, bad);
203 tp = *p + ceph_x_encrypt_offset();
204 ret = ceph_x_decrypt(&th->session_key, p, end);
207 dout(" encrypted ticket, decrypted %d bytes\n", ret);
215 ceph_decode_32_safe(ptp, tpend, dlen, bad);
216 dout(" ticket blob is %d bytes\n", dlen);
217 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
218 blob_struct_v = ceph_decode_8(ptp);
219 if (blob_struct_v != 1)
222 new_secret_id = ceph_decode_64(ptp);
223 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
227 /* all is well, update our ticket */
228 ceph_crypto_key_destroy(&th->session_key);
230 ceph_buffer_put(th->ticket_blob);
231 th->session_key = new_session_key;
232 th->ticket_blob = new_ticket_blob;
233 th->secret_id = new_secret_id;
234 th->expires = new_expires;
235 th->renew_after = new_renew_after;
237 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
238 type, ceph_entity_type_name(type), th->secret_id,
239 (int)th->ticket_blob->vec.iov_len);
240 xi->have_keys |= th->service;
246 ceph_crypto_key_destroy(&new_session_key);
250 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
251 struct ceph_crypto_key *secret,
252 void *buf, void *end)
259 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
260 if (reply_struct_v != 1)
263 ceph_decode_32_safe(&p, end, num, bad);
264 dout("%d tickets\n", num);
267 ret = process_one_ticket(ac, secret, &p, end);
278 static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
280 ceph_crypto_key_destroy(&au->session_key);
282 ceph_buffer_put(au->buf);
287 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
288 struct ceph_x_ticket_handler *th,
289 struct ceph_x_authorizer *au)
292 struct ceph_x_authorize_a *msg_a;
293 struct ceph_x_authorize_b *msg_b;
296 int ticket_blob_len =
297 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
299 dout("build_authorizer for %s %p\n",
300 ceph_entity_type_name(th->service), au);
302 ceph_crypto_key_destroy(&au->session_key);
303 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
307 maxlen = sizeof(*msg_a) + ticket_blob_len +
308 ceph_x_encrypt_buflen(sizeof(*msg_b));
309 dout(" need len %d\n", maxlen);
310 if (au->buf && au->buf->alloc_len < maxlen) {
311 ceph_buffer_put(au->buf);
315 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
321 au->service = th->service;
322 au->secret_id = th->secret_id;
324 msg_a = au->buf->vec.iov_base;
326 msg_a->global_id = cpu_to_le64(ac->global_id);
327 msg_a->service_id = cpu_to_le32(th->service);
328 msg_a->ticket_blob.struct_v = 1;
329 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
330 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
331 if (ticket_blob_len) {
332 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
333 th->ticket_blob->vec.iov_len);
335 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
336 le64_to_cpu(msg_a->ticket_blob.secret_id));
339 p += ticket_blob_len;
340 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
342 msg_b = p + ceph_x_encrypt_offset();
344 get_random_bytes(&au->nonce, sizeof(au->nonce));
345 msg_b->nonce = cpu_to_le64(au->nonce);
346 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
352 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
353 dout(" built authorizer nonce %llx len %d\n", au->nonce,
354 (int)au->buf->vec.iov_len);
358 ceph_x_authorizer_cleanup(au);
362 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
365 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
367 ceph_encode_64(p, th->secret_id);
368 if (th->ticket_blob) {
369 const char *buf = th->ticket_blob->vec.iov_base;
370 u32 len = th->ticket_blob->vec.iov_len;
372 ceph_encode_32_safe(p, end, len, bad);
373 ceph_encode_copy_safe(p, end, buf, len, bad);
375 ceph_encode_32_safe(p, end, 0, bad);
383 static bool need_key(struct ceph_x_ticket_handler *th)
388 return get_seconds() >= th->renew_after;
391 static bool have_key(struct ceph_x_ticket_handler *th)
394 if (get_seconds() >= th->expires)
395 th->have_key = false;
401 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
403 int want = ac->want_keys;
404 struct ceph_x_info *xi = ac->private;
407 *pneed = ac->want_keys & ~(xi->have_keys);
409 for (service = 1; service <= want; service <<= 1) {
410 struct ceph_x_ticket_handler *th;
412 if (!(ac->want_keys & service))
415 if (*pneed & service)
418 th = get_ticket_handler(ac, service);
427 xi->have_keys &= ~service;
431 static int ceph_x_build_request(struct ceph_auth_client *ac,
432 void *buf, void *end)
434 struct ceph_x_info *xi = ac->private;
436 struct ceph_x_request_header *head = buf;
438 struct ceph_x_ticket_handler *th =
439 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
444 ceph_x_validate_tickets(ac, &need);
446 dout("build_request want %x have %x need %x\n",
447 ac->want_keys, xi->have_keys, need);
449 if (need & CEPH_ENTITY_TYPE_AUTH) {
450 struct ceph_x_authenticate *auth = (void *)(head + 1);
452 void *enc_buf = xi->auth_authorizer.enc_buf;
453 struct ceph_x_challenge_blob *blob = enc_buf +
454 ceph_x_encrypt_offset();
460 dout(" get_auth_session_key\n");
461 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
463 /* encrypt and hash */
464 get_random_bytes(&auth->client_challenge, sizeof(u64));
465 blob->client_challenge = auth->client_challenge;
466 blob->server_challenge = cpu_to_le64(xi->server_challenge);
467 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
474 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
475 auth->key ^= *(__le64 *)u;
476 dout(" server_challenge %llx client_challenge %llx key %llx\n",
477 xi->server_challenge, le64_to_cpu(auth->client_challenge),
478 le64_to_cpu(auth->key));
480 /* now encode the old ticket if exists */
481 ret = ceph_x_encode_ticket(th, &p, end);
490 struct ceph_x_service_ticket_request *req;
494 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
496 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
499 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
500 xi->auth_authorizer.buf->vec.iov_len);
503 req->keys = cpu_to_le32(need);
511 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
512 void *buf, void *end)
514 struct ceph_x_info *xi = ac->private;
515 struct ceph_x_reply_header *head = buf;
516 struct ceph_x_ticket_handler *th;
522 return result; /* XXX hmm? */
526 struct ceph_x_server_challenge *sc = buf;
528 if (len != sizeof(*sc))
530 xi->server_challenge = le64_to_cpu(sc->server_challenge);
531 dout("handle_reply got server challenge %llx\n",
532 xi->server_challenge);
533 xi->starting = false;
534 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
538 op = le16_to_cpu(head->op);
539 result = le32_to_cpu(head->result);
540 dout("handle_reply op %d result %d\n", op, result);
542 case CEPHX_GET_AUTH_SESSION_KEY:
543 /* verify auth key */
544 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
545 buf + sizeof(*head), end);
548 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
549 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
552 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
553 buf + sizeof(*head), end);
561 if (ac->want_keys == xi->have_keys)
566 static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
568 struct ceph_x_authorizer *au = (void *)a;
570 ceph_x_authorizer_cleanup(au);
574 static int ceph_x_create_authorizer(
575 struct ceph_auth_client *ac, int peer_type,
576 struct ceph_auth_handshake *auth)
578 struct ceph_x_authorizer *au;
579 struct ceph_x_ticket_handler *th;
582 th = get_ticket_handler(ac, peer_type);
586 au = kzalloc(sizeof(*au), GFP_NOFS);
590 au->base.destroy = ceph_x_destroy_authorizer;
592 ret = ceph_x_build_authorizer(ac, th, au);
598 auth->authorizer = (struct ceph_authorizer *) au;
599 auth->authorizer_buf = au->buf->vec.iov_base;
600 auth->authorizer_buf_len = au->buf->vec.iov_len;
601 auth->authorizer_reply_buf = au->enc_buf;
602 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
603 auth->sign_message = ac->ops->sign_message;
604 auth->check_message_signature = ac->ops->check_message_signature;
609 static int ceph_x_update_authorizer(
610 struct ceph_auth_client *ac, int peer_type,
611 struct ceph_auth_handshake *auth)
613 struct ceph_x_authorizer *au;
614 struct ceph_x_ticket_handler *th;
616 th = get_ticket_handler(ac, peer_type);
620 au = (struct ceph_x_authorizer *)auth->authorizer;
621 if (au->secret_id < th->secret_id) {
622 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
623 au->service, au->secret_id, th->secret_id);
624 return ceph_x_build_authorizer(ac, th, au);
629 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
630 struct ceph_authorizer *a)
632 struct ceph_x_authorizer *au = (void *)a;
633 void *p = au->enc_buf;
634 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
637 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
640 if (ret != sizeof(*reply))
643 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
647 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
648 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
652 static void ceph_x_reset(struct ceph_auth_client *ac)
654 struct ceph_x_info *xi = ac->private;
658 xi->server_challenge = 0;
661 static void ceph_x_destroy(struct ceph_auth_client *ac)
663 struct ceph_x_info *xi = ac->private;
666 dout("ceph_x_destroy %p\n", ac);
667 ceph_crypto_key_destroy(&xi->secret);
669 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
670 struct ceph_x_ticket_handler *th =
671 rb_entry(p, struct ceph_x_ticket_handler, node);
672 remove_ticket_handler(ac, th);
675 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
681 static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
683 struct ceph_x_ticket_handler *th;
685 th = get_ticket_handler(ac, peer_type);
687 th->have_key = false;
690 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
694 * We are to invalidate a service ticket in the hopes of
695 * getting a new, hopefully more valid, one. But, we won't get
696 * it unless our AUTH ticket is good, so invalidate AUTH ticket
697 * as well, just in case.
699 invalidate_ticket(ac, peer_type);
700 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
703 static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
706 void *enc_buf = au->enc_buf;
713 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
716 sigblock->len = cpu_to_le32(4*sizeof(u32));
717 sigblock->header_crc = msg->hdr.crc;
718 sigblock->front_crc = msg->footer.front_crc;
719 sigblock->middle_crc = msg->footer.middle_crc;
720 sigblock->data_crc = msg->footer.data_crc;
721 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
726 *psig = *(__le64 *)(enc_buf + sizeof(u32));
730 static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
731 struct ceph_msg *msg)
736 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
739 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
744 msg->footer.sig = sig;
745 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
749 static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
750 struct ceph_msg *msg)
755 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
758 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
762 if (sig_check == msg->footer.sig)
764 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
765 dout("ceph_x_check_message_signature %p has signature %llx "
766 "expect %llx\n", msg, msg->footer.sig, sig_check);
768 dout("ceph_x_check_message_signature %p sender did not set "
769 "CEPH_MSG_FOOTER_SIGNED\n", msg);
773 static const struct ceph_auth_client_ops ceph_x_ops = {
775 .is_authenticated = ceph_x_is_authenticated,
776 .should_authenticate = ceph_x_should_authenticate,
777 .build_request = ceph_x_build_request,
778 .handle_reply = ceph_x_handle_reply,
779 .create_authorizer = ceph_x_create_authorizer,
780 .update_authorizer = ceph_x_update_authorizer,
781 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
782 .invalidate_authorizer = ceph_x_invalidate_authorizer,
783 .reset = ceph_x_reset,
784 .destroy = ceph_x_destroy,
785 .sign_message = ceph_x_sign_message,
786 .check_message_signature = ceph_x_check_message_signature,
790 int ceph_x_init(struct ceph_auth_client *ac)
792 struct ceph_x_info *xi;
795 dout("ceph_x_init %p\n", ac);
797 xi = kzalloc(sizeof(*xi), GFP_NOFS);
803 pr_err("no secret set (for auth_x protocol)\n");
807 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
809 pr_err("cannot clone key: %d\n", ret);
814 xi->ticket_handlers = RB_ROOT;
816 ac->protocol = CEPH_AUTH_CEPHX;
818 ac->ops = &ceph_x_ops;