2 #include "ceph_debug.h"
5 #include <linux/module.h>
6 #include <linux/random.h>
7 #include <linux/slab.h>
10 #include "auth_x_protocol.h"
15 #define TEMP_TICKET_BUF_LEN 256
17 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
21 struct ceph_x_info *xi = ac->private;
24 ceph_x_validate_tickets(ac, &need);
25 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
26 ac->want_keys, need, xi->have_keys);
27 return (ac->want_keys & xi->have_keys) == ac->want_keys;
30 static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
32 struct ceph_x_info *xi = ac->private;
35 ceph_x_validate_tickets(ac, &need);
36 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
37 ac->want_keys, need, xi->have_keys);
41 static int ceph_x_encrypt_buflen(int ilen)
43 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
47 static int ceph_x_encrypt(struct ceph_crypto_key *secret,
48 void *ibuf, int ilen, void *obuf, size_t olen)
50 struct ceph_x_encrypt_header head = {
52 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
54 size_t len = olen - sizeof(u32);
57 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
58 &head, sizeof(head), ibuf, ilen);
61 ceph_encode_32(&obuf, len);
62 return len + sizeof(u32);
65 static int ceph_x_decrypt(struct ceph_crypto_key *secret,
66 void **p, void *end, void *obuf, size_t olen)
68 struct ceph_x_encrypt_header head;
69 size_t head_len = sizeof(head);
72 len = ceph_decode_32(p);
76 dout("ceph_x_decrypt len %d\n", len);
77 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
81 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
88 * get existing (or insert new) ticket handler
90 static struct ceph_x_ticket_handler *
91 get_ticket_handler(struct ceph_auth_client *ac, int service)
93 struct ceph_x_ticket_handler *th;
94 struct ceph_x_info *xi = ac->private;
95 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
99 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
100 if (service < th->service)
102 else if (service > th->service)
109 th = kzalloc(sizeof(*th), GFP_NOFS);
111 return ERR_PTR(-ENOMEM);
112 th->service = service;
113 rb_link_node(&th->node, parent, p);
114 rb_insert_color(&th->node, &xi->ticket_handlers);
118 static void remove_ticket_handler(struct ceph_auth_client *ac,
119 struct ceph_x_ticket_handler *th)
121 struct ceph_x_info *xi = ac->private;
123 dout("remove_ticket_handler %p %d\n", th, th->service);
124 rb_erase(&th->node, &xi->ticket_handlers);
125 ceph_crypto_key_destroy(&th->session_key);
127 ceph_buffer_put(th->ticket_blob);
131 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
132 struct ceph_crypto_key *secret,
133 void *buf, void *end)
135 struct ceph_x_info *xi = ac->private;
143 dbuf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
148 ticket_buf = kmalloc(TEMP_TICKET_BUF_LEN, GFP_NOFS);
152 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
153 reply_struct_v = ceph_decode_8(&p);
154 if (reply_struct_v != 1)
156 num = ceph_decode_32(&p);
157 dout("%d tickets\n", num);
160 u8 tkt_struct_v, blob_struct_v;
161 struct ceph_x_ticket_handler *th;
165 struct timespec validity;
166 struct ceph_crypto_key old_key;
168 struct ceph_timespec new_validity;
169 struct ceph_crypto_key new_session_key;
170 struct ceph_buffer *new_ticket_blob;
171 unsigned long new_expires, new_renew_after;
174 ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
176 type = ceph_decode_32(&p);
177 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
179 tkt_struct_v = ceph_decode_8(&p);
180 if (tkt_struct_v != 1)
183 th = get_ticket_handler(ac, type);
190 dlen = ceph_x_decrypt(secret, &p, end, dbuf,
191 TEMP_TICKET_BUF_LEN);
196 dout(" decrypted %d bytes\n", dlen);
200 tkt_struct_v = ceph_decode_8(&dp);
201 if (tkt_struct_v != 1)
204 memcpy(&old_key, &th->session_key, sizeof(old_key));
205 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
209 ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
210 ceph_decode_timespec(&validity, &new_validity);
211 new_expires = get_seconds() + validity.tv_sec;
212 new_renew_after = new_expires - (validity.tv_sec / 4);
213 dout(" expires=%lu renew_after=%lu\n", new_expires,
216 /* ticket blob for service */
217 ceph_decode_8_safe(&p, end, is_enc, bad);
221 dout(" encrypted ticket\n");
222 dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
223 TEMP_TICKET_BUF_LEN);
228 dlen = ceph_decode_32(&tp);
231 ceph_decode_32_safe(&p, end, dlen, bad);
232 ceph_decode_need(&p, end, dlen, bad);
233 ceph_decode_copy(&p, ticket_buf, dlen);
236 dout(" ticket blob is %d bytes\n", dlen);
237 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
238 blob_struct_v = ceph_decode_8(&tp);
239 new_secret_id = ceph_decode_64(&tp);
240 ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
244 /* all is well, update our ticket */
245 ceph_crypto_key_destroy(&th->session_key);
247 ceph_buffer_put(th->ticket_blob);
248 th->session_key = new_session_key;
249 th->ticket_blob = new_ticket_blob;
250 th->validity = new_validity;
251 th->secret_id = new_secret_id;
252 th->expires = new_expires;
253 th->renew_after = new_renew_after;
254 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
255 type, ceph_entity_type_name(type), th->secret_id,
256 (int)th->ticket_blob->vec.iov_len);
257 xi->have_keys |= th->service;
272 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
273 struct ceph_x_ticket_handler *th,
274 struct ceph_x_authorizer *au)
277 struct ceph_x_authorize_a *msg_a;
278 struct ceph_x_authorize_b msg_b;
281 int ticket_blob_len =
282 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
284 dout("build_authorizer for %s %p\n",
285 ceph_entity_type_name(th->service), au);
287 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
288 ceph_x_encrypt_buflen(ticket_blob_len);
289 dout(" need len %d\n", maxlen);
290 if (au->buf && au->buf->alloc_len < maxlen) {
291 ceph_buffer_put(au->buf);
295 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
299 au->service = th->service;
301 msg_a = au->buf->vec.iov_base;
303 msg_a->global_id = cpu_to_le64(ac->global_id);
304 msg_a->service_id = cpu_to_le32(th->service);
305 msg_a->ticket_blob.struct_v = 1;
306 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
307 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
308 if (ticket_blob_len) {
309 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
310 th->ticket_blob->vec.iov_len);
312 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
313 le64_to_cpu(msg_a->ticket_blob.secret_id));
316 p += ticket_blob_len;
317 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
319 get_random_bytes(&au->nonce, sizeof(au->nonce));
321 msg_b.nonce = cpu_to_le64(au->nonce);
322 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
327 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
328 dout(" built authorizer nonce %llx len %d\n", au->nonce,
329 (int)au->buf->vec.iov_len);
330 BUG_ON(au->buf->vec.iov_len > maxlen);
334 ceph_buffer_put(au->buf);
339 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
342 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
344 ceph_encode_64(p, th->secret_id);
345 if (th->ticket_blob) {
346 const char *buf = th->ticket_blob->vec.iov_base;
347 u32 len = th->ticket_blob->vec.iov_len;
349 ceph_encode_32_safe(p, end, len, bad);
350 ceph_encode_copy_safe(p, end, buf, len, bad);
352 ceph_encode_32_safe(p, end, 0, bad);
360 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
362 int want = ac->want_keys;
363 struct ceph_x_info *xi = ac->private;
366 *pneed = ac->want_keys & ~(xi->have_keys);
368 for (service = 1; service <= want; service <<= 1) {
369 struct ceph_x_ticket_handler *th;
371 if (!(ac->want_keys & service))
374 if (*pneed & service)
377 th = get_ticket_handler(ac, service);
384 if (get_seconds() >= th->renew_after)
386 if (get_seconds() >= th->expires)
387 xi->have_keys &= ~service;
392 static int ceph_x_build_request(struct ceph_auth_client *ac,
393 void *buf, void *end)
395 struct ceph_x_info *xi = ac->private;
397 struct ceph_x_request_header *head = buf;
399 struct ceph_x_ticket_handler *th =
400 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
405 ceph_x_validate_tickets(ac, &need);
407 dout("build_request want %x have %x need %x\n",
408 ac->want_keys, xi->have_keys, need);
410 if (need & CEPH_ENTITY_TYPE_AUTH) {
411 struct ceph_x_authenticate *auth = (void *)(head + 1);
413 struct ceph_x_challenge_blob tmp;
420 dout(" get_auth_session_key\n");
421 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
423 /* encrypt and hash */
424 get_random_bytes(&auth->client_challenge, sizeof(u64));
425 tmp.client_challenge = auth->client_challenge;
426 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
427 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
428 tmp_enc, sizeof(tmp_enc));
434 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
435 auth->key ^= *(__le64 *)u;
436 dout(" server_challenge %llx client_challenge %llx key %llx\n",
437 xi->server_challenge, le64_to_cpu(auth->client_challenge),
438 le64_to_cpu(auth->key));
440 /* now encode the old ticket if exists */
441 ret = ceph_x_encode_ticket(th, &p, end);
450 struct ceph_x_service_ticket_request *req;
454 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
456 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
459 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
460 xi->auth_authorizer.buf->vec.iov_len);
463 req->keys = cpu_to_le32(need);
471 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
472 void *buf, void *end)
474 struct ceph_x_info *xi = ac->private;
475 struct ceph_x_reply_header *head = buf;
476 struct ceph_x_ticket_handler *th;
482 return result; /* XXX hmm? */
486 struct ceph_x_server_challenge *sc = buf;
488 if (len != sizeof(*sc))
490 xi->server_challenge = le64_to_cpu(sc->server_challenge);
491 dout("handle_reply got server challenge %llx\n",
492 xi->server_challenge);
493 xi->starting = false;
494 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
498 op = le16_to_cpu(head->op);
499 result = le32_to_cpu(head->result);
500 dout("handle_reply op %d result %d\n", op, result);
502 case CEPHX_GET_AUTH_SESSION_KEY:
503 /* verify auth key */
504 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
505 buf + sizeof(*head), end);
508 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
509 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
512 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
513 buf + sizeof(*head), end);
521 if (ac->want_keys == xi->have_keys)
526 static int ceph_x_create_authorizer(
527 struct ceph_auth_client *ac, int peer_type,
528 struct ceph_authorizer **a,
529 void **buf, size_t *len,
530 void **reply_buf, size_t *reply_len)
532 struct ceph_x_authorizer *au;
533 struct ceph_x_ticket_handler *th;
536 th = get_ticket_handler(ac, peer_type);
540 au = kzalloc(sizeof(*au), GFP_NOFS);
544 ret = ceph_x_build_authorizer(ac, th, au);
550 *a = (struct ceph_authorizer *)au;
551 *buf = au->buf->vec.iov_base;
552 *len = au->buf->vec.iov_len;
553 *reply_buf = au->reply_buf;
554 *reply_len = sizeof(au->reply_buf);
558 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
559 struct ceph_authorizer *a, size_t len)
561 struct ceph_x_authorizer *au = (void *)a;
562 struct ceph_x_ticket_handler *th;
564 struct ceph_x_authorize_reply reply;
565 void *p = au->reply_buf;
566 void *end = p + sizeof(au->reply_buf);
568 th = get_ticket_handler(ac, au->service);
571 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
574 if (ret != sizeof(reply))
577 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
581 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
582 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
586 static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
587 struct ceph_authorizer *a)
589 struct ceph_x_authorizer *au = (void *)a;
591 ceph_buffer_put(au->buf);
596 static void ceph_x_reset(struct ceph_auth_client *ac)
598 struct ceph_x_info *xi = ac->private;
602 xi->server_challenge = 0;
605 static void ceph_x_destroy(struct ceph_auth_client *ac)
607 struct ceph_x_info *xi = ac->private;
610 dout("ceph_x_destroy %p\n", ac);
611 ceph_crypto_key_destroy(&xi->secret);
613 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
614 struct ceph_x_ticket_handler *th =
615 rb_entry(p, struct ceph_x_ticket_handler, node);
616 remove_ticket_handler(ac, th);
619 if (xi->auth_authorizer.buf)
620 ceph_buffer_put(xi->auth_authorizer.buf);
626 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
629 struct ceph_x_ticket_handler *th;
631 th = get_ticket_handler(ac, peer_type);
633 remove_ticket_handler(ac, th);
637 static const struct ceph_auth_client_ops ceph_x_ops = {
639 .is_authenticated = ceph_x_is_authenticated,
640 .should_authenticate = ceph_x_should_authenticate,
641 .build_request = ceph_x_build_request,
642 .handle_reply = ceph_x_handle_reply,
643 .create_authorizer = ceph_x_create_authorizer,
644 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
645 .destroy_authorizer = ceph_x_destroy_authorizer,
646 .invalidate_authorizer = ceph_x_invalidate_authorizer,
647 .reset = ceph_x_reset,
648 .destroy = ceph_x_destroy,
652 int ceph_x_init(struct ceph_auth_client *ac)
654 struct ceph_x_info *xi;
657 dout("ceph_x_init %p\n", ac);
659 xi = kzalloc(sizeof(*xi), GFP_NOFS);
665 pr_err("no secret set (for auth_x protocol)\n");
669 ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
674 xi->ticket_handlers = RB_ROOT;
676 ac->protocol = CEPH_AUTH_CEPHX;
678 ac->ops = &ceph_x_ops;