xref: /openbmc/linux/net/ceph/auth_x.c (revision f6cdb292)
13d14c5d2SYehuda Sadeh 
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh 
43d14c5d2SYehuda Sadeh #include <linux/err.h>
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/random.h>
73d14c5d2SYehuda Sadeh #include <linux/slab.h>
83d14c5d2SYehuda Sadeh 
93d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
103d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
11a51983e4SIlya Dryomov #include <linux/ceph/libceph.h>
1233d07337SYan, Zheng #include <linux/ceph/messenger.h>
133d14c5d2SYehuda Sadeh 
143d14c5d2SYehuda Sadeh #include "crypto.h"
153d14c5d2SYehuda Sadeh #include "auth_x.h"
163d14c5d2SYehuda Sadeh #include "auth_x_protocol.h"
173d14c5d2SYehuda Sadeh 
183d14c5d2SYehuda Sadeh static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
193d14c5d2SYehuda Sadeh 
203d14c5d2SYehuda Sadeh static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
213d14c5d2SYehuda Sadeh {
223d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
233d14c5d2SYehuda Sadeh 	int need;
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh 	ceph_x_validate_tickets(ac, &need);
263d14c5d2SYehuda Sadeh 	dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
273d14c5d2SYehuda Sadeh 	     ac->want_keys, need, xi->have_keys);
283d14c5d2SYehuda Sadeh 	return (ac->want_keys & xi->have_keys) == ac->want_keys;
293d14c5d2SYehuda Sadeh }
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
323d14c5d2SYehuda Sadeh {
333d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
343d14c5d2SYehuda Sadeh 	int need;
353d14c5d2SYehuda Sadeh 
363d14c5d2SYehuda Sadeh 	ceph_x_validate_tickets(ac, &need);
373d14c5d2SYehuda Sadeh 	dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
383d14c5d2SYehuda Sadeh 	     ac->want_keys, need, xi->have_keys);
393d14c5d2SYehuda Sadeh 	return need != 0;
403d14c5d2SYehuda Sadeh }
413d14c5d2SYehuda Sadeh 
423d14c5d2SYehuda Sadeh static int ceph_x_encrypt_buflen(int ilen)
433d14c5d2SYehuda Sadeh {
443d14c5d2SYehuda Sadeh 	return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
453d14c5d2SYehuda Sadeh 		sizeof(u32);
463d14c5d2SYehuda Sadeh }
473d14c5d2SYehuda Sadeh 
483d14c5d2SYehuda Sadeh static int ceph_x_encrypt(struct ceph_crypto_key *secret,
493d14c5d2SYehuda Sadeh 			  void *ibuf, int ilen, void *obuf, size_t olen)
503d14c5d2SYehuda Sadeh {
513d14c5d2SYehuda Sadeh 	struct ceph_x_encrypt_header head = {
523d14c5d2SYehuda Sadeh 		.struct_v = 1,
533d14c5d2SYehuda Sadeh 		.magic = cpu_to_le64(CEPHX_ENC_MAGIC)
543d14c5d2SYehuda Sadeh 	};
553d14c5d2SYehuda Sadeh 	size_t len = olen - sizeof(u32);
563d14c5d2SYehuda Sadeh 	int ret;
573d14c5d2SYehuda Sadeh 
583d14c5d2SYehuda Sadeh 	ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
593d14c5d2SYehuda Sadeh 			    &head, sizeof(head), ibuf, ilen);
603d14c5d2SYehuda Sadeh 	if (ret)
613d14c5d2SYehuda Sadeh 		return ret;
623d14c5d2SYehuda Sadeh 	ceph_encode_32(&obuf, len);
633d14c5d2SYehuda Sadeh 	return len + sizeof(u32);
643d14c5d2SYehuda Sadeh }
653d14c5d2SYehuda Sadeh 
663d14c5d2SYehuda Sadeh static int ceph_x_decrypt(struct ceph_crypto_key *secret,
67c27a3e4dSIlya Dryomov 			  void **p, void *end, void **obuf, size_t olen)
683d14c5d2SYehuda Sadeh {
693d14c5d2SYehuda Sadeh 	struct ceph_x_encrypt_header head;
703d14c5d2SYehuda Sadeh 	size_t head_len = sizeof(head);
713d14c5d2SYehuda Sadeh 	int len, ret;
723d14c5d2SYehuda Sadeh 
733d14c5d2SYehuda Sadeh 	len = ceph_decode_32(p);
743d14c5d2SYehuda Sadeh 	if (*p + len > end)
753d14c5d2SYehuda Sadeh 		return -EINVAL;
763d14c5d2SYehuda Sadeh 
773d14c5d2SYehuda Sadeh 	dout("ceph_x_decrypt len %d\n", len);
78c27a3e4dSIlya Dryomov 	if (*obuf == NULL) {
79c27a3e4dSIlya Dryomov 		*obuf = kmalloc(len, GFP_NOFS);
80c27a3e4dSIlya Dryomov 		if (!*obuf)
81c27a3e4dSIlya Dryomov 			return -ENOMEM;
82c27a3e4dSIlya Dryomov 		olen = len;
83c27a3e4dSIlya Dryomov 	}
84c27a3e4dSIlya Dryomov 
85c27a3e4dSIlya Dryomov 	ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
863d14c5d2SYehuda Sadeh 	if (ret)
873d14c5d2SYehuda Sadeh 		return ret;
883d14c5d2SYehuda Sadeh 	if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
893d14c5d2SYehuda Sadeh 		return -EPERM;
903d14c5d2SYehuda Sadeh 	*p += len;
913d14c5d2SYehuda Sadeh 	return olen;
923d14c5d2SYehuda Sadeh }
933d14c5d2SYehuda Sadeh 
943d14c5d2SYehuda Sadeh /*
953d14c5d2SYehuda Sadeh  * get existing (or insert new) ticket handler
963d14c5d2SYehuda Sadeh  */
973d14c5d2SYehuda Sadeh static struct ceph_x_ticket_handler *
983d14c5d2SYehuda Sadeh get_ticket_handler(struct ceph_auth_client *ac, int service)
993d14c5d2SYehuda Sadeh {
1003d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th;
1013d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
1023d14c5d2SYehuda Sadeh 	struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
1033d14c5d2SYehuda Sadeh 
1043d14c5d2SYehuda Sadeh 	while (*p) {
1053d14c5d2SYehuda Sadeh 		parent = *p;
1063d14c5d2SYehuda Sadeh 		th = rb_entry(parent, struct ceph_x_ticket_handler, node);
1073d14c5d2SYehuda Sadeh 		if (service < th->service)
1083d14c5d2SYehuda Sadeh 			p = &(*p)->rb_left;
1093d14c5d2SYehuda Sadeh 		else if (service > th->service)
1103d14c5d2SYehuda Sadeh 			p = &(*p)->rb_right;
1113d14c5d2SYehuda Sadeh 		else
1123d14c5d2SYehuda Sadeh 			return th;
1133d14c5d2SYehuda Sadeh 	}
1143d14c5d2SYehuda Sadeh 
1153d14c5d2SYehuda Sadeh 	/* add it */
1163d14c5d2SYehuda Sadeh 	th = kzalloc(sizeof(*th), GFP_NOFS);
1173d14c5d2SYehuda Sadeh 	if (!th)
1183d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
1193d14c5d2SYehuda Sadeh 	th->service = service;
1203d14c5d2SYehuda Sadeh 	rb_link_node(&th->node, parent, p);
1213d14c5d2SYehuda Sadeh 	rb_insert_color(&th->node, &xi->ticket_handlers);
1223d14c5d2SYehuda Sadeh 	return th;
1233d14c5d2SYehuda Sadeh }
1243d14c5d2SYehuda Sadeh 
1253d14c5d2SYehuda Sadeh static void remove_ticket_handler(struct ceph_auth_client *ac,
1263d14c5d2SYehuda Sadeh 				  struct ceph_x_ticket_handler *th)
1273d14c5d2SYehuda Sadeh {
1283d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
1293d14c5d2SYehuda Sadeh 
1303d14c5d2SYehuda Sadeh 	dout("remove_ticket_handler %p %d\n", th, th->service);
1313d14c5d2SYehuda Sadeh 	rb_erase(&th->node, &xi->ticket_handlers);
1323d14c5d2SYehuda Sadeh 	ceph_crypto_key_destroy(&th->session_key);
1333d14c5d2SYehuda Sadeh 	if (th->ticket_blob)
1343d14c5d2SYehuda Sadeh 		ceph_buffer_put(th->ticket_blob);
1353d14c5d2SYehuda Sadeh 	kfree(th);
1363d14c5d2SYehuda Sadeh }
1373d14c5d2SYehuda Sadeh 
138597cda35SIlya Dryomov static int process_one_ticket(struct ceph_auth_client *ac,
1393d14c5d2SYehuda Sadeh 			      struct ceph_crypto_key *secret,
140c27a3e4dSIlya Dryomov 			      void **p, void *end)
1413d14c5d2SYehuda Sadeh {
1423d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
1433d14c5d2SYehuda Sadeh 	int type;
1443d14c5d2SYehuda Sadeh 	u8 tkt_struct_v, blob_struct_v;
1453d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th;
146c27a3e4dSIlya Dryomov 	void *dbuf = NULL;
1473d14c5d2SYehuda Sadeh 	void *dp, *dend;
1483d14c5d2SYehuda Sadeh 	int dlen;
1493d14c5d2SYehuda Sadeh 	char is_enc;
1503d14c5d2SYehuda Sadeh 	struct timespec validity;
1513d14c5d2SYehuda Sadeh 	struct ceph_crypto_key old_key;
152c27a3e4dSIlya Dryomov 	void *ticket_buf = NULL;
1533d14c5d2SYehuda Sadeh 	void *tp, *tpend;
154e9226d7cSIlya Dryomov 	void **ptp;
1553d14c5d2SYehuda Sadeh 	struct ceph_crypto_key new_session_key;
1563d14c5d2SYehuda Sadeh 	struct ceph_buffer *new_ticket_blob;
1573d14c5d2SYehuda Sadeh 	unsigned long new_expires, new_renew_after;
1583d14c5d2SYehuda Sadeh 	u64 new_secret_id;
159597cda35SIlya Dryomov 	int ret;
1603d14c5d2SYehuda Sadeh 
161597cda35SIlya Dryomov 	ceph_decode_need(p, end, sizeof(u32) + 1, bad);
1623d14c5d2SYehuda Sadeh 
163597cda35SIlya Dryomov 	type = ceph_decode_32(p);
1643d14c5d2SYehuda Sadeh 	dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
1653d14c5d2SYehuda Sadeh 
166597cda35SIlya Dryomov 	tkt_struct_v = ceph_decode_8(p);
1673d14c5d2SYehuda Sadeh 	if (tkt_struct_v != 1)
1683d14c5d2SYehuda Sadeh 		goto bad;
1693d14c5d2SYehuda Sadeh 
1703d14c5d2SYehuda Sadeh 	th = get_ticket_handler(ac, type);
1713d14c5d2SYehuda Sadeh 	if (IS_ERR(th)) {
1723d14c5d2SYehuda Sadeh 		ret = PTR_ERR(th);
1733d14c5d2SYehuda Sadeh 		goto out;
1743d14c5d2SYehuda Sadeh 	}
1753d14c5d2SYehuda Sadeh 
1763d14c5d2SYehuda Sadeh 	/* blob for me */
177c27a3e4dSIlya Dryomov 	dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
1783d14c5d2SYehuda Sadeh 	if (dlen <= 0) {
1793d14c5d2SYehuda Sadeh 		ret = dlen;
1803d14c5d2SYehuda Sadeh 		goto out;
1813d14c5d2SYehuda Sadeh 	}
1823d14c5d2SYehuda Sadeh 	dout(" decrypted %d bytes\n", dlen);
1833d14c5d2SYehuda Sadeh 	dp = dbuf;
184597cda35SIlya Dryomov 	dend = dp + dlen;
1853d14c5d2SYehuda Sadeh 
1863d14c5d2SYehuda Sadeh 	tkt_struct_v = ceph_decode_8(&dp);
1873d14c5d2SYehuda Sadeh 	if (tkt_struct_v != 1)
1883d14c5d2SYehuda Sadeh 		goto bad;
1893d14c5d2SYehuda Sadeh 
1903d14c5d2SYehuda Sadeh 	memcpy(&old_key, &th->session_key, sizeof(old_key));
1913d14c5d2SYehuda Sadeh 	ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
1923d14c5d2SYehuda Sadeh 	if (ret)
1933d14c5d2SYehuda Sadeh 		goto out;
1943d14c5d2SYehuda Sadeh 
195f6cdb292SIlya Dryomov 	ceph_decode_timespec(&validity, dp);
196f6cdb292SIlya Dryomov 	dp += sizeof(struct ceph_timespec);
1973d14c5d2SYehuda Sadeh 	new_expires = get_seconds() + validity.tv_sec;
1983d14c5d2SYehuda Sadeh 	new_renew_after = new_expires - (validity.tv_sec / 4);
1993d14c5d2SYehuda Sadeh 	dout(" expires=%lu renew_after=%lu\n", new_expires,
2003d14c5d2SYehuda Sadeh 	     new_renew_after);
2013d14c5d2SYehuda Sadeh 
2023d14c5d2SYehuda Sadeh 	/* ticket blob for service */
203597cda35SIlya Dryomov 	ceph_decode_8_safe(p, end, is_enc, bad);
2043d14c5d2SYehuda Sadeh 	if (is_enc) {
2053d14c5d2SYehuda Sadeh 		/* encrypted */
2063d14c5d2SYehuda Sadeh 		dout(" encrypted ticket\n");
207c27a3e4dSIlya Dryomov 		dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0);
2083d14c5d2SYehuda Sadeh 		if (dlen < 0) {
2093d14c5d2SYehuda Sadeh 			ret = dlen;
2103d14c5d2SYehuda Sadeh 			goto out;
2113d14c5d2SYehuda Sadeh 		}
212c27a3e4dSIlya Dryomov 		tp = ticket_buf;
213e9226d7cSIlya Dryomov 		ptp = &tp;
214e9226d7cSIlya Dryomov 		tpend = *ptp + dlen;
2153d14c5d2SYehuda Sadeh 	} else {
2163d14c5d2SYehuda Sadeh 		/* unencrypted */
217e9226d7cSIlya Dryomov 		ptp = p;
218e9226d7cSIlya Dryomov 		tpend = end;
219c27a3e4dSIlya Dryomov 	}
220e9226d7cSIlya Dryomov 	ceph_decode_32_safe(ptp, tpend, dlen, bad);
2213d14c5d2SYehuda Sadeh 	dout(" ticket blob is %d bytes\n", dlen);
222e9226d7cSIlya Dryomov 	ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
223e9226d7cSIlya Dryomov 	blob_struct_v = ceph_decode_8(ptp);
224e9226d7cSIlya Dryomov 	new_secret_id = ceph_decode_64(ptp);
225e9226d7cSIlya Dryomov 	ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
2263d14c5d2SYehuda Sadeh 	if (ret)
2273d14c5d2SYehuda Sadeh 		goto out;
2283d14c5d2SYehuda Sadeh 
2293d14c5d2SYehuda Sadeh 	/* all is well, update our ticket */
2303d14c5d2SYehuda Sadeh 	ceph_crypto_key_destroy(&th->session_key);
2313d14c5d2SYehuda Sadeh 	if (th->ticket_blob)
2323d14c5d2SYehuda Sadeh 		ceph_buffer_put(th->ticket_blob);
2333d14c5d2SYehuda Sadeh 	th->session_key = new_session_key;
2343d14c5d2SYehuda Sadeh 	th->ticket_blob = new_ticket_blob;
2353d14c5d2SYehuda Sadeh 	th->secret_id = new_secret_id;
2363d14c5d2SYehuda Sadeh 	th->expires = new_expires;
2373d14c5d2SYehuda Sadeh 	th->renew_after = new_renew_after;
2386abe097dSIlya Dryomov 	th->have_key = true;
2393d14c5d2SYehuda Sadeh 	dout(" got ticket service %d (%s) secret_id %lld len %d\n",
2403d14c5d2SYehuda Sadeh 	     type, ceph_entity_type_name(type), th->secret_id,
2413d14c5d2SYehuda Sadeh 	     (int)th->ticket_blob->vec.iov_len);
2423d14c5d2SYehuda Sadeh 	xi->have_keys |= th->service;
243597cda35SIlya Dryomov 
244597cda35SIlya Dryomov out:
245c27a3e4dSIlya Dryomov 	kfree(ticket_buf);
246c27a3e4dSIlya Dryomov 	kfree(dbuf);
247597cda35SIlya Dryomov 	return ret;
248597cda35SIlya Dryomov 
249597cda35SIlya Dryomov bad:
250597cda35SIlya Dryomov 	ret = -EINVAL;
251597cda35SIlya Dryomov 	goto out;
252597cda35SIlya Dryomov }
253597cda35SIlya Dryomov 
254597cda35SIlya Dryomov static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
255597cda35SIlya Dryomov 				    struct ceph_crypto_key *secret,
256597cda35SIlya Dryomov 				    void *buf, void *end)
257597cda35SIlya Dryomov {
258597cda35SIlya Dryomov 	void *p = buf;
259597cda35SIlya Dryomov 	u8 reply_struct_v;
260597cda35SIlya Dryomov 	u32 num;
261597cda35SIlya Dryomov 	int ret;
262597cda35SIlya Dryomov 
263597cda35SIlya Dryomov 	ceph_decode_8_safe(&p, end, reply_struct_v, bad);
264597cda35SIlya Dryomov 	if (reply_struct_v != 1)
265597cda35SIlya Dryomov 		return -EINVAL;
266597cda35SIlya Dryomov 
267597cda35SIlya Dryomov 	ceph_decode_32_safe(&p, end, num, bad);
268597cda35SIlya Dryomov 	dout("%d tickets\n", num);
269597cda35SIlya Dryomov 
270597cda35SIlya Dryomov 	while (num--) {
271c27a3e4dSIlya Dryomov 		ret = process_one_ticket(ac, secret, &p, end);
272597cda35SIlya Dryomov 		if (ret)
273c27a3e4dSIlya Dryomov 			return ret;
2743d14c5d2SYehuda Sadeh 	}
2753d14c5d2SYehuda Sadeh 
276c27a3e4dSIlya Dryomov 	return 0;
2773d14c5d2SYehuda Sadeh 
2783d14c5d2SYehuda Sadeh bad:
279c27a3e4dSIlya Dryomov 	return -EINVAL;
2803d14c5d2SYehuda Sadeh }
2813d14c5d2SYehuda Sadeh 
282cbf99a11SIlya Dryomov static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
283cbf99a11SIlya Dryomov {
284cbf99a11SIlya Dryomov 	ceph_crypto_key_destroy(&au->session_key);
285cbf99a11SIlya Dryomov 	if (au->buf) {
286cbf99a11SIlya Dryomov 		ceph_buffer_put(au->buf);
287cbf99a11SIlya Dryomov 		au->buf = NULL;
288cbf99a11SIlya Dryomov 	}
289cbf99a11SIlya Dryomov }
290cbf99a11SIlya Dryomov 
2913d14c5d2SYehuda Sadeh static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
2923d14c5d2SYehuda Sadeh 				   struct ceph_x_ticket_handler *th,
2933d14c5d2SYehuda Sadeh 				   struct ceph_x_authorizer *au)
2943d14c5d2SYehuda Sadeh {
2953d14c5d2SYehuda Sadeh 	int maxlen;
2963d14c5d2SYehuda Sadeh 	struct ceph_x_authorize_a *msg_a;
2973d14c5d2SYehuda Sadeh 	struct ceph_x_authorize_b msg_b;
2983d14c5d2SYehuda Sadeh 	void *p, *end;
2993d14c5d2SYehuda Sadeh 	int ret;
3003d14c5d2SYehuda Sadeh 	int ticket_blob_len =
3013d14c5d2SYehuda Sadeh 		(th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
3023d14c5d2SYehuda Sadeh 
3033d14c5d2SYehuda Sadeh 	dout("build_authorizer for %s %p\n",
3043d14c5d2SYehuda Sadeh 	     ceph_entity_type_name(th->service), au);
3053d14c5d2SYehuda Sadeh 
306ae385eafSYan, Zheng 	ceph_crypto_key_destroy(&au->session_key);
307ae385eafSYan, Zheng 	ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
308ae385eafSYan, Zheng 	if (ret)
309cbf99a11SIlya Dryomov 		goto out_au;
310ae385eafSYan, Zheng 
3113d14c5d2SYehuda Sadeh 	maxlen = sizeof(*msg_a) + sizeof(msg_b) +
3123d14c5d2SYehuda Sadeh 		ceph_x_encrypt_buflen(ticket_blob_len);
3133d14c5d2SYehuda Sadeh 	dout("  need len %d\n", maxlen);
3143d14c5d2SYehuda Sadeh 	if (au->buf && au->buf->alloc_len < maxlen) {
3153d14c5d2SYehuda Sadeh 		ceph_buffer_put(au->buf);
3163d14c5d2SYehuda Sadeh 		au->buf = NULL;
3173d14c5d2SYehuda Sadeh 	}
3183d14c5d2SYehuda Sadeh 	if (!au->buf) {
3193d14c5d2SYehuda Sadeh 		au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
320ae385eafSYan, Zheng 		if (!au->buf) {
321cbf99a11SIlya Dryomov 			ret = -ENOMEM;
322cbf99a11SIlya Dryomov 			goto out_au;
3233d14c5d2SYehuda Sadeh 		}
324ae385eafSYan, Zheng 	}
3253d14c5d2SYehuda Sadeh 	au->service = th->service;
3260bed9b5cSSage Weil 	au->secret_id = th->secret_id;
3273d14c5d2SYehuda Sadeh 
3283d14c5d2SYehuda Sadeh 	msg_a = au->buf->vec.iov_base;
3293d14c5d2SYehuda Sadeh 	msg_a->struct_v = 1;
3303d14c5d2SYehuda Sadeh 	msg_a->global_id = cpu_to_le64(ac->global_id);
3313d14c5d2SYehuda Sadeh 	msg_a->service_id = cpu_to_le32(th->service);
3323d14c5d2SYehuda Sadeh 	msg_a->ticket_blob.struct_v = 1;
3333d14c5d2SYehuda Sadeh 	msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
3343d14c5d2SYehuda Sadeh 	msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
3353d14c5d2SYehuda Sadeh 	if (ticket_blob_len) {
3363d14c5d2SYehuda Sadeh 		memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
3373d14c5d2SYehuda Sadeh 		       th->ticket_blob->vec.iov_len);
3383d14c5d2SYehuda Sadeh 	}
3393d14c5d2SYehuda Sadeh 	dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
3403d14c5d2SYehuda Sadeh 	     le64_to_cpu(msg_a->ticket_blob.secret_id));
3413d14c5d2SYehuda Sadeh 
3423d14c5d2SYehuda Sadeh 	p = msg_a + 1;
3433d14c5d2SYehuda Sadeh 	p += ticket_blob_len;
3443d14c5d2SYehuda Sadeh 	end = au->buf->vec.iov_base + au->buf->vec.iov_len;
3453d14c5d2SYehuda Sadeh 
3463d14c5d2SYehuda Sadeh 	get_random_bytes(&au->nonce, sizeof(au->nonce));
3473d14c5d2SYehuda Sadeh 	msg_b.struct_v = 1;
3483d14c5d2SYehuda Sadeh 	msg_b.nonce = cpu_to_le64(au->nonce);
349ae385eafSYan, Zheng 	ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
3503d14c5d2SYehuda Sadeh 			     p, end - p);
3513d14c5d2SYehuda Sadeh 	if (ret < 0)
352cbf99a11SIlya Dryomov 		goto out_au;
3533d14c5d2SYehuda Sadeh 	p += ret;
3543d14c5d2SYehuda Sadeh 	au->buf->vec.iov_len = p - au->buf->vec.iov_base;
3553d14c5d2SYehuda Sadeh 	dout(" built authorizer nonce %llx len %d\n", au->nonce,
3563d14c5d2SYehuda Sadeh 	     (int)au->buf->vec.iov_len);
3573d14c5d2SYehuda Sadeh 	BUG_ON(au->buf->vec.iov_len > maxlen);
3583d14c5d2SYehuda Sadeh 	return 0;
3593d14c5d2SYehuda Sadeh 
360cbf99a11SIlya Dryomov out_au:
361cbf99a11SIlya Dryomov 	ceph_x_authorizer_cleanup(au);
3623d14c5d2SYehuda Sadeh 	return ret;
3633d14c5d2SYehuda Sadeh }
3643d14c5d2SYehuda Sadeh 
3653d14c5d2SYehuda Sadeh static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
3663d14c5d2SYehuda Sadeh 				void **p, void *end)
3673d14c5d2SYehuda Sadeh {
3683d14c5d2SYehuda Sadeh 	ceph_decode_need(p, end, 1 + sizeof(u64), bad);
3693d14c5d2SYehuda Sadeh 	ceph_encode_8(p, 1);
3703d14c5d2SYehuda Sadeh 	ceph_encode_64(p, th->secret_id);
3713d14c5d2SYehuda Sadeh 	if (th->ticket_blob) {
3723d14c5d2SYehuda Sadeh 		const char *buf = th->ticket_blob->vec.iov_base;
3733d14c5d2SYehuda Sadeh 		u32 len = th->ticket_blob->vec.iov_len;
3743d14c5d2SYehuda Sadeh 
3753d14c5d2SYehuda Sadeh 		ceph_encode_32_safe(p, end, len, bad);
3763d14c5d2SYehuda Sadeh 		ceph_encode_copy_safe(p, end, buf, len, bad);
3773d14c5d2SYehuda Sadeh 	} else {
3783d14c5d2SYehuda Sadeh 		ceph_encode_32_safe(p, end, 0, bad);
3793d14c5d2SYehuda Sadeh 	}
3803d14c5d2SYehuda Sadeh 
3813d14c5d2SYehuda Sadeh 	return 0;
3823d14c5d2SYehuda Sadeh bad:
3833d14c5d2SYehuda Sadeh 	return -ERANGE;
3843d14c5d2SYehuda Sadeh }
3853d14c5d2SYehuda Sadeh 
3866abe097dSIlya Dryomov static bool need_key(struct ceph_x_ticket_handler *th)
3876abe097dSIlya Dryomov {
3886abe097dSIlya Dryomov 	if (!th->have_key)
3896abe097dSIlya Dryomov 		return true;
3906abe097dSIlya Dryomov 
3916abe097dSIlya Dryomov 	return get_seconds() >= th->renew_after;
3926abe097dSIlya Dryomov }
3936abe097dSIlya Dryomov 
3946abe097dSIlya Dryomov static bool have_key(struct ceph_x_ticket_handler *th)
3956abe097dSIlya Dryomov {
3966abe097dSIlya Dryomov 	if (th->have_key) {
3976abe097dSIlya Dryomov 		if (get_seconds() >= th->expires)
3986abe097dSIlya Dryomov 			th->have_key = false;
3996abe097dSIlya Dryomov 	}
4006abe097dSIlya Dryomov 
4016abe097dSIlya Dryomov 	return th->have_key;
4026abe097dSIlya Dryomov }
4036abe097dSIlya Dryomov 
4043d14c5d2SYehuda Sadeh static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
4053d14c5d2SYehuda Sadeh {
4063d14c5d2SYehuda Sadeh 	int want = ac->want_keys;
4073d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
4083d14c5d2SYehuda Sadeh 	int service;
4093d14c5d2SYehuda Sadeh 
4103d14c5d2SYehuda Sadeh 	*pneed = ac->want_keys & ~(xi->have_keys);
4113d14c5d2SYehuda Sadeh 
4123d14c5d2SYehuda Sadeh 	for (service = 1; service <= want; service <<= 1) {
4133d14c5d2SYehuda Sadeh 		struct ceph_x_ticket_handler *th;
4143d14c5d2SYehuda Sadeh 
4153d14c5d2SYehuda Sadeh 		if (!(ac->want_keys & service))
4163d14c5d2SYehuda Sadeh 			continue;
4173d14c5d2SYehuda Sadeh 
4183d14c5d2SYehuda Sadeh 		if (*pneed & service)
4193d14c5d2SYehuda Sadeh 			continue;
4203d14c5d2SYehuda Sadeh 
4213d14c5d2SYehuda Sadeh 		th = get_ticket_handler(ac, service);
4223d14c5d2SYehuda Sadeh 		if (IS_ERR(th)) {
4233d14c5d2SYehuda Sadeh 			*pneed |= service;
4243d14c5d2SYehuda Sadeh 			continue;
4253d14c5d2SYehuda Sadeh 		}
4263d14c5d2SYehuda Sadeh 
4276abe097dSIlya Dryomov 		if (need_key(th))
4283d14c5d2SYehuda Sadeh 			*pneed |= service;
4296abe097dSIlya Dryomov 		if (!have_key(th))
4303d14c5d2SYehuda Sadeh 			xi->have_keys &= ~service;
4313d14c5d2SYehuda Sadeh 	}
4323d14c5d2SYehuda Sadeh }
4333d14c5d2SYehuda Sadeh 
4343d14c5d2SYehuda Sadeh static int ceph_x_build_request(struct ceph_auth_client *ac,
4353d14c5d2SYehuda Sadeh 				void *buf, void *end)
4363d14c5d2SYehuda Sadeh {
4373d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
4383d14c5d2SYehuda Sadeh 	int need;
4393d14c5d2SYehuda Sadeh 	struct ceph_x_request_header *head = buf;
4403d14c5d2SYehuda Sadeh 	int ret;
4413d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th =
4423d14c5d2SYehuda Sadeh 		get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
4433d14c5d2SYehuda Sadeh 
4443d14c5d2SYehuda Sadeh 	if (IS_ERR(th))
4453d14c5d2SYehuda Sadeh 		return PTR_ERR(th);
4463d14c5d2SYehuda Sadeh 
4473d14c5d2SYehuda Sadeh 	ceph_x_validate_tickets(ac, &need);
4483d14c5d2SYehuda Sadeh 
4493d14c5d2SYehuda Sadeh 	dout("build_request want %x have %x need %x\n",
4503d14c5d2SYehuda Sadeh 	     ac->want_keys, xi->have_keys, need);
4513d14c5d2SYehuda Sadeh 
4523d14c5d2SYehuda Sadeh 	if (need & CEPH_ENTITY_TYPE_AUTH) {
4533d14c5d2SYehuda Sadeh 		struct ceph_x_authenticate *auth = (void *)(head + 1);
4543d14c5d2SYehuda Sadeh 		void *p = auth + 1;
4553d14c5d2SYehuda Sadeh 		struct ceph_x_challenge_blob tmp;
4563d14c5d2SYehuda Sadeh 		char tmp_enc[40];
4573d14c5d2SYehuda Sadeh 		u64 *u;
4583d14c5d2SYehuda Sadeh 
4593d14c5d2SYehuda Sadeh 		if (p > end)
4603d14c5d2SYehuda Sadeh 			return -ERANGE;
4613d14c5d2SYehuda Sadeh 
4623d14c5d2SYehuda Sadeh 		dout(" get_auth_session_key\n");
4633d14c5d2SYehuda Sadeh 		head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
4643d14c5d2SYehuda Sadeh 
4653d14c5d2SYehuda Sadeh 		/* encrypt and hash */
4663d14c5d2SYehuda Sadeh 		get_random_bytes(&auth->client_challenge, sizeof(u64));
4673d14c5d2SYehuda Sadeh 		tmp.client_challenge = auth->client_challenge;
4683d14c5d2SYehuda Sadeh 		tmp.server_challenge = cpu_to_le64(xi->server_challenge);
4693d14c5d2SYehuda Sadeh 		ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
4703d14c5d2SYehuda Sadeh 				     tmp_enc, sizeof(tmp_enc));
4713d14c5d2SYehuda Sadeh 		if (ret < 0)
4723d14c5d2SYehuda Sadeh 			return ret;
4733d14c5d2SYehuda Sadeh 
4743d14c5d2SYehuda Sadeh 		auth->struct_v = 1;
4753d14c5d2SYehuda Sadeh 		auth->key = 0;
4763d14c5d2SYehuda Sadeh 		for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
4773d14c5d2SYehuda Sadeh 			auth->key ^= *(__le64 *)u;
4783d14c5d2SYehuda Sadeh 		dout(" server_challenge %llx client_challenge %llx key %llx\n",
4793d14c5d2SYehuda Sadeh 		     xi->server_challenge, le64_to_cpu(auth->client_challenge),
4803d14c5d2SYehuda Sadeh 		     le64_to_cpu(auth->key));
4813d14c5d2SYehuda Sadeh 
4823d14c5d2SYehuda Sadeh 		/* now encode the old ticket if exists */
4833d14c5d2SYehuda Sadeh 		ret = ceph_x_encode_ticket(th, &p, end);
4843d14c5d2SYehuda Sadeh 		if (ret < 0)
4853d14c5d2SYehuda Sadeh 			return ret;
4863d14c5d2SYehuda Sadeh 
4873d14c5d2SYehuda Sadeh 		return p - buf;
4883d14c5d2SYehuda Sadeh 	}
4893d14c5d2SYehuda Sadeh 
4903d14c5d2SYehuda Sadeh 	if (need) {
4913d14c5d2SYehuda Sadeh 		void *p = head + 1;
4923d14c5d2SYehuda Sadeh 		struct ceph_x_service_ticket_request *req;
4933d14c5d2SYehuda Sadeh 
4943d14c5d2SYehuda Sadeh 		if (p > end)
4953d14c5d2SYehuda Sadeh 			return -ERANGE;
4963d14c5d2SYehuda Sadeh 		head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
4973d14c5d2SYehuda Sadeh 
4983d14c5d2SYehuda Sadeh 		ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
4993d14c5d2SYehuda Sadeh 		if (ret)
5003d14c5d2SYehuda Sadeh 			return ret;
5013d14c5d2SYehuda Sadeh 		ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
5023d14c5d2SYehuda Sadeh 				 xi->auth_authorizer.buf->vec.iov_len);
5033d14c5d2SYehuda Sadeh 
5043d14c5d2SYehuda Sadeh 		req = p;
5053d14c5d2SYehuda Sadeh 		req->keys = cpu_to_le32(need);
5063d14c5d2SYehuda Sadeh 		p += sizeof(*req);
5073d14c5d2SYehuda Sadeh 		return p - buf;
5083d14c5d2SYehuda Sadeh 	}
5093d14c5d2SYehuda Sadeh 
5103d14c5d2SYehuda Sadeh 	return 0;
5113d14c5d2SYehuda Sadeh }
5123d14c5d2SYehuda Sadeh 
5133d14c5d2SYehuda Sadeh static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
5143d14c5d2SYehuda Sadeh 			       void *buf, void *end)
5153d14c5d2SYehuda Sadeh {
5163d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
5173d14c5d2SYehuda Sadeh 	struct ceph_x_reply_header *head = buf;
5183d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th;
5193d14c5d2SYehuda Sadeh 	int len = end - buf;
5203d14c5d2SYehuda Sadeh 	int op;
5213d14c5d2SYehuda Sadeh 	int ret;
5223d14c5d2SYehuda Sadeh 
5233d14c5d2SYehuda Sadeh 	if (result)
5243d14c5d2SYehuda Sadeh 		return result;  /* XXX hmm? */
5253d14c5d2SYehuda Sadeh 
5263d14c5d2SYehuda Sadeh 	if (xi->starting) {
5273d14c5d2SYehuda Sadeh 		/* it's a hello */
5283d14c5d2SYehuda Sadeh 		struct ceph_x_server_challenge *sc = buf;
5293d14c5d2SYehuda Sadeh 
5303d14c5d2SYehuda Sadeh 		if (len != sizeof(*sc))
5313d14c5d2SYehuda Sadeh 			return -EINVAL;
5323d14c5d2SYehuda Sadeh 		xi->server_challenge = le64_to_cpu(sc->server_challenge);
5333d14c5d2SYehuda Sadeh 		dout("handle_reply got server challenge %llx\n",
5343d14c5d2SYehuda Sadeh 		     xi->server_challenge);
5353d14c5d2SYehuda Sadeh 		xi->starting = false;
5363d14c5d2SYehuda Sadeh 		xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
5373d14c5d2SYehuda Sadeh 		return -EAGAIN;
5383d14c5d2SYehuda Sadeh 	}
5393d14c5d2SYehuda Sadeh 
5403d14c5d2SYehuda Sadeh 	op = le16_to_cpu(head->op);
5413d14c5d2SYehuda Sadeh 	result = le32_to_cpu(head->result);
5423d14c5d2SYehuda Sadeh 	dout("handle_reply op %d result %d\n", op, result);
5433d14c5d2SYehuda Sadeh 	switch (op) {
5443d14c5d2SYehuda Sadeh 	case CEPHX_GET_AUTH_SESSION_KEY:
5453d14c5d2SYehuda Sadeh 		/* verify auth key */
5463d14c5d2SYehuda Sadeh 		ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
5473d14c5d2SYehuda Sadeh 					       buf + sizeof(*head), end);
5483d14c5d2SYehuda Sadeh 		break;
5493d14c5d2SYehuda Sadeh 
5503d14c5d2SYehuda Sadeh 	case CEPHX_GET_PRINCIPAL_SESSION_KEY:
5513d14c5d2SYehuda Sadeh 		th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
5523d14c5d2SYehuda Sadeh 		if (IS_ERR(th))
5533d14c5d2SYehuda Sadeh 			return PTR_ERR(th);
5543d14c5d2SYehuda Sadeh 		ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
5553d14c5d2SYehuda Sadeh 					       buf + sizeof(*head), end);
5563d14c5d2SYehuda Sadeh 		break;
5573d14c5d2SYehuda Sadeh 
5583d14c5d2SYehuda Sadeh 	default:
5593d14c5d2SYehuda Sadeh 		return -EINVAL;
5603d14c5d2SYehuda Sadeh 	}
5613d14c5d2SYehuda Sadeh 	if (ret)
5623d14c5d2SYehuda Sadeh 		return ret;
5633d14c5d2SYehuda Sadeh 	if (ac->want_keys == xi->have_keys)
5643d14c5d2SYehuda Sadeh 		return 0;
5653d14c5d2SYehuda Sadeh 	return -EAGAIN;
5663d14c5d2SYehuda Sadeh }
5673d14c5d2SYehuda Sadeh 
5683d14c5d2SYehuda Sadeh static int ceph_x_create_authorizer(
5693d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac, int peer_type,
57074f1869fSAlex Elder 	struct ceph_auth_handshake *auth)
5713d14c5d2SYehuda Sadeh {
5723d14c5d2SYehuda Sadeh 	struct ceph_x_authorizer *au;
5733d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th;
5743d14c5d2SYehuda Sadeh 	int ret;
5753d14c5d2SYehuda Sadeh 
5763d14c5d2SYehuda Sadeh 	th = get_ticket_handler(ac, peer_type);
5773d14c5d2SYehuda Sadeh 	if (IS_ERR(th))
5783d14c5d2SYehuda Sadeh 		return PTR_ERR(th);
5793d14c5d2SYehuda Sadeh 
5803d14c5d2SYehuda Sadeh 	au = kzalloc(sizeof(*au), GFP_NOFS);
5813d14c5d2SYehuda Sadeh 	if (!au)
5823d14c5d2SYehuda Sadeh 		return -ENOMEM;
5833d14c5d2SYehuda Sadeh 
5843d14c5d2SYehuda Sadeh 	ret = ceph_x_build_authorizer(ac, th, au);
5853d14c5d2SYehuda Sadeh 	if (ret) {
5863d14c5d2SYehuda Sadeh 		kfree(au);
5873d14c5d2SYehuda Sadeh 		return ret;
5883d14c5d2SYehuda Sadeh 	}
5893d14c5d2SYehuda Sadeh 
59074f1869fSAlex Elder 	auth->authorizer = (struct ceph_authorizer *) au;
59174f1869fSAlex Elder 	auth->authorizer_buf = au->buf->vec.iov_base;
59274f1869fSAlex Elder 	auth->authorizer_buf_len = au->buf->vec.iov_len;
59374f1869fSAlex Elder 	auth->authorizer_reply_buf = au->reply_buf;
59474f1869fSAlex Elder 	auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
59533d07337SYan, Zheng 	auth->sign_message = ac->ops->sign_message;
59633d07337SYan, Zheng 	auth->check_message_signature = ac->ops->check_message_signature;
59774f1869fSAlex Elder 
5983d14c5d2SYehuda Sadeh 	return 0;
5993d14c5d2SYehuda Sadeh }
6003d14c5d2SYehuda Sadeh 
6010bed9b5cSSage Weil static int ceph_x_update_authorizer(
6020bed9b5cSSage Weil 	struct ceph_auth_client *ac, int peer_type,
6030bed9b5cSSage Weil 	struct ceph_auth_handshake *auth)
6040bed9b5cSSage Weil {
6050bed9b5cSSage Weil 	struct ceph_x_authorizer *au;
6060bed9b5cSSage Weil 	struct ceph_x_ticket_handler *th;
6070bed9b5cSSage Weil 
6080bed9b5cSSage Weil 	th = get_ticket_handler(ac, peer_type);
6090bed9b5cSSage Weil 	if (IS_ERR(th))
6100bed9b5cSSage Weil 		return PTR_ERR(th);
6110bed9b5cSSage Weil 
6120bed9b5cSSage Weil 	au = (struct ceph_x_authorizer *)auth->authorizer;
6130bed9b5cSSage Weil 	if (au->secret_id < th->secret_id) {
6140bed9b5cSSage Weil 		dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
6150bed9b5cSSage Weil 		     au->service, au->secret_id, th->secret_id);
6160bed9b5cSSage Weil 		return ceph_x_build_authorizer(ac, th, au);
6170bed9b5cSSage Weil 	}
6180bed9b5cSSage Weil 	return 0;
6190bed9b5cSSage Weil }
6200bed9b5cSSage Weil 
6213d14c5d2SYehuda Sadeh static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
6223d14c5d2SYehuda Sadeh 					  struct ceph_authorizer *a, size_t len)
6233d14c5d2SYehuda Sadeh {
6243d14c5d2SYehuda Sadeh 	struct ceph_x_authorizer *au = (void *)a;
6253d14c5d2SYehuda Sadeh 	int ret = 0;
6263d14c5d2SYehuda Sadeh 	struct ceph_x_authorize_reply reply;
627c27a3e4dSIlya Dryomov 	void *preply = &reply;
6283d14c5d2SYehuda Sadeh 	void *p = au->reply_buf;
6293d14c5d2SYehuda Sadeh 	void *end = p + sizeof(au->reply_buf);
6303d14c5d2SYehuda Sadeh 
631ae385eafSYan, Zheng 	ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply));
6323d14c5d2SYehuda Sadeh 	if (ret < 0)
6333d14c5d2SYehuda Sadeh 		return ret;
6343d14c5d2SYehuda Sadeh 	if (ret != sizeof(reply))
6353d14c5d2SYehuda Sadeh 		return -EPERM;
6363d14c5d2SYehuda Sadeh 
6373d14c5d2SYehuda Sadeh 	if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
6383d14c5d2SYehuda Sadeh 		ret = -EPERM;
6393d14c5d2SYehuda Sadeh 	else
6403d14c5d2SYehuda Sadeh 		ret = 0;
6413d14c5d2SYehuda Sadeh 	dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
6423d14c5d2SYehuda Sadeh 	     au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
6433d14c5d2SYehuda Sadeh 	return ret;
6443d14c5d2SYehuda Sadeh }
6453d14c5d2SYehuda Sadeh 
6463d14c5d2SYehuda Sadeh static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
6473d14c5d2SYehuda Sadeh 				      struct ceph_authorizer *a)
6483d14c5d2SYehuda Sadeh {
6493d14c5d2SYehuda Sadeh 	struct ceph_x_authorizer *au = (void *)a;
6503d14c5d2SYehuda Sadeh 
651cbf99a11SIlya Dryomov 	ceph_x_authorizer_cleanup(au);
6523d14c5d2SYehuda Sadeh 	kfree(au);
6533d14c5d2SYehuda Sadeh }
6543d14c5d2SYehuda Sadeh 
6553d14c5d2SYehuda Sadeh 
6563d14c5d2SYehuda Sadeh static void ceph_x_reset(struct ceph_auth_client *ac)
6573d14c5d2SYehuda Sadeh {
6583d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
6593d14c5d2SYehuda Sadeh 
6603d14c5d2SYehuda Sadeh 	dout("reset\n");
6613d14c5d2SYehuda Sadeh 	xi->starting = true;
6623d14c5d2SYehuda Sadeh 	xi->server_challenge = 0;
6633d14c5d2SYehuda Sadeh }
6643d14c5d2SYehuda Sadeh 
6653d14c5d2SYehuda Sadeh static void ceph_x_destroy(struct ceph_auth_client *ac)
6663d14c5d2SYehuda Sadeh {
6673d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi = ac->private;
6683d14c5d2SYehuda Sadeh 	struct rb_node *p;
6693d14c5d2SYehuda Sadeh 
6703d14c5d2SYehuda Sadeh 	dout("ceph_x_destroy %p\n", ac);
6713d14c5d2SYehuda Sadeh 	ceph_crypto_key_destroy(&xi->secret);
6723d14c5d2SYehuda Sadeh 
6733d14c5d2SYehuda Sadeh 	while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
6743d14c5d2SYehuda Sadeh 		struct ceph_x_ticket_handler *th =
6753d14c5d2SYehuda Sadeh 			rb_entry(p, struct ceph_x_ticket_handler, node);
6763d14c5d2SYehuda Sadeh 		remove_ticket_handler(ac, th);
6773d14c5d2SYehuda Sadeh 	}
6783d14c5d2SYehuda Sadeh 
679cbf99a11SIlya Dryomov 	ceph_x_authorizer_cleanup(&xi->auth_authorizer);
6803d14c5d2SYehuda Sadeh 
6813d14c5d2SYehuda Sadeh 	kfree(ac->private);
6823d14c5d2SYehuda Sadeh 	ac->private = NULL;
6833d14c5d2SYehuda Sadeh }
6843d14c5d2SYehuda Sadeh 
685187d131dSIlya Dryomov static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
6863d14c5d2SYehuda Sadeh {
6873d14c5d2SYehuda Sadeh 	struct ceph_x_ticket_handler *th;
6883d14c5d2SYehuda Sadeh 
6893d14c5d2SYehuda Sadeh 	th = get_ticket_handler(ac, peer_type);
6903d14c5d2SYehuda Sadeh 	if (!IS_ERR(th))
6916abe097dSIlya Dryomov 		th->have_key = false;
6923d14c5d2SYehuda Sadeh }
6933d14c5d2SYehuda Sadeh 
694187d131dSIlya Dryomov static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
695187d131dSIlya Dryomov 					 int peer_type)
696187d131dSIlya Dryomov {
697187d131dSIlya Dryomov 	/*
698187d131dSIlya Dryomov 	 * We are to invalidate a service ticket in the hopes of
699187d131dSIlya Dryomov 	 * getting a new, hopefully more valid, one.  But, we won't get
700187d131dSIlya Dryomov 	 * it unless our AUTH ticket is good, so invalidate AUTH ticket
701187d131dSIlya Dryomov 	 * as well, just in case.
702187d131dSIlya Dryomov 	 */
703187d131dSIlya Dryomov 	invalidate_ticket(ac, peer_type);
704187d131dSIlya Dryomov 	invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
705187d131dSIlya Dryomov }
706187d131dSIlya Dryomov 
70733d07337SYan, Zheng static int calcu_signature(struct ceph_x_authorizer *au,
70833d07337SYan, Zheng 			   struct ceph_msg *msg, __le64 *sig)
70933d07337SYan, Zheng {
71033d07337SYan, Zheng 	int ret;
71133d07337SYan, Zheng 	char tmp_enc[40];
71233d07337SYan, Zheng 	__le32 tmp[5] = {
713d7d5a007SIlya Dryomov 		cpu_to_le32(16), msg->hdr.crc, msg->footer.front_crc,
71433d07337SYan, Zheng 		msg->footer.middle_crc, msg->footer.data_crc,
71533d07337SYan, Zheng 	};
71633d07337SYan, Zheng 	ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp),
71733d07337SYan, Zheng 			     tmp_enc, sizeof(tmp_enc));
71833d07337SYan, Zheng 	if (ret < 0)
71933d07337SYan, Zheng 		return ret;
72033d07337SYan, Zheng 	*sig = *(__le64*)(tmp_enc + 4);
72133d07337SYan, Zheng 	return 0;
72233d07337SYan, Zheng }
72333d07337SYan, Zheng 
72433d07337SYan, Zheng static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
72533d07337SYan, Zheng 			       struct ceph_msg *msg)
72633d07337SYan, Zheng {
72733d07337SYan, Zheng 	int ret;
7284199b8eeSIlya Dryomov 
729a51983e4SIlya Dryomov 	if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
730a51983e4SIlya Dryomov 		return 0;
731a51983e4SIlya Dryomov 
73233d07337SYan, Zheng 	ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
73333d07337SYan, Zheng 			      msg, &msg->footer.sig);
73433d07337SYan, Zheng 	if (ret < 0)
73533d07337SYan, Zheng 		return ret;
73633d07337SYan, Zheng 	msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
73733d07337SYan, Zheng 	return 0;
73833d07337SYan, Zheng }
73933d07337SYan, Zheng 
74033d07337SYan, Zheng static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
74133d07337SYan, Zheng 					  struct ceph_msg *msg)
74233d07337SYan, Zheng {
74333d07337SYan, Zheng 	__le64 sig_check;
74433d07337SYan, Zheng 	int ret;
74533d07337SYan, Zheng 
746a51983e4SIlya Dryomov 	if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
747a51983e4SIlya Dryomov 		return 0;
748a51983e4SIlya Dryomov 
74933d07337SYan, Zheng 	ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
75033d07337SYan, Zheng 			      msg, &sig_check);
75133d07337SYan, Zheng 	if (ret < 0)
75233d07337SYan, Zheng 		return ret;
75333d07337SYan, Zheng 	if (sig_check == msg->footer.sig)
75433d07337SYan, Zheng 		return 0;
75533d07337SYan, Zheng 	if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
75633d07337SYan, Zheng 		dout("ceph_x_check_message_signature %p has signature %llx "
75733d07337SYan, Zheng 		     "expect %llx\n", msg, msg->footer.sig, sig_check);
75833d07337SYan, Zheng 	else
75933d07337SYan, Zheng 		dout("ceph_x_check_message_signature %p sender did not set "
76033d07337SYan, Zheng 		     "CEPH_MSG_FOOTER_SIGNED\n", msg);
76133d07337SYan, Zheng 	return -EBADMSG;
76233d07337SYan, Zheng }
7633d14c5d2SYehuda Sadeh 
7643d14c5d2SYehuda Sadeh static const struct ceph_auth_client_ops ceph_x_ops = {
7653d14c5d2SYehuda Sadeh 	.name = "x",
7663d14c5d2SYehuda Sadeh 	.is_authenticated = ceph_x_is_authenticated,
7673d14c5d2SYehuda Sadeh 	.should_authenticate = ceph_x_should_authenticate,
7683d14c5d2SYehuda Sadeh 	.build_request = ceph_x_build_request,
7693d14c5d2SYehuda Sadeh 	.handle_reply = ceph_x_handle_reply,
7703d14c5d2SYehuda Sadeh 	.create_authorizer = ceph_x_create_authorizer,
7710bed9b5cSSage Weil 	.update_authorizer = ceph_x_update_authorizer,
7723d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = ceph_x_verify_authorizer_reply,
7733d14c5d2SYehuda Sadeh 	.destroy_authorizer = ceph_x_destroy_authorizer,
7743d14c5d2SYehuda Sadeh 	.invalidate_authorizer = ceph_x_invalidate_authorizer,
7753d14c5d2SYehuda Sadeh 	.reset =  ceph_x_reset,
7763d14c5d2SYehuda Sadeh 	.destroy = ceph_x_destroy,
77733d07337SYan, Zheng 	.sign_message = ceph_x_sign_message,
77833d07337SYan, Zheng 	.check_message_signature = ceph_x_check_message_signature,
7793d14c5d2SYehuda Sadeh };
7803d14c5d2SYehuda Sadeh 
7813d14c5d2SYehuda Sadeh 
7823d14c5d2SYehuda Sadeh int ceph_x_init(struct ceph_auth_client *ac)
7833d14c5d2SYehuda Sadeh {
7843d14c5d2SYehuda Sadeh 	struct ceph_x_info *xi;
7853d14c5d2SYehuda Sadeh 	int ret;
7863d14c5d2SYehuda Sadeh 
7873d14c5d2SYehuda Sadeh 	dout("ceph_x_init %p\n", ac);
7883d14c5d2SYehuda Sadeh 	ret = -ENOMEM;
7893d14c5d2SYehuda Sadeh 	xi = kzalloc(sizeof(*xi), GFP_NOFS);
7903d14c5d2SYehuda Sadeh 	if (!xi)
7913d14c5d2SYehuda Sadeh 		goto out;
7923d14c5d2SYehuda Sadeh 
7933d14c5d2SYehuda Sadeh 	ret = -EINVAL;
7948323c3aaSTommi Virtanen 	if (!ac->key) {
7953d14c5d2SYehuda Sadeh 		pr_err("no secret set (for auth_x protocol)\n");
7963d14c5d2SYehuda Sadeh 		goto out_nomem;
7973d14c5d2SYehuda Sadeh 	}
7983d14c5d2SYehuda Sadeh 
7998323c3aaSTommi Virtanen 	ret = ceph_crypto_key_clone(&xi->secret, ac->key);
8008323c3aaSTommi Virtanen 	if (ret < 0) {
8018323c3aaSTommi Virtanen 		pr_err("cannot clone key: %d\n", ret);
8023d14c5d2SYehuda Sadeh 		goto out_nomem;
8038323c3aaSTommi Virtanen 	}
8043d14c5d2SYehuda Sadeh 
8053d14c5d2SYehuda Sadeh 	xi->starting = true;
8063d14c5d2SYehuda Sadeh 	xi->ticket_handlers = RB_ROOT;
8073d14c5d2SYehuda Sadeh 
8083d14c5d2SYehuda Sadeh 	ac->protocol = CEPH_AUTH_CEPHX;
8093d14c5d2SYehuda Sadeh 	ac->private = xi;
8103d14c5d2SYehuda Sadeh 	ac->ops = &ceph_x_ops;
8113d14c5d2SYehuda Sadeh 	return 0;
8123d14c5d2SYehuda Sadeh 
8133d14c5d2SYehuda Sadeh out_nomem:
8143d14c5d2SYehuda Sadeh 	kfree(xi);
8153d14c5d2SYehuda Sadeh out:
8163d14c5d2SYehuda Sadeh 	return ret;
8173d14c5d2SYehuda Sadeh }
8183d14c5d2SYehuda Sadeh 
8193d14c5d2SYehuda Sadeh 
820