blob: 33d3ad4dc456ef24ad9aa0876ff90794a340aa61 [file] [log] [blame]
Sage Weilec0994e2010-02-02 16:25:35 -08001
2#include "ceph_debug.h"
3
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
7
8#include "auth_x.h"
9#include "auth_x_protocol.h"
10#include "crypto.h"
11#include "auth.h"
12#include "decode.h"
13
14struct kmem_cache *ceph_x_ticketbuf_cachep;
15
16#define TEMP_TICKET_BUF_LEN 256
17
18static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19
20static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
21{
22 struct ceph_x_info *xi = ac->private;
23 int need;
24
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;
29}
30
Sage Weil807c86e2010-03-15 15:52:17 -070031static int ceph_x_encrypt_buflen(int ilen)
32{
33 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
34 sizeof(u32);
35}
36
Sage Weilec0994e2010-02-02 16:25:35 -080037static int ceph_x_encrypt(struct ceph_crypto_key *secret,
38 void *ibuf, int ilen, void *obuf, size_t olen)
39{
40 struct ceph_x_encrypt_header head = {
41 .struct_v = 1,
42 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
43 };
44 size_t len = olen - sizeof(u32);
45 int ret;
46
47 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
48 &head, sizeof(head), ibuf, ilen);
49 if (ret)
50 return ret;
51 ceph_encode_32(&obuf, len);
52 return len + sizeof(u32);
53}
54
55static int ceph_x_decrypt(struct ceph_crypto_key *secret,
56 void **p, void *end, void *obuf, size_t olen)
57{
58 struct ceph_x_encrypt_header head;
59 size_t head_len = sizeof(head);
60 int len, ret;
61
62 len = ceph_decode_32(p);
63 if (*p + len > end)
64 return -EINVAL;
65
66 dout("ceph_x_decrypt len %d\n", len);
67 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
68 *p, len);
69 if (ret)
70 return ret;
71 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
72 return -EPERM;
73 *p += len;
74 return olen;
75}
76
77/*
78 * get existing (or insert new) ticket handler
79 */
80struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
81 int service)
82{
83 struct ceph_x_ticket_handler *th;
84 struct ceph_x_info *xi = ac->private;
85 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
86
87 while (*p) {
88 parent = *p;
89 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
90 if (service < th->service)
91 p = &(*p)->rb_left;
92 else if (service > th->service)
93 p = &(*p)->rb_right;
94 else
95 return th;
96 }
97
98 /* add it */
99 th = kzalloc(sizeof(*th), GFP_NOFS);
100 if (!th)
101 return ERR_PTR(-ENOMEM);
102 th->service = service;
103 rb_link_node(&th->node, parent, p);
104 rb_insert_color(&th->node, &xi->ticket_handlers);
105 return th;
106}
107
108static void remove_ticket_handler(struct ceph_auth_client *ac,
109 struct ceph_x_ticket_handler *th)
110{
111 struct ceph_x_info *xi = ac->private;
112
113 dout("remove_ticket_handler %p %d\n", th, th->service);
114 rb_erase(&th->node, &xi->ticket_handlers);
115 ceph_crypto_key_destroy(&th->session_key);
116 if (th->ticket_blob)
117 ceph_buffer_put(th->ticket_blob);
118 kfree(th);
119}
120
121static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
122 struct ceph_crypto_key *secret,
123 void *buf, void *end)
124{
125 struct ceph_x_info *xi = ac->private;
126 int num;
127 void *p = buf;
128 int ret;
129 char *dbuf;
130 char *ticket_buf;
131 u8 struct_v;
132
133 dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
134 if (!dbuf)
135 return -ENOMEM;
136
137 ret = -ENOMEM;
138 ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
139 GFP_NOFS | GFP_ATOMIC);
140 if (!ticket_buf)
141 goto out_dbuf;
142
143 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
144 struct_v = ceph_decode_8(&p);
145 if (struct_v != 1)
146 goto bad;
147 num = ceph_decode_32(&p);
148 dout("%d tickets\n", num);
149 while (num--) {
150 int type;
151 u8 struct_v;
152 struct ceph_x_ticket_handler *th;
153 void *dp, *dend;
154 int dlen;
155 char is_enc;
156 struct timespec validity;
157 struct ceph_crypto_key old_key;
158 void *tp, *tpend;
Sage Weil5b3dbb42010-03-15 15:57:04 -0700159 struct ceph_buffer *new_ticket_blob;
Sage Weilec0994e2010-02-02 16:25:35 -0800160
161 ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
162
163 type = ceph_decode_32(&p);
164 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
165
166 struct_v = ceph_decode_8(&p);
167 if (struct_v != 1)
168 goto bad;
169
170 th = get_ticket_handler(ac, type);
171 if (IS_ERR(th)) {
172 ret = PTR_ERR(th);
173 goto out;
174 }
175
176 /* blob for me */
177 dlen = ceph_x_decrypt(secret, &p, end, dbuf,
178 TEMP_TICKET_BUF_LEN);
179 if (dlen <= 0) {
180 ret = dlen;
181 goto out;
182 }
183 dout(" decrypted %d bytes\n", dlen);
184 dend = dbuf + dlen;
185 dp = dbuf;
186
187 struct_v = ceph_decode_8(&dp);
188 if (struct_v != 1)
189 goto bad;
190
191 memcpy(&old_key, &th->session_key, sizeof(old_key));
192 ret = ceph_crypto_key_decode(&th->session_key, &dp, dend);
193 if (ret)
194 goto out;
195
196 ceph_decode_copy(&dp, &th->validity, sizeof(th->validity));
197 ceph_decode_timespec(&validity, &th->validity);
198 th->expires = get_seconds() + validity.tv_sec;
199 th->renew_after = th->expires - (validity.tv_sec / 4);
200 dout(" expires=%lu renew_after=%lu\n", th->expires,
201 th->renew_after);
202
203 /* ticket blob for service */
204 ceph_decode_8_safe(&p, end, is_enc, bad);
205 tp = ticket_buf;
206 if (is_enc) {
207 /* encrypted */
208 dout(" encrypted ticket\n");
209 dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
210 TEMP_TICKET_BUF_LEN);
211 if (dlen < 0) {
212 ret = dlen;
213 goto out;
214 }
215 dlen = ceph_decode_32(&tp);
216 } else {
217 /* unencrypted */
218 ceph_decode_32_safe(&p, end, dlen, bad);
219 ceph_decode_need(&p, end, dlen, bad);
220 ceph_decode_copy(&p, ticket_buf, dlen);
221 }
222 tpend = tp + dlen;
223 dout(" ticket blob is %d bytes\n", dlen);
224 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
225 struct_v = ceph_decode_8(&tp);
226 th->secret_id = ceph_decode_64(&tp);
Sage Weil5b3dbb42010-03-15 15:57:04 -0700227 ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
Sage Weilec0994e2010-02-02 16:25:35 -0800228 if (ret)
229 goto out;
Sage Weil5b3dbb42010-03-15 15:57:04 -0700230 if (th->ticket_blob)
231 ceph_buffer_put(th->ticket_blob);
232 th->ticket_blob = new_ticket_blob;
Sage Weilec0994e2010-02-02 16:25:35 -0800233 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
234 type, ceph_entity_type_name(type), th->secret_id,
235 (int)th->ticket_blob->vec.iov_len);
236 xi->have_keys |= th->service;
237 }
238
239 ret = 0;
240out:
241 kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
242out_dbuf:
243 kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
244 return ret;
245
246bad:
247 ret = -EINVAL;
248 goto out;
249}
250
251static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
252 struct ceph_x_ticket_handler *th,
253 struct ceph_x_authorizer *au)
254{
Sage Weil807c86e2010-03-15 15:52:17 -0700255 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800256 struct ceph_x_authorize_a *msg_a;
257 struct ceph_x_authorize_b msg_b;
258 void *p, *end;
259 int ret;
260 int ticket_blob_len =
261 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
262
263 dout("build_authorizer for %s %p\n",
264 ceph_entity_type_name(th->service), au);
265
Sage Weil807c86e2010-03-15 15:52:17 -0700266 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
267 ceph_x_encrypt_buflen(ticket_blob_len);
268 dout(" need len %d\n", maxlen);
269 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800270 ceph_buffer_put(au->buf);
271 au->buf = NULL;
272 }
273 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700274 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Sage Weilec0994e2010-02-02 16:25:35 -0800275 if (!au->buf)
276 return -ENOMEM;
277 }
278 au->service = th->service;
279
280 msg_a = au->buf->vec.iov_base;
281 msg_a->struct_v = 1;
282 msg_a->global_id = cpu_to_le64(ac->global_id);
283 msg_a->service_id = cpu_to_le32(th->service);
284 msg_a->ticket_blob.struct_v = 1;
285 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
286 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
287 if (ticket_blob_len) {
288 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
289 th->ticket_blob->vec.iov_len);
290 }
291 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
292 le64_to_cpu(msg_a->ticket_blob.secret_id));
293
294 p = msg_a + 1;
295 p += ticket_blob_len;
296 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
297
298 get_random_bytes(&au->nonce, sizeof(au->nonce));
299 msg_b.struct_v = 1;
300 msg_b.nonce = cpu_to_le64(au->nonce);
301 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
302 p, end - p);
303 if (ret < 0)
304 goto out_buf;
305 p += ret;
306 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
307 dout(" built authorizer nonce %llx len %d\n", au->nonce,
308 (int)au->buf->vec.iov_len);
Sage Weil807c86e2010-03-15 15:52:17 -0700309 BUG_ON(au->buf->vec.iov_len > maxlen);
Sage Weilec0994e2010-02-02 16:25:35 -0800310 return 0;
311
312out_buf:
313 ceph_buffer_put(au->buf);
314 au->buf = NULL;
315 return ret;
316}
317
318static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
319 void **p, void *end)
320{
321 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
322 ceph_encode_8(p, 1);
323 ceph_encode_64(p, th->secret_id);
324 if (th->ticket_blob) {
325 const char *buf = th->ticket_blob->vec.iov_base;
326 u32 len = th->ticket_blob->vec.iov_len;
327
328 ceph_encode_32_safe(p, end, len, bad);
329 ceph_encode_copy_safe(p, end, buf, len, bad);
330 } else {
331 ceph_encode_32_safe(p, end, 0, bad);
332 }
333
334 return 0;
335bad:
336 return -ERANGE;
337}
338
339static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
340{
341 int want = ac->want_keys;
342 struct ceph_x_info *xi = ac->private;
343 int service;
344
345 *pneed = ac->want_keys & ~(xi->have_keys);
346
347 for (service = 1; service <= want; service <<= 1) {
348 struct ceph_x_ticket_handler *th;
349
350 if (!(ac->want_keys & service))
351 continue;
352
353 if (*pneed & service)
354 continue;
355
356 th = get_ticket_handler(ac, service);
357
358 if (!th) {
359 *pneed |= service;
360 continue;
361 }
362
363 if (get_seconds() >= th->renew_after)
364 *pneed |= service;
365 if (get_seconds() >= th->expires)
366 xi->have_keys &= ~service;
367 }
368}
369
370
371static int ceph_x_build_request(struct ceph_auth_client *ac,
372 void *buf, void *end)
373{
374 struct ceph_x_info *xi = ac->private;
375 int need;
376 struct ceph_x_request_header *head = buf;
377 int ret;
378 struct ceph_x_ticket_handler *th =
379 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
380
381 ceph_x_validate_tickets(ac, &need);
382
383 dout("build_request want %x have %x need %x\n",
384 ac->want_keys, xi->have_keys, need);
385
386 if (need & CEPH_ENTITY_TYPE_AUTH) {
387 struct ceph_x_authenticate *auth = (void *)(head + 1);
388 void *p = auth + 1;
389 struct ceph_x_challenge_blob tmp;
390 char tmp_enc[40];
391 u64 *u;
392
393 if (p > end)
394 return -ERANGE;
395
396 dout(" get_auth_session_key\n");
397 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
398
399 /* encrypt and hash */
400 get_random_bytes(&auth->client_challenge, sizeof(u64));
401 tmp.client_challenge = auth->client_challenge;
402 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
403 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
404 tmp_enc, sizeof(tmp_enc));
405 if (ret < 0)
406 return ret;
407
408 auth->struct_v = 1;
409 auth->key = 0;
410 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
411 auth->key ^= *u;
412 dout(" server_challenge %llx client_challenge %llx key %llx\n",
413 xi->server_challenge, le64_to_cpu(auth->client_challenge),
414 le64_to_cpu(auth->key));
415
416 /* now encode the old ticket if exists */
417 ret = ceph_x_encode_ticket(th, &p, end);
418 if (ret < 0)
419 return ret;
420
421 return p - buf;
422 }
423
424 if (need) {
425 void *p = head + 1;
426 struct ceph_x_service_ticket_request *req;
427
428 if (p > end)
429 return -ERANGE;
430 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
431
432 BUG_ON(!th);
433 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
434 if (ret)
435 return ret;
436 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
437 xi->auth_authorizer.buf->vec.iov_len);
438
439 req = p;
440 req->keys = cpu_to_le32(need);
441 p += sizeof(*req);
442 return p - buf;
443 }
444
445 return 0;
446}
447
448static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
449 void *buf, void *end)
450{
451 struct ceph_x_info *xi = ac->private;
452 struct ceph_x_reply_header *head = buf;
453 struct ceph_x_ticket_handler *th;
454 int len = end - buf;
455 int op;
456 int ret;
457
458 if (result)
459 return result; /* XXX hmm? */
460
461 if (xi->starting) {
462 /* it's a hello */
463 struct ceph_x_server_challenge *sc = buf;
464
465 if (len != sizeof(*sc))
466 return -EINVAL;
467 xi->server_challenge = le64_to_cpu(sc->server_challenge);
468 dout("handle_reply got server challenge %llx\n",
469 xi->server_challenge);
470 xi->starting = false;
471 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
472 return -EAGAIN;
473 }
474
475 op = le32_to_cpu(head->op);
476 result = le32_to_cpu(head->result);
477 dout("handle_reply op %d result %d\n", op, result);
478 switch (op) {
479 case CEPHX_GET_AUTH_SESSION_KEY:
480 /* verify auth key */
481 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
482 buf + sizeof(*head), end);
483 break;
484
485 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
486 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
487 BUG_ON(!th);
488 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
489 buf + sizeof(*head), end);
490 break;
491
492 default:
493 return -EINVAL;
494 }
495 if (ret)
496 return ret;
497 if (ac->want_keys == xi->have_keys)
498 return 0;
499 return -EAGAIN;
500}
501
502static int ceph_x_create_authorizer(
503 struct ceph_auth_client *ac, int peer_type,
504 struct ceph_authorizer **a,
505 void **buf, size_t *len,
506 void **reply_buf, size_t *reply_len)
507{
508 struct ceph_x_authorizer *au;
509 struct ceph_x_ticket_handler *th;
510 int ret;
511
512 th = get_ticket_handler(ac, peer_type);
513 if (IS_ERR(th))
514 return PTR_ERR(th);
515
516 au = kzalloc(sizeof(*au), GFP_NOFS);
517 if (!au)
518 return -ENOMEM;
519
520 ret = ceph_x_build_authorizer(ac, th, au);
521 if (ret) {
522 kfree(au);
523 return ret;
524 }
525
526 *a = (struct ceph_authorizer *)au;
527 *buf = au->buf->vec.iov_base;
528 *len = au->buf->vec.iov_len;
529 *reply_buf = au->reply_buf;
530 *reply_len = sizeof(au->reply_buf);
531 return 0;
532}
533
534static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
535 struct ceph_authorizer *a, size_t len)
536{
537 struct ceph_x_authorizer *au = (void *)a;
538 struct ceph_x_ticket_handler *th;
539 int ret = 0;
540 struct ceph_x_authorize_reply reply;
541 void *p = au->reply_buf;
542 void *end = p + sizeof(au->reply_buf);
543
544 th = get_ticket_handler(ac, au->service);
545 if (!th)
546 return -EIO; /* hrm! */
547 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
548 if (ret < 0)
549 return ret;
550 if (ret != sizeof(reply))
551 return -EPERM;
552
553 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
554 ret = -EPERM;
555 else
556 ret = 0;
557 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
558 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
559 return ret;
560}
561
562static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
563 struct ceph_authorizer *a)
564{
565 struct ceph_x_authorizer *au = (void *)a;
566
567 ceph_buffer_put(au->buf);
568 kfree(au);
569}
570
571
572static void ceph_x_reset(struct ceph_auth_client *ac)
573{
574 struct ceph_x_info *xi = ac->private;
575
576 dout("reset\n");
577 xi->starting = true;
578 xi->server_challenge = 0;
579}
580
581static void ceph_x_destroy(struct ceph_auth_client *ac)
582{
583 struct ceph_x_info *xi = ac->private;
584 struct rb_node *p;
585
586 dout("ceph_x_destroy %p\n", ac);
587 ceph_crypto_key_destroy(&xi->secret);
588
589 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
590 struct ceph_x_ticket_handler *th =
591 rb_entry(p, struct ceph_x_ticket_handler, node);
592 remove_ticket_handler(ac, th);
593 }
594
595 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
596
597 kfree(ac->private);
598 ac->private = NULL;
599}
600
601static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
602 int peer_type)
603{
604 struct ceph_x_ticket_handler *th;
605
606 th = get_ticket_handler(ac, peer_type);
607 if (th && !IS_ERR(th))
608 remove_ticket_handler(ac, th);
609}
610
611
612static const struct ceph_auth_client_ops ceph_x_ops = {
613 .is_authenticated = ceph_x_is_authenticated,
614 .build_request = ceph_x_build_request,
615 .handle_reply = ceph_x_handle_reply,
616 .create_authorizer = ceph_x_create_authorizer,
617 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
618 .destroy_authorizer = ceph_x_destroy_authorizer,
619 .invalidate_authorizer = ceph_x_invalidate_authorizer,
620 .reset = ceph_x_reset,
621 .destroy = ceph_x_destroy,
622};
623
624
625int ceph_x_init(struct ceph_auth_client *ac)
626{
627 struct ceph_x_info *xi;
628 int ret;
629
630 dout("ceph_x_init %p\n", ac);
631 xi = kzalloc(sizeof(*xi), GFP_NOFS);
632 if (!xi)
633 return -ENOMEM;
634
635 ret = -ENOMEM;
636 ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
637 TEMP_TICKET_BUF_LEN, 8,
638 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
639 NULL);
640 if (!ceph_x_ticketbuf_cachep)
641 goto done_nomem;
642 ret = -EINVAL;
643 if (!ac->secret) {
644 pr_err("no secret set (for auth_x protocol)\n");
645 goto done_nomem;
646 }
647
648 ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
649 if (ret)
650 goto done_nomem;
651
652 xi->starting = true;
653 xi->ticket_handlers = RB_ROOT;
654
655 ac->protocol = CEPH_AUTH_CEPHX;
656 ac->private = xi;
657 ac->ops = &ceph_x_ops;
658 return 0;
659
660done_nomem:
661 kfree(xi);
662 if (ceph_x_ticketbuf_cachep)
663 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
664 return ret;
665}
666
667