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