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(struct ceph_crypto_key *secret,
32 void *ibuf, int ilen, void *obuf, size_t olen)
34 struct ceph_x_encrypt_header head = {
36 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
38 size_t len = olen - sizeof(u32);
41 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
42 &head, sizeof(head), ibuf, ilen);
45 ceph_encode_32(&obuf, len);
46 return len + sizeof(u32);
49 static int ceph_x_decrypt(struct ceph_crypto_key *secret,
50 void **p, void *end, void *obuf, size_t olen)
52 struct ceph_x_encrypt_header head;
53 size_t head_len = sizeof(head);
56 len = ceph_decode_32(p);
60 dout("ceph_x_decrypt len %d\n", len);
61 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
65 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
72 * get existing (or insert new) ticket handler
74 struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
77 struct ceph_x_ticket_handler *th;
78 struct ceph_x_info *xi = ac->private;
79 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
83 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
84 if (service < th->service)
86 else if (service > th->service)
93 th = kzalloc(sizeof(*th), GFP_NOFS);
95 return ERR_PTR(-ENOMEM);
96 th->service = service;
97 rb_link_node(&th->node, parent, p);
98 rb_insert_color(&th->node, &xi->ticket_handlers);
102 static void remove_ticket_handler(struct ceph_auth_client *ac,
103 struct ceph_x_ticket_handler *th)
105 struct ceph_x_info *xi = ac->private;
107 dout("remove_ticket_handler %p %d\n", th, th->service);
108 rb_erase(&th->node, &xi->ticket_handlers);
109 ceph_crypto_key_destroy(&th->session_key);
111 ceph_buffer_put(th->ticket_blob);
115 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
116 struct ceph_crypto_key *secret,
117 void *buf, void *end)
119 struct ceph_x_info *xi = ac->private;
127 dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
132 ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
133 GFP_NOFS | GFP_ATOMIC);
137 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
138 struct_v = ceph_decode_8(&p);
141 num = ceph_decode_32(&p);
142 dout("%d tickets\n", num);
146 struct ceph_x_ticket_handler *th;
150 struct timespec validity;
151 struct ceph_crypto_key old_key;
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 struct_v = ceph_decode_8(&p);
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 struct_v = ceph_decode_8(&dp);
184 memcpy(&old_key, &th->session_key, sizeof(old_key));
185 ret = ceph_crypto_key_decode(&th->session_key, &dp, dend);
189 ceph_decode_copy(&dp, &th->validity, sizeof(th->validity));
190 ceph_decode_timespec(&validity, &th->validity);
191 th->expires = get_seconds() + validity.tv_sec;
192 th->renew_after = th->expires - (validity.tv_sec / 4);
193 dout(" expires=%lu renew_after=%lu\n", th->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 struct_v = ceph_decode_8(&tp);
219 th->secret_id = ceph_decode_64(&tp);
220 ret = ceph_decode_buffer(&th->ticket_blob, &tp, tpend);
223 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
224 type, ceph_entity_type_name(type), th->secret_id,
225 (int)th->ticket_blob->vec.iov_len);
226 xi->have_keys |= th->service;
231 kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
233 kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
241 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
242 struct ceph_x_ticket_handler *th,
243 struct ceph_x_authorizer *au)
246 struct ceph_x_authorize_a *msg_a;
247 struct ceph_x_authorize_b msg_b;
250 int ticket_blob_len =
251 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
253 dout("build_authorizer for %s %p\n",
254 ceph_entity_type_name(th->service), au);
256 len = sizeof(*msg_a) + sizeof(msg_b) + sizeof(u32) +
257 ticket_blob_len + 16;
258 dout(" need len %d\n", len);
259 if (au->buf && au->buf->alloc_len < len) {
260 ceph_buffer_put(au->buf);
264 au->buf = ceph_buffer_new(len, GFP_NOFS);
268 au->service = th->service;
270 msg_a = au->buf->vec.iov_base;
272 msg_a->global_id = cpu_to_le64(ac->global_id);
273 msg_a->service_id = cpu_to_le32(th->service);
274 msg_a->ticket_blob.struct_v = 1;
275 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
276 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
277 if (ticket_blob_len) {
278 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
279 th->ticket_blob->vec.iov_len);
281 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
282 le64_to_cpu(msg_a->ticket_blob.secret_id));
285 p += ticket_blob_len;
286 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
288 get_random_bytes(&au->nonce, sizeof(au->nonce));
290 msg_b.nonce = cpu_to_le64(au->nonce);
291 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
296 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
297 dout(" built authorizer nonce %llx len %d\n", au->nonce,
298 (int)au->buf->vec.iov_len);
302 ceph_buffer_put(au->buf);
307 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
310 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
312 ceph_encode_64(p, th->secret_id);
313 if (th->ticket_blob) {
314 const char *buf = th->ticket_blob->vec.iov_base;
315 u32 len = th->ticket_blob->vec.iov_len;
317 ceph_encode_32_safe(p, end, len, bad);
318 ceph_encode_copy_safe(p, end, buf, len, bad);
320 ceph_encode_32_safe(p, end, 0, bad);
328 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
330 int want = ac->want_keys;
331 struct ceph_x_info *xi = ac->private;
334 *pneed = ac->want_keys & ~(xi->have_keys);
336 for (service = 1; service <= want; service <<= 1) {
337 struct ceph_x_ticket_handler *th;
339 if (!(ac->want_keys & service))
342 if (*pneed & service)
345 th = get_ticket_handler(ac, service);
352 if (get_seconds() >= th->renew_after)
354 if (get_seconds() >= th->expires)
355 xi->have_keys &= ~service;
360 static int ceph_x_build_request(struct ceph_auth_client *ac,
361 void *buf, void *end)
363 struct ceph_x_info *xi = ac->private;
365 struct ceph_x_request_header *head = buf;
367 struct ceph_x_ticket_handler *th =
368 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
370 ceph_x_validate_tickets(ac, &need);
372 dout("build_request want %x have %x need %x\n",
373 ac->want_keys, xi->have_keys, need);
375 if (need & CEPH_ENTITY_TYPE_AUTH) {
376 struct ceph_x_authenticate *auth = (void *)(head + 1);
378 struct ceph_x_challenge_blob tmp;
385 dout(" get_auth_session_key\n");
386 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
388 /* encrypt and hash */
389 get_random_bytes(&auth->client_challenge, sizeof(u64));
390 tmp.client_challenge = auth->client_challenge;
391 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
392 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
393 tmp_enc, sizeof(tmp_enc));
399 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
401 dout(" server_challenge %llx client_challenge %llx key %llx\n",
402 xi->server_challenge, le64_to_cpu(auth->client_challenge),
403 le64_to_cpu(auth->key));
405 /* now encode the old ticket if exists */
406 ret = ceph_x_encode_ticket(th, &p, end);
415 struct ceph_x_service_ticket_request *req;
419 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
422 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
425 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
426 xi->auth_authorizer.buf->vec.iov_len);
429 req->keys = cpu_to_le32(need);
437 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
438 void *buf, void *end)
440 struct ceph_x_info *xi = ac->private;
441 struct ceph_x_reply_header *head = buf;
442 struct ceph_x_ticket_handler *th;
448 return result; /* XXX hmm? */
452 struct ceph_x_server_challenge *sc = buf;
454 if (len != sizeof(*sc))
456 xi->server_challenge = le64_to_cpu(sc->server_challenge);
457 dout("handle_reply got server challenge %llx\n",
458 xi->server_challenge);
459 xi->starting = false;
460 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
464 op = le32_to_cpu(head->op);
465 result = le32_to_cpu(head->result);
466 dout("handle_reply op %d result %d\n", op, result);
468 case CEPHX_GET_AUTH_SESSION_KEY:
469 /* verify auth key */
470 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
471 buf + sizeof(*head), end);
474 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
475 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
477 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
478 buf + sizeof(*head), end);
486 if (ac->want_keys == xi->have_keys)
491 static int ceph_x_create_authorizer(
492 struct ceph_auth_client *ac, int peer_type,
493 struct ceph_authorizer **a,
494 void **buf, size_t *len,
495 void **reply_buf, size_t *reply_len)
497 struct ceph_x_authorizer *au;
498 struct ceph_x_ticket_handler *th;
501 th = get_ticket_handler(ac, peer_type);
505 au = kzalloc(sizeof(*au), GFP_NOFS);
509 ret = ceph_x_build_authorizer(ac, th, au);
515 *a = (struct ceph_authorizer *)au;
516 *buf = au->buf->vec.iov_base;
517 *len = au->buf->vec.iov_len;
518 *reply_buf = au->reply_buf;
519 *reply_len = sizeof(au->reply_buf);
523 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
524 struct ceph_authorizer *a, size_t len)
526 struct ceph_x_authorizer *au = (void *)a;
527 struct ceph_x_ticket_handler *th;
529 struct ceph_x_authorize_reply reply;
530 void *p = au->reply_buf;
531 void *end = p + sizeof(au->reply_buf);
533 th = get_ticket_handler(ac, au->service);
535 return -EIO; /* hrm! */
536 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
539 if (ret != sizeof(reply))
542 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
546 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
547 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
551 static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
552 struct ceph_authorizer *a)
554 struct ceph_x_authorizer *au = (void *)a;
556 ceph_buffer_put(au->buf);
561 static void ceph_x_reset(struct ceph_auth_client *ac)
563 struct ceph_x_info *xi = ac->private;
567 xi->server_challenge = 0;
570 static void ceph_x_destroy(struct ceph_auth_client *ac)
572 struct ceph_x_info *xi = ac->private;
575 dout("ceph_x_destroy %p\n", ac);
576 ceph_crypto_key_destroy(&xi->secret);
578 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
579 struct ceph_x_ticket_handler *th =
580 rb_entry(p, struct ceph_x_ticket_handler, node);
581 remove_ticket_handler(ac, th);
584 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
590 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
593 struct ceph_x_ticket_handler *th;
595 th = get_ticket_handler(ac, peer_type);
596 if (th && !IS_ERR(th))
597 remove_ticket_handler(ac, th);
601 static const struct ceph_auth_client_ops ceph_x_ops = {
602 .is_authenticated = ceph_x_is_authenticated,
603 .build_request = ceph_x_build_request,
604 .handle_reply = ceph_x_handle_reply,
605 .create_authorizer = ceph_x_create_authorizer,
606 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
607 .destroy_authorizer = ceph_x_destroy_authorizer,
608 .invalidate_authorizer = ceph_x_invalidate_authorizer,
609 .reset = ceph_x_reset,
610 .destroy = ceph_x_destroy,
614 int ceph_x_init(struct ceph_auth_client *ac)
616 struct ceph_x_info *xi;
619 dout("ceph_x_init %p\n", ac);
620 xi = kzalloc(sizeof(*xi), GFP_NOFS);
625 ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
626 TEMP_TICKET_BUF_LEN, 8,
627 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
629 if (!ceph_x_ticketbuf_cachep)
633 pr_err("no secret set (for auth_x protocol)\n");
637 ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
642 xi->ticket_handlers = RB_ROOT;
644 ac->protocol = CEPH_AUTH_CEPHX;
646 ac->ops = &ceph_x_ops;
651 if (ceph_x_ticketbuf_cachep)
652 kmem_cache_destroy(ceph_x_ticketbuf_cachep);