Loading...
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/ceph/ceph_debug.h>
4
5#include <linux/err.h>
6#include <linux/module.h>
7#include <linux/random.h>
8#include <linux/slab.h>
9
10#include <linux/ceph/decode.h>
11#include <linux/ceph/auth.h>
12#include <linux/ceph/libceph.h>
13#include <linux/ceph/messenger.h>
14
15#include "crypto.h"
16#include "auth_x.h"
17#include "auth_x_protocol.h"
18
19static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
20
21static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
22{
23 struct ceph_x_info *xi = ac->private;
24 int need;
25
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;
30}
31
32static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
33{
34 struct ceph_x_info *xi = ac->private;
35 int need;
36
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);
40 return need != 0;
41}
42
43static int ceph_x_encrypt_offset(void)
44{
45 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
46}
47
48static int ceph_x_encrypt_buflen(int ilen)
49{
50 return ceph_x_encrypt_offset() + ilen + 16;
51}
52
53static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
54 int buf_len, int plaintext_len)
55{
56 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
57 int ciphertext_len;
58 int ret;
59
60 hdr->struct_v = 1;
61 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
62
63 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
64 plaintext_len + sizeof(struct ceph_x_encrypt_header),
65 &ciphertext_len);
66 if (ret)
67 return ret;
68
69 ceph_encode_32(&buf, ciphertext_len);
70 return sizeof(u32) + ciphertext_len;
71}
72
73static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
74{
75 struct ceph_x_encrypt_header *hdr = *p + sizeof(u32);
76 int ciphertext_len, plaintext_len;
77 int ret;
78
79 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
80 ceph_decode_need(p, end, ciphertext_len, e_inval);
81
82 ret = ceph_crypt(secret, false, *p, end - *p, ciphertext_len,
83 &plaintext_len);
84 if (ret)
85 return ret;
86
87 if (hdr->struct_v != 1 || le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC)
88 return -EPERM;
89
90 *p += ciphertext_len;
91 return plaintext_len - sizeof(struct ceph_x_encrypt_header);
92
93e_inval:
94 return -EINVAL;
95}
96
97/*
98 * get existing (or insert new) ticket handler
99 */
100static struct ceph_x_ticket_handler *
101get_ticket_handler(struct ceph_auth_client *ac, int service)
102{
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;
106
107 while (*p) {
108 parent = *p;
109 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
110 if (service < th->service)
111 p = &(*p)->rb_left;
112 else if (service > th->service)
113 p = &(*p)->rb_right;
114 else
115 return th;
116 }
117
118 /* add it */
119 th = kzalloc(sizeof(*th), GFP_NOFS);
120 if (!th)
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);
125 return th;
126}
127
128static void remove_ticket_handler(struct ceph_auth_client *ac,
129 struct ceph_x_ticket_handler *th)
130{
131 struct ceph_x_info *xi = ac->private;
132
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);
136 if (th->ticket_blob)
137 ceph_buffer_put(th->ticket_blob);
138 kfree(th);
139}
140
141static int process_one_ticket(struct ceph_auth_client *ac,
142 struct ceph_crypto_key *secret,
143 void **p, void *end)
144{
145 struct ceph_x_info *xi = ac->private;
146 int type;
147 u8 tkt_struct_v, blob_struct_v;
148 struct ceph_x_ticket_handler *th;
149 void *dp, *dend;
150 int dlen;
151 char is_enc;
152 struct timespec validity;
153 void *tp, *tpend;
154 void **ptp;
155 struct ceph_crypto_key new_session_key = { 0 };
156 struct ceph_buffer *new_ticket_blob;
157 unsigned long new_expires, new_renew_after;
158 u64 new_secret_id;
159 int ret;
160
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 tkt_struct_v = ceph_decode_8(p);
167 if (tkt_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 dp = *p + ceph_x_encrypt_offset();
178 ret = ceph_x_decrypt(secret, p, end);
179 if (ret < 0)
180 goto out;
181 dout(" decrypted %d bytes\n", ret);
182 dend = dp + ret;
183
184 tkt_struct_v = ceph_decode_8(&dp);
185 if (tkt_struct_v != 1)
186 goto bad;
187
188 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
189 if (ret)
190 goto out;
191
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,
197 new_renew_after);
198
199 /* ticket blob for service */
200 ceph_decode_8_safe(p, end, is_enc, bad);
201 if (is_enc) {
202 /* encrypted */
203 tp = *p + ceph_x_encrypt_offset();
204 ret = ceph_x_decrypt(&th->session_key, p, end);
205 if (ret < 0)
206 goto out;
207 dout(" encrypted ticket, decrypted %d bytes\n", ret);
208 ptp = &tp;
209 tpend = tp + ret;
210 } else {
211 /* unencrypted */
212 ptp = p;
213 tpend = end;
214 }
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)
220 goto bad;
221
222 new_secret_id = ceph_decode_64(ptp);
223 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
224 if (ret)
225 goto out;
226
227 /* all is well, update our ticket */
228 ceph_crypto_key_destroy(&th->session_key);
229 if (th->ticket_blob)
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;
236 th->have_key = true;
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;
241 return 0;
242
243bad:
244 ret = -EINVAL;
245out:
246 ceph_crypto_key_destroy(&new_session_key);
247 return ret;
248}
249
250static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
251 struct ceph_crypto_key *secret,
252 void *buf, void *end)
253{
254 void *p = buf;
255 u8 reply_struct_v;
256 u32 num;
257 int ret;
258
259 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
260 if (reply_struct_v != 1)
261 return -EINVAL;
262
263 ceph_decode_32_safe(&p, end, num, bad);
264 dout("%d tickets\n", num);
265
266 while (num--) {
267 ret = process_one_ticket(ac, secret, &p, end);
268 if (ret)
269 return ret;
270 }
271
272 return 0;
273
274bad:
275 return -EINVAL;
276}
277
278static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
279{
280 ceph_crypto_key_destroy(&au->session_key);
281 if (au->buf) {
282 ceph_buffer_put(au->buf);
283 au->buf = NULL;
284 }
285}
286
287static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
288 struct ceph_x_ticket_handler *th,
289 struct ceph_x_authorizer *au)
290{
291 int maxlen;
292 struct ceph_x_authorize_a *msg_a;
293 struct ceph_x_authorize_b *msg_b;
294 void *p, *end;
295 int ret;
296 int ticket_blob_len =
297 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
298
299 dout("build_authorizer for %s %p\n",
300 ceph_entity_type_name(th->service), au);
301
302 ceph_crypto_key_destroy(&au->session_key);
303 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
304 if (ret)
305 goto out_au;
306
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);
312 au->buf = NULL;
313 }
314 if (!au->buf) {
315 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
316 if (!au->buf) {
317 ret = -ENOMEM;
318 goto out_au;
319 }
320 }
321 au->service = th->service;
322 au->secret_id = th->secret_id;
323
324 msg_a = au->buf->vec.iov_base;
325 msg_a->struct_v = 1;
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);
334 }
335 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
336 le64_to_cpu(msg_a->ticket_blob.secret_id));
337
338 p = msg_a + 1;
339 p += ticket_blob_len;
340 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
341
342 msg_b = p + ceph_x_encrypt_offset();
343 msg_b->struct_v = 1;
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));
347 if (ret < 0)
348 goto out_au;
349
350 p += ret;
351 WARN_ON(p > end);
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);
355 return 0;
356
357out_au:
358 ceph_x_authorizer_cleanup(au);
359 return ret;
360}
361
362static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
363 void **p, void *end)
364{
365 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
366 ceph_encode_8(p, 1);
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;
371
372 ceph_encode_32_safe(p, end, len, bad);
373 ceph_encode_copy_safe(p, end, buf, len, bad);
374 } else {
375 ceph_encode_32_safe(p, end, 0, bad);
376 }
377
378 return 0;
379bad:
380 return -ERANGE;
381}
382
383static bool need_key(struct ceph_x_ticket_handler *th)
384{
385 if (!th->have_key)
386 return true;
387
388 return get_seconds() >= th->renew_after;
389}
390
391static bool have_key(struct ceph_x_ticket_handler *th)
392{
393 if (th->have_key) {
394 if (get_seconds() >= th->expires)
395 th->have_key = false;
396 }
397
398 return th->have_key;
399}
400
401static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
402{
403 int want = ac->want_keys;
404 struct ceph_x_info *xi = ac->private;
405 int service;
406
407 *pneed = ac->want_keys & ~(xi->have_keys);
408
409 for (service = 1; service <= want; service <<= 1) {
410 struct ceph_x_ticket_handler *th;
411
412 if (!(ac->want_keys & service))
413 continue;
414
415 if (*pneed & service)
416 continue;
417
418 th = get_ticket_handler(ac, service);
419 if (IS_ERR(th)) {
420 *pneed |= service;
421 continue;
422 }
423
424 if (need_key(th))
425 *pneed |= service;
426 if (!have_key(th))
427 xi->have_keys &= ~service;
428 }
429}
430
431static int ceph_x_build_request(struct ceph_auth_client *ac,
432 void *buf, void *end)
433{
434 struct ceph_x_info *xi = ac->private;
435 int need;
436 struct ceph_x_request_header *head = buf;
437 int ret;
438 struct ceph_x_ticket_handler *th =
439 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
440
441 if (IS_ERR(th))
442 return PTR_ERR(th);
443
444 ceph_x_validate_tickets(ac, &need);
445
446 dout("build_request want %x have %x need %x\n",
447 ac->want_keys, xi->have_keys, need);
448
449 if (need & CEPH_ENTITY_TYPE_AUTH) {
450 struct ceph_x_authenticate *auth = (void *)(head + 1);
451 void *p = auth + 1;
452 void *enc_buf = xi->auth_authorizer.enc_buf;
453 struct ceph_x_challenge_blob *blob = enc_buf +
454 ceph_x_encrypt_offset();
455 u64 *u;
456
457 if (p > end)
458 return -ERANGE;
459
460 dout(" get_auth_session_key\n");
461 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
462
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,
468 sizeof(*blob));
469 if (ret < 0)
470 return ret;
471
472 auth->struct_v = 1;
473 auth->key = 0;
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));
479
480 /* now encode the old ticket if exists */
481 ret = ceph_x_encode_ticket(th, &p, end);
482 if (ret < 0)
483 return ret;
484
485 return p - buf;
486 }
487
488 if (need) {
489 void *p = head + 1;
490 struct ceph_x_service_ticket_request *req;
491
492 if (p > end)
493 return -ERANGE;
494 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
495
496 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
497 if (ret)
498 return ret;
499 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
500 xi->auth_authorizer.buf->vec.iov_len);
501
502 req = p;
503 req->keys = cpu_to_le32(need);
504 p += sizeof(*req);
505 return p - buf;
506 }
507
508 return 0;
509}
510
511static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
512 void *buf, void *end)
513{
514 struct ceph_x_info *xi = ac->private;
515 struct ceph_x_reply_header *head = buf;
516 struct ceph_x_ticket_handler *th;
517 int len = end - buf;
518 int op;
519 int ret;
520
521 if (result)
522 return result; /* XXX hmm? */
523
524 if (xi->starting) {
525 /* it's a hello */
526 struct ceph_x_server_challenge *sc = buf;
527
528 if (len != sizeof(*sc))
529 return -EINVAL;
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;
535 return -EAGAIN;
536 }
537
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);
541 switch (op) {
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);
546 break;
547
548 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
549 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
550 if (IS_ERR(th))
551 return PTR_ERR(th);
552 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
553 buf + sizeof(*head), end);
554 break;
555
556 default:
557 return -EINVAL;
558 }
559 if (ret)
560 return ret;
561 if (ac->want_keys == xi->have_keys)
562 return 0;
563 return -EAGAIN;
564}
565
566static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
567{
568 struct ceph_x_authorizer *au = (void *)a;
569
570 ceph_x_authorizer_cleanup(au);
571 kfree(au);
572}
573
574static int ceph_x_create_authorizer(
575 struct ceph_auth_client *ac, int peer_type,
576 struct ceph_auth_handshake *auth)
577{
578 struct ceph_x_authorizer *au;
579 struct ceph_x_ticket_handler *th;
580 int ret;
581
582 th = get_ticket_handler(ac, peer_type);
583 if (IS_ERR(th))
584 return PTR_ERR(th);
585
586 au = kzalloc(sizeof(*au), GFP_NOFS);
587 if (!au)
588 return -ENOMEM;
589
590 au->base.destroy = ceph_x_destroy_authorizer;
591
592 ret = ceph_x_build_authorizer(ac, th, au);
593 if (ret) {
594 kfree(au);
595 return ret;
596 }
597
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;
605
606 return 0;
607}
608
609static int ceph_x_update_authorizer(
610 struct ceph_auth_client *ac, int peer_type,
611 struct ceph_auth_handshake *auth)
612{
613 struct ceph_x_authorizer *au;
614 struct ceph_x_ticket_handler *th;
615
616 th = get_ticket_handler(ac, peer_type);
617 if (IS_ERR(th))
618 return PTR_ERR(th);
619
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);
625 }
626 return 0;
627}
628
629static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
630 struct ceph_authorizer *a)
631{
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();
635 int ret;
636
637 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
638 if (ret < 0)
639 return ret;
640 if (ret != sizeof(*reply))
641 return -EPERM;
642
643 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
644 ret = -EPERM;
645 else
646 ret = 0;
647 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
648 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
649 return ret;
650}
651
652static void ceph_x_reset(struct ceph_auth_client *ac)
653{
654 struct ceph_x_info *xi = ac->private;
655
656 dout("reset\n");
657 xi->starting = true;
658 xi->server_challenge = 0;
659}
660
661static void ceph_x_destroy(struct ceph_auth_client *ac)
662{
663 struct ceph_x_info *xi = ac->private;
664 struct rb_node *p;
665
666 dout("ceph_x_destroy %p\n", ac);
667 ceph_crypto_key_destroy(&xi->secret);
668
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);
673 }
674
675 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
676
677 kfree(ac->private);
678 ac->private = NULL;
679}
680
681static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
682{
683 struct ceph_x_ticket_handler *th;
684
685 th = get_ticket_handler(ac, peer_type);
686 if (!IS_ERR(th))
687 th->have_key = false;
688}
689
690static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
691 int peer_type)
692{
693 /*
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.
698 */
699 invalidate_ticket(ac, peer_type);
700 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
701}
702
703static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
704 __le64 *psig)
705{
706 void *enc_buf = au->enc_buf;
707 struct {
708 __le32 len;
709 __le32 header_crc;
710 __le32 front_crc;
711 __le32 middle_crc;
712 __le32 data_crc;
713 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
714 int ret;
715
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,
722 sizeof(*sigblock));
723 if (ret < 0)
724 return ret;
725
726 *psig = *(__le64 *)(enc_buf + sizeof(u32));
727 return 0;
728}
729
730static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
731 struct ceph_msg *msg)
732{
733 __le64 sig;
734 int ret;
735
736 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
737 return 0;
738
739 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
740 msg, &sig);
741 if (ret)
742 return ret;
743
744 msg->footer.sig = sig;
745 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
746 return 0;
747}
748
749static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
750 struct ceph_msg *msg)
751{
752 __le64 sig_check;
753 int ret;
754
755 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
756 return 0;
757
758 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
759 msg, &sig_check);
760 if (ret)
761 return ret;
762 if (sig_check == msg->footer.sig)
763 return 0;
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);
767 else
768 dout("ceph_x_check_message_signature %p sender did not set "
769 "CEPH_MSG_FOOTER_SIGNED\n", msg);
770 return -EBADMSG;
771}
772
773static const struct ceph_auth_client_ops ceph_x_ops = {
774 .name = "x",
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,
787};
788
789
790int ceph_x_init(struct ceph_auth_client *ac)
791{
792 struct ceph_x_info *xi;
793 int ret;
794
795 dout("ceph_x_init %p\n", ac);
796 ret = -ENOMEM;
797 xi = kzalloc(sizeof(*xi), GFP_NOFS);
798 if (!xi)
799 goto out;
800
801 ret = -EINVAL;
802 if (!ac->key) {
803 pr_err("no secret set (for auth_x protocol)\n");
804 goto out_nomem;
805 }
806
807 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
808 if (ret < 0) {
809 pr_err("cannot clone key: %d\n", ret);
810 goto out_nomem;
811 }
812
813 xi->starting = true;
814 xi->ticket_handlers = RB_ROOT;
815
816 ac->protocol = CEPH_AUTH_CEPHX;
817 ac->private = xi;
818 ac->ops = &ceph_x_ops;
819 return 0;
820
821out_nomem:
822 kfree(xi);
823out:
824 return ret;
825}
826
827
1
2#include <linux/ceph/ceph_debug.h>
3
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
7#include <linux/slab.h>
8
9#include <linux/ceph/decode.h>
10#include <linux/ceph/auth.h>
11#include <linux/ceph/libceph.h>
12#include <linux/ceph/messenger.h>
13
14#include "crypto.h"
15#include "auth_x.h"
16#include "auth_x_protocol.h"
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
31static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
32{
33 struct ceph_x_info *xi = ac->private;
34 int need;
35
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);
39 return need != 0;
40}
41
42static int ceph_x_encrypt_offset(void)
43{
44 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
45}
46
47static int ceph_x_encrypt_buflen(int ilen)
48{
49 return ceph_x_encrypt_offset() + ilen + 16;
50}
51
52static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
53 int buf_len, int plaintext_len)
54{
55 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
56 int ciphertext_len;
57 int ret;
58
59 hdr->struct_v = 1;
60 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
61
62 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
63 plaintext_len + sizeof(struct ceph_x_encrypt_header),
64 &ciphertext_len);
65 if (ret)
66 return ret;
67
68 ceph_encode_32(&buf, ciphertext_len);
69 return sizeof(u32) + ciphertext_len;
70}
71
72static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
73{
74 struct ceph_x_encrypt_header *hdr = *p + sizeof(u32);
75 int ciphertext_len, plaintext_len;
76 int ret;
77
78 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
79 ceph_decode_need(p, end, ciphertext_len, e_inval);
80
81 ret = ceph_crypt(secret, false, *p, end - *p, ciphertext_len,
82 &plaintext_len);
83 if (ret)
84 return ret;
85
86 if (hdr->struct_v != 1 || le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC)
87 return -EPERM;
88
89 *p += ciphertext_len;
90 return plaintext_len - sizeof(struct ceph_x_encrypt_header);
91
92e_inval:
93 return -EINVAL;
94}
95
96/*
97 * get existing (or insert new) ticket handler
98 */
99static struct ceph_x_ticket_handler *
100get_ticket_handler(struct ceph_auth_client *ac, int service)
101{
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;
105
106 while (*p) {
107 parent = *p;
108 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
109 if (service < th->service)
110 p = &(*p)->rb_left;
111 else if (service > th->service)
112 p = &(*p)->rb_right;
113 else
114 return th;
115 }
116
117 /* add it */
118 th = kzalloc(sizeof(*th), GFP_NOFS);
119 if (!th)
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);
124 return th;
125}
126
127static void remove_ticket_handler(struct ceph_auth_client *ac,
128 struct ceph_x_ticket_handler *th)
129{
130 struct ceph_x_info *xi = ac->private;
131
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);
135 if (th->ticket_blob)
136 ceph_buffer_put(th->ticket_blob);
137 kfree(th);
138}
139
140static int process_one_ticket(struct ceph_auth_client *ac,
141 struct ceph_crypto_key *secret,
142 void **p, void *end)
143{
144 struct ceph_x_info *xi = ac->private;
145 int type;
146 u8 tkt_struct_v, blob_struct_v;
147 struct ceph_x_ticket_handler *th;
148 void *dp, *dend;
149 int dlen;
150 char is_enc;
151 struct timespec validity;
152 void *tp, *tpend;
153 void **ptp;
154 struct ceph_crypto_key new_session_key;
155 struct ceph_buffer *new_ticket_blob;
156 unsigned long new_expires, new_renew_after;
157 u64 new_secret_id;
158 int ret;
159
160 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
161
162 type = ceph_decode_32(p);
163 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
164
165 tkt_struct_v = ceph_decode_8(p);
166 if (tkt_struct_v != 1)
167 goto bad;
168
169 th = get_ticket_handler(ac, type);
170 if (IS_ERR(th)) {
171 ret = PTR_ERR(th);
172 goto out;
173 }
174
175 /* blob for me */
176 dp = *p + ceph_x_encrypt_offset();
177 ret = ceph_x_decrypt(secret, p, end);
178 if (ret < 0)
179 goto out;
180 dout(" decrypted %d bytes\n", ret);
181 dend = dp + ret;
182
183 tkt_struct_v = ceph_decode_8(&dp);
184 if (tkt_struct_v != 1)
185 goto bad;
186
187 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
188 if (ret)
189 goto out;
190
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,
196 new_renew_after);
197
198 /* ticket blob for service */
199 ceph_decode_8_safe(p, end, is_enc, bad);
200 if (is_enc) {
201 /* encrypted */
202 tp = *p + ceph_x_encrypt_offset();
203 ret = ceph_x_decrypt(&th->session_key, p, end);
204 if (ret < 0)
205 goto out;
206 dout(" encrypted ticket, decrypted %d bytes\n", ret);
207 ptp = &tp;
208 tpend = tp + ret;
209 } else {
210 /* unencrypted */
211 ptp = p;
212 tpend = end;
213 }
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 new_secret_id = ceph_decode_64(ptp);
219 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
220 if (ret)
221 goto out;
222
223 /* all is well, update our ticket */
224 ceph_crypto_key_destroy(&th->session_key);
225 if (th->ticket_blob)
226 ceph_buffer_put(th->ticket_blob);
227 th->session_key = new_session_key;
228 th->ticket_blob = new_ticket_blob;
229 th->secret_id = new_secret_id;
230 th->expires = new_expires;
231 th->renew_after = new_renew_after;
232 th->have_key = true;
233 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
238out:
239 return ret;
240
241bad:
242 ret = -EINVAL;
243 goto out;
244}
245
246static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
247 struct ceph_crypto_key *secret,
248 void *buf, void *end)
249{
250 void *p = buf;
251 u8 reply_struct_v;
252 u32 num;
253 int ret;
254
255 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
256 if (reply_struct_v != 1)
257 return -EINVAL;
258
259 ceph_decode_32_safe(&p, end, num, bad);
260 dout("%d tickets\n", num);
261
262 while (num--) {
263 ret = process_one_ticket(ac, secret, &p, end);
264 if (ret)
265 return ret;
266 }
267
268 return 0;
269
270bad:
271 return -EINVAL;
272}
273
274static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
275{
276 ceph_crypto_key_destroy(&au->session_key);
277 if (au->buf) {
278 ceph_buffer_put(au->buf);
279 au->buf = NULL;
280 }
281}
282
283static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
284 struct ceph_x_ticket_handler *th,
285 struct ceph_x_authorizer *au)
286{
287 int maxlen;
288 struct ceph_x_authorize_a *msg_a;
289 struct ceph_x_authorize_b *msg_b;
290 void *p, *end;
291 int ret;
292 int ticket_blob_len =
293 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
294
295 dout("build_authorizer for %s %p\n",
296 ceph_entity_type_name(th->service), au);
297
298 ceph_crypto_key_destroy(&au->session_key);
299 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
300 if (ret)
301 goto out_au;
302
303 maxlen = sizeof(*msg_a) + ticket_blob_len +
304 ceph_x_encrypt_buflen(sizeof(*msg_b));
305 dout(" need len %d\n", maxlen);
306 if (au->buf && au->buf->alloc_len < maxlen) {
307 ceph_buffer_put(au->buf);
308 au->buf = NULL;
309 }
310 if (!au->buf) {
311 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
312 if (!au->buf) {
313 ret = -ENOMEM;
314 goto out_au;
315 }
316 }
317 au->service = th->service;
318 au->secret_id = th->secret_id;
319
320 msg_a = au->buf->vec.iov_base;
321 msg_a->struct_v = 1;
322 msg_a->global_id = cpu_to_le64(ac->global_id);
323 msg_a->service_id = cpu_to_le32(th->service);
324 msg_a->ticket_blob.struct_v = 1;
325 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
326 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
327 if (ticket_blob_len) {
328 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
329 th->ticket_blob->vec.iov_len);
330 }
331 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
332 le64_to_cpu(msg_a->ticket_blob.secret_id));
333
334 p = msg_a + 1;
335 p += ticket_blob_len;
336 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
337
338 msg_b = p + ceph_x_encrypt_offset();
339 msg_b->struct_v = 1;
340 get_random_bytes(&au->nonce, sizeof(au->nonce));
341 msg_b->nonce = cpu_to_le64(au->nonce);
342 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
343 if (ret < 0)
344 goto out_au;
345
346 p += ret;
347 WARN_ON(p > end);
348 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
349 dout(" built authorizer nonce %llx len %d\n", au->nonce,
350 (int)au->buf->vec.iov_len);
351 return 0;
352
353out_au:
354 ceph_x_authorizer_cleanup(au);
355 return ret;
356}
357
358static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
359 void **p, void *end)
360{
361 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
362 ceph_encode_8(p, 1);
363 ceph_encode_64(p, th->secret_id);
364 if (th->ticket_blob) {
365 const char *buf = th->ticket_blob->vec.iov_base;
366 u32 len = th->ticket_blob->vec.iov_len;
367
368 ceph_encode_32_safe(p, end, len, bad);
369 ceph_encode_copy_safe(p, end, buf, len, bad);
370 } else {
371 ceph_encode_32_safe(p, end, 0, bad);
372 }
373
374 return 0;
375bad:
376 return -ERANGE;
377}
378
379static bool need_key(struct ceph_x_ticket_handler *th)
380{
381 if (!th->have_key)
382 return true;
383
384 return get_seconds() >= th->renew_after;
385}
386
387static bool have_key(struct ceph_x_ticket_handler *th)
388{
389 if (th->have_key) {
390 if (get_seconds() >= th->expires)
391 th->have_key = false;
392 }
393
394 return th->have_key;
395}
396
397static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
398{
399 int want = ac->want_keys;
400 struct ceph_x_info *xi = ac->private;
401 int service;
402
403 *pneed = ac->want_keys & ~(xi->have_keys);
404
405 for (service = 1; service <= want; service <<= 1) {
406 struct ceph_x_ticket_handler *th;
407
408 if (!(ac->want_keys & service))
409 continue;
410
411 if (*pneed & service)
412 continue;
413
414 th = get_ticket_handler(ac, service);
415 if (IS_ERR(th)) {
416 *pneed |= service;
417 continue;
418 }
419
420 if (need_key(th))
421 *pneed |= service;
422 if (!have_key(th))
423 xi->have_keys &= ~service;
424 }
425}
426
427static int ceph_x_build_request(struct ceph_auth_client *ac,
428 void *buf, void *end)
429{
430 struct ceph_x_info *xi = ac->private;
431 int need;
432 struct ceph_x_request_header *head = buf;
433 int ret;
434 struct ceph_x_ticket_handler *th =
435 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
436
437 if (IS_ERR(th))
438 return PTR_ERR(th);
439
440 ceph_x_validate_tickets(ac, &need);
441
442 dout("build_request want %x have %x need %x\n",
443 ac->want_keys, xi->have_keys, need);
444
445 if (need & CEPH_ENTITY_TYPE_AUTH) {
446 struct ceph_x_authenticate *auth = (void *)(head + 1);
447 void *p = auth + 1;
448 void *enc_buf = xi->auth_authorizer.enc_buf;
449 struct ceph_x_challenge_blob *blob = enc_buf +
450 ceph_x_encrypt_offset();
451 u64 *u;
452
453 if (p > end)
454 return -ERANGE;
455
456 dout(" get_auth_session_key\n");
457 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
458
459 /* encrypt and hash */
460 get_random_bytes(&auth->client_challenge, sizeof(u64));
461 blob->client_challenge = auth->client_challenge;
462 blob->server_challenge = cpu_to_le64(xi->server_challenge);
463 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
464 sizeof(*blob));
465 if (ret < 0)
466 return ret;
467
468 auth->struct_v = 1;
469 auth->key = 0;
470 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
471 auth->key ^= *(__le64 *)u;
472 dout(" server_challenge %llx client_challenge %llx key %llx\n",
473 xi->server_challenge, le64_to_cpu(auth->client_challenge),
474 le64_to_cpu(auth->key));
475
476 /* now encode the old ticket if exists */
477 ret = ceph_x_encode_ticket(th, &p, end);
478 if (ret < 0)
479 return ret;
480
481 return p - buf;
482 }
483
484 if (need) {
485 void *p = head + 1;
486 struct ceph_x_service_ticket_request *req;
487
488 if (p > end)
489 return -ERANGE;
490 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
491
492 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
493 if (ret)
494 return ret;
495 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
496 xi->auth_authorizer.buf->vec.iov_len);
497
498 req = p;
499 req->keys = cpu_to_le32(need);
500 p += sizeof(*req);
501 return p - buf;
502 }
503
504 return 0;
505}
506
507static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
508 void *buf, void *end)
509{
510 struct ceph_x_info *xi = ac->private;
511 struct ceph_x_reply_header *head = buf;
512 struct ceph_x_ticket_handler *th;
513 int len = end - buf;
514 int op;
515 int ret;
516
517 if (result)
518 return result; /* XXX hmm? */
519
520 if (xi->starting) {
521 /* it's a hello */
522 struct ceph_x_server_challenge *sc = buf;
523
524 if (len != sizeof(*sc))
525 return -EINVAL;
526 xi->server_challenge = le64_to_cpu(sc->server_challenge);
527 dout("handle_reply got server challenge %llx\n",
528 xi->server_challenge);
529 xi->starting = false;
530 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
531 return -EAGAIN;
532 }
533
534 op = le16_to_cpu(head->op);
535 result = le32_to_cpu(head->result);
536 dout("handle_reply op %d result %d\n", op, result);
537 switch (op) {
538 case CEPHX_GET_AUTH_SESSION_KEY:
539 /* verify auth key */
540 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
541 buf + sizeof(*head), end);
542 break;
543
544 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
545 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
546 if (IS_ERR(th))
547 return PTR_ERR(th);
548 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
549 buf + sizeof(*head), end);
550 break;
551
552 default:
553 return -EINVAL;
554 }
555 if (ret)
556 return ret;
557 if (ac->want_keys == xi->have_keys)
558 return 0;
559 return -EAGAIN;
560}
561
562static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
563{
564 struct ceph_x_authorizer *au = (void *)a;
565
566 ceph_x_authorizer_cleanup(au);
567 kfree(au);
568}
569
570static int ceph_x_create_authorizer(
571 struct ceph_auth_client *ac, int peer_type,
572 struct ceph_auth_handshake *auth)
573{
574 struct ceph_x_authorizer *au;
575 struct ceph_x_ticket_handler *th;
576 int ret;
577
578 th = get_ticket_handler(ac, peer_type);
579 if (IS_ERR(th))
580 return PTR_ERR(th);
581
582 au = kzalloc(sizeof(*au), GFP_NOFS);
583 if (!au)
584 return -ENOMEM;
585
586 au->base.destroy = ceph_x_destroy_authorizer;
587
588 ret = ceph_x_build_authorizer(ac, th, au);
589 if (ret) {
590 kfree(au);
591 return ret;
592 }
593
594 auth->authorizer = (struct ceph_authorizer *) au;
595 auth->authorizer_buf = au->buf->vec.iov_base;
596 auth->authorizer_buf_len = au->buf->vec.iov_len;
597 auth->authorizer_reply_buf = au->enc_buf;
598 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
599 auth->sign_message = ac->ops->sign_message;
600 auth->check_message_signature = ac->ops->check_message_signature;
601
602 return 0;
603}
604
605static int ceph_x_update_authorizer(
606 struct ceph_auth_client *ac, int peer_type,
607 struct ceph_auth_handshake *auth)
608{
609 struct ceph_x_authorizer *au;
610 struct ceph_x_ticket_handler *th;
611
612 th = get_ticket_handler(ac, peer_type);
613 if (IS_ERR(th))
614 return PTR_ERR(th);
615
616 au = (struct ceph_x_authorizer *)auth->authorizer;
617 if (au->secret_id < th->secret_id) {
618 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
619 au->service, au->secret_id, th->secret_id);
620 return ceph_x_build_authorizer(ac, th, au);
621 }
622 return 0;
623}
624
625static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
626 struct ceph_authorizer *a)
627{
628 struct ceph_x_authorizer *au = (void *)a;
629 void *p = au->enc_buf;
630 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
631 int ret;
632
633 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
634 if (ret < 0)
635 return ret;
636 if (ret != sizeof(*reply))
637 return -EPERM;
638
639 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
640 ret = -EPERM;
641 else
642 ret = 0;
643 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
644 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
645 return ret;
646}
647
648static void ceph_x_reset(struct ceph_auth_client *ac)
649{
650 struct ceph_x_info *xi = ac->private;
651
652 dout("reset\n");
653 xi->starting = true;
654 xi->server_challenge = 0;
655}
656
657static void ceph_x_destroy(struct ceph_auth_client *ac)
658{
659 struct ceph_x_info *xi = ac->private;
660 struct rb_node *p;
661
662 dout("ceph_x_destroy %p\n", ac);
663 ceph_crypto_key_destroy(&xi->secret);
664
665 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
666 struct ceph_x_ticket_handler *th =
667 rb_entry(p, struct ceph_x_ticket_handler, node);
668 remove_ticket_handler(ac, th);
669 }
670
671 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
672
673 kfree(ac->private);
674 ac->private = NULL;
675}
676
677static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
678{
679 struct ceph_x_ticket_handler *th;
680
681 th = get_ticket_handler(ac, peer_type);
682 if (!IS_ERR(th))
683 th->have_key = false;
684}
685
686static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
687 int peer_type)
688{
689 /*
690 * We are to invalidate a service ticket in the hopes of
691 * getting a new, hopefully more valid, one. But, we won't get
692 * it unless our AUTH ticket is good, so invalidate AUTH ticket
693 * as well, just in case.
694 */
695 invalidate_ticket(ac, peer_type);
696 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
697}
698
699static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
700 __le64 *psig)
701{
702 void *enc_buf = au->enc_buf;
703 struct {
704 __le32 len;
705 __le32 header_crc;
706 __le32 front_crc;
707 __le32 middle_crc;
708 __le32 data_crc;
709 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
710 int ret;
711
712 sigblock->len = cpu_to_le32(4*sizeof(u32));
713 sigblock->header_crc = msg->hdr.crc;
714 sigblock->front_crc = msg->footer.front_crc;
715 sigblock->middle_crc = msg->footer.middle_crc;
716 sigblock->data_crc = msg->footer.data_crc;
717 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
718 sizeof(*sigblock));
719 if (ret < 0)
720 return ret;
721
722 *psig = *(__le64 *)(enc_buf + sizeof(u32));
723 return 0;
724}
725
726static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
727 struct ceph_msg *msg)
728{
729 __le64 sig;
730 int ret;
731
732 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
733 return 0;
734
735 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
736 msg, &sig);
737 if (ret)
738 return ret;
739
740 msg->footer.sig = sig;
741 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
742 return 0;
743}
744
745static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
746 struct ceph_msg *msg)
747{
748 __le64 sig_check;
749 int ret;
750
751 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
752 return 0;
753
754 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
755 msg, &sig_check);
756 if (ret)
757 return ret;
758 if (sig_check == msg->footer.sig)
759 return 0;
760 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
761 dout("ceph_x_check_message_signature %p has signature %llx "
762 "expect %llx\n", msg, msg->footer.sig, sig_check);
763 else
764 dout("ceph_x_check_message_signature %p sender did not set "
765 "CEPH_MSG_FOOTER_SIGNED\n", msg);
766 return -EBADMSG;
767}
768
769static const struct ceph_auth_client_ops ceph_x_ops = {
770 .name = "x",
771 .is_authenticated = ceph_x_is_authenticated,
772 .should_authenticate = ceph_x_should_authenticate,
773 .build_request = ceph_x_build_request,
774 .handle_reply = ceph_x_handle_reply,
775 .create_authorizer = ceph_x_create_authorizer,
776 .update_authorizer = ceph_x_update_authorizer,
777 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
778 .invalidate_authorizer = ceph_x_invalidate_authorizer,
779 .reset = ceph_x_reset,
780 .destroy = ceph_x_destroy,
781 .sign_message = ceph_x_sign_message,
782 .check_message_signature = ceph_x_check_message_signature,
783};
784
785
786int ceph_x_init(struct ceph_auth_client *ac)
787{
788 struct ceph_x_info *xi;
789 int ret;
790
791 dout("ceph_x_init %p\n", ac);
792 ret = -ENOMEM;
793 xi = kzalloc(sizeof(*xi), GFP_NOFS);
794 if (!xi)
795 goto out;
796
797 ret = -EINVAL;
798 if (!ac->key) {
799 pr_err("no secret set (for auth_x protocol)\n");
800 goto out_nomem;
801 }
802
803 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
804 if (ret < 0) {
805 pr_err("cannot clone key: %d\n", ret);
806 goto out_nomem;
807 }
808
809 xi->starting = true;
810 xi->ticket_handlers = RB_ROOT;
811
812 ac->protocol = CEPH_AUTH_CEPHX;
813 ac->private = xi;
814 ac->ops = &ceph_x_ops;
815 return 0;
816
817out_nomem:
818 kfree(xi);
819out:
820 return ret;
821}
822
823