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