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