blob: 61cccb93f6532993245277d663b7d2b770f4d4dd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weilec0994e2010-02-02 16:25:35 -08002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08004
5#include <linux/err.h>
6#include <linux/module.h>
7#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08009
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include <linux/ceph/decode.h>
11#include <linux/ceph/auth.h>
Ilya Dryomova51983e2015-10-28 23:52:06 +010012#include <linux/ceph/libceph.h>
Yan, Zheng33d07332014-11-04 16:33:37 +080013#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014
15#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080016#include "auth_x.h"
17#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080018
Sage Weilec0994e2010-02-02 16:25:35 -080019static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
20
21static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
22{
23 struct ceph_x_info *xi = ac->private;
24 int need;
25
26 ceph_x_validate_tickets(ac, &need);
27 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
28 ac->want_keys, need, xi->have_keys);
29 return (ac->want_keys & xi->have_keys) == ac->want_keys;
30}
31
Sage Weila41359f2010-05-25 15:39:06 -070032static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
33{
34 struct ceph_x_info *xi = ac->private;
35 int need;
36
37 ceph_x_validate_tickets(ac, &need);
38 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
39 ac->want_keys, need, xi->have_keys);
40 return need != 0;
41}
42
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010043static int ceph_x_encrypt_offset(void)
44{
45 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
46}
47
Sage Weil807c86e2010-03-15 15:52:17 -070048static int ceph_x_encrypt_buflen(int ilen)
49{
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010050 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070051}
52
Ilya Dryomovd03857c2016-12-02 16:35:07 +010053static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
54 int buf_len, int plaintext_len)
Sage Weilec0994e2010-02-02 16:25:35 -080055{
Ilya Dryomovd03857c2016-12-02 16:35:07 +010056 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
57 int ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080058 int ret;
59
Ilya Dryomovd03857c2016-12-02 16:35:07 +010060 hdr->struct_v = 1;
61 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
62
63 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
64 plaintext_len + sizeof(struct ceph_x_encrypt_header),
65 &ciphertext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080066 if (ret)
67 return ret;
Ilya Dryomovd03857c2016-12-02 16:35:07 +010068
69 ceph_encode_32(&buf, ciphertext_len);
70 return sizeof(u32) + ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080071}
72
Ilya Dryomovc571fe22018-07-26 18:05:43 +020073static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
74 int ciphertext_len)
75{
76 struct ceph_x_encrypt_header *hdr = p;
77 int plaintext_len;
78 int ret;
79
80 ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
81 &plaintext_len);
82 if (ret)
83 return ret;
84
85 if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
86 pr_err("%s bad magic\n", __func__);
87 return -EINVAL;
88 }
89
90 return plaintext_len - sizeof(*hdr);
91}
92
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010093static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -080094{
Ilya Dryomovc571fe22018-07-26 18:05:43 +020095 int ciphertext_len;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010096 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -080097
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010098 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
99 ceph_decode_need(p, end, ciphertext_len, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -0800100
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200101 ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
102 if (ret < 0)
Sage Weilec0994e2010-02-02 16:25:35 -0800103 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100104
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100105 *p += ciphertext_len;
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200106 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100107
108e_inval:
109 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800110}
111
112/*
113 * get existing (or insert new) ticket handler
114 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700115static struct ceph_x_ticket_handler *
116get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800117{
118 struct ceph_x_ticket_handler *th;
119 struct ceph_x_info *xi = ac->private;
120 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
121
122 while (*p) {
123 parent = *p;
124 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
125 if (service < th->service)
126 p = &(*p)->rb_left;
127 else if (service > th->service)
128 p = &(*p)->rb_right;
129 else
130 return th;
131 }
132
133 /* add it */
134 th = kzalloc(sizeof(*th), GFP_NOFS);
135 if (!th)
136 return ERR_PTR(-ENOMEM);
137 th->service = service;
138 rb_link_node(&th->node, parent, p);
139 rb_insert_color(&th->node, &xi->ticket_handlers);
140 return th;
141}
142
143static void remove_ticket_handler(struct ceph_auth_client *ac,
144 struct ceph_x_ticket_handler *th)
145{
146 struct ceph_x_info *xi = ac->private;
147
148 dout("remove_ticket_handler %p %d\n", th, th->service);
149 rb_erase(&th->node, &xi->ticket_handlers);
150 ceph_crypto_key_destroy(&th->session_key);
151 if (th->ticket_blob)
152 ceph_buffer_put(th->ticket_blob);
153 kfree(th);
154}
155
Ilya Dryomov597cda32014-09-08 17:25:34 +0400156static int process_one_ticket(struct ceph_auth_client *ac,
157 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400158 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400159{
160 struct ceph_x_info *xi = ac->private;
161 int type;
162 u8 tkt_struct_v, blob_struct_v;
163 struct ceph_x_ticket_handler *th;
164 void *dp, *dend;
165 int dlen;
166 char is_enc;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200167 struct timespec64 validity;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400168 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400169 void **ptp;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200170 struct ceph_crypto_key new_session_key = { 0 };
Ilya Dryomov597cda32014-09-08 17:25:34 +0400171 struct ceph_buffer *new_ticket_blob;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200172 time64_t new_expires, new_renew_after;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400173 u64 new_secret_id;
174 int ret;
175
176 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
177
178 type = ceph_decode_32(p);
179 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
180
181 tkt_struct_v = ceph_decode_8(p);
182 if (tkt_struct_v != 1)
183 goto bad;
184
185 th = get_ticket_handler(ac, type);
186 if (IS_ERR(th)) {
187 ret = PTR_ERR(th);
188 goto out;
189 }
190
191 /* blob for me */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100192 dp = *p + ceph_x_encrypt_offset();
193 ret = ceph_x_decrypt(secret, p, end);
194 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400195 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100196 dout(" decrypted %d bytes\n", ret);
197 dend = dp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400198
199 tkt_struct_v = ceph_decode_8(&dp);
200 if (tkt_struct_v != 1)
201 goto bad;
202
Ilya Dryomov597cda32014-09-08 17:25:34 +0400203 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
204 if (ret)
205 goto out;
206
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200207 ceph_decode_timespec64(&validity, dp);
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100208 dp += sizeof(struct ceph_timespec);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200209 new_expires = ktime_get_real_seconds() + validity.tv_sec;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400210 new_renew_after = new_expires - (validity.tv_sec / 4);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200211 dout(" expires=%llu renew_after=%llu\n", new_expires,
Ilya Dryomov597cda32014-09-08 17:25:34 +0400212 new_renew_after);
213
214 /* ticket blob for service */
215 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400216 if (is_enc) {
217 /* encrypted */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100218 tp = *p + ceph_x_encrypt_offset();
219 ret = ceph_x_decrypt(&th->session_key, p, end);
220 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400221 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100222 dout(" encrypted ticket, decrypted %d bytes\n", ret);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400223 ptp = &tp;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100224 tpend = tp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400225 } else {
226 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400227 ptp = p;
228 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400229 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400230 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400231 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400232 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
233 blob_struct_v = ceph_decode_8(ptp);
Ilya Dryomovd18a1242017-05-19 12:21:56 +0200234 if (blob_struct_v != 1)
235 goto bad;
236
Ilya Dryomove9226d72014-10-22 18:15:37 +0400237 new_secret_id = ceph_decode_64(ptp);
238 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400239 if (ret)
240 goto out;
241
242 /* all is well, update our ticket */
243 ceph_crypto_key_destroy(&th->session_key);
244 if (th->ticket_blob)
245 ceph_buffer_put(th->ticket_blob);
246 th->session_key = new_session_key;
247 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400248 th->secret_id = new_secret_id;
249 th->expires = new_expires;
250 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100251 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400252 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
253 type, ceph_entity_type_name(type), th->secret_id,
254 (int)th->ticket_blob->vec.iov_len);
255 xi->have_keys |= th->service;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200256 return 0;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400257
258bad:
259 ret = -EINVAL;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200260out:
261 ceph_crypto_key_destroy(&new_session_key);
262 return ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400263}
264
Sage Weilec0994e2010-02-02 16:25:35 -0800265static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
266 struct ceph_crypto_key *secret,
267 void *buf, void *end)
268{
Sage Weilec0994e2010-02-02 16:25:35 -0800269 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700270 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400271 u32 num;
272 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800273
Ilya Dryomov597cda32014-09-08 17:25:34 +0400274 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700275 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400276 return -EINVAL;
277
278 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800279 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400280
Sage Weilec0994e2010-02-02 16:25:35 -0800281 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400282 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800283 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400284 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800285 }
286
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400287 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800288
289bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400290 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800291}
292
Ilya Dryomov149cac42018-07-27 16:37:54 +0200293/*
294 * Encode and encrypt the second part (ceph_x_authorize_b) of the
295 * authorizer. The first part (ceph_x_authorize_a) should already be
296 * encoded.
297 */
298static int encrypt_authorizer(struct ceph_x_authorizer *au)
299{
300 struct ceph_x_authorize_a *msg_a;
301 struct ceph_x_authorize_b *msg_b;
302 void *p, *end;
303 int ret;
304
305 msg_a = au->buf->vec.iov_base;
306 WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
307 p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
308 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
309
310 msg_b = p + ceph_x_encrypt_offset();
311 msg_b->struct_v = 1;
312 msg_b->nonce = cpu_to_le64(au->nonce);
313
314 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
315 if (ret < 0)
316 return ret;
317
318 p += ret;
319 WARN_ON(p > end);
320 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
321
322 return 0;
323}
324
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100325static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
326{
327 ceph_crypto_key_destroy(&au->session_key);
328 if (au->buf) {
329 ceph_buffer_put(au->buf);
330 au->buf = NULL;
331 }
332}
333
Sage Weilec0994e2010-02-02 16:25:35 -0800334static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
335 struct ceph_x_ticket_handler *th,
336 struct ceph_x_authorizer *au)
337{
Sage Weil807c86e2010-03-15 15:52:17 -0700338 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800339 struct ceph_x_authorize_a *msg_a;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100340 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800341 int ret;
342 int ticket_blob_len =
343 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
344
345 dout("build_authorizer for %s %p\n",
346 ceph_entity_type_name(th->service), au);
347
Yan, Zhengae385ea2014-11-04 16:32:35 +0800348 ceph_crypto_key_destroy(&au->session_key);
349 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
350 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100351 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800352
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100353 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100354 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700355 dout(" need len %d\n", maxlen);
356 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800357 ceph_buffer_put(au->buf);
358 au->buf = NULL;
359 }
360 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700361 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800362 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100363 ret = -ENOMEM;
364 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800365 }
Sage Weilec0994e2010-02-02 16:25:35 -0800366 }
367 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700368 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800369
370 msg_a = au->buf->vec.iov_base;
371 msg_a->struct_v = 1;
372 msg_a->global_id = cpu_to_le64(ac->global_id);
373 msg_a->service_id = cpu_to_le32(th->service);
374 msg_a->ticket_blob.struct_v = 1;
375 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
376 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
377 if (ticket_blob_len) {
378 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
379 th->ticket_blob->vec.iov_len);
380 }
381 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
382 le64_to_cpu(msg_a->ticket_blob.secret_id));
383
Sage Weilec0994e2010-02-02 16:25:35 -0800384 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomov149cac42018-07-27 16:37:54 +0200385 ret = encrypt_authorizer(au);
386 if (ret) {
387 pr_err("failed to encrypt authorizer: %d", ret);
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100388 goto out_au;
Ilya Dryomov149cac42018-07-27 16:37:54 +0200389 }
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100390
Sage Weilec0994e2010-02-02 16:25:35 -0800391 dout(" built authorizer nonce %llx len %d\n", au->nonce,
392 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800393 return 0;
394
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100395out_au:
396 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800397 return ret;
398}
399
400static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
401 void **p, void *end)
402{
403 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
404 ceph_encode_8(p, 1);
405 ceph_encode_64(p, th->secret_id);
406 if (th->ticket_blob) {
407 const char *buf = th->ticket_blob->vec.iov_base;
408 u32 len = th->ticket_blob->vec.iov_len;
409
410 ceph_encode_32_safe(p, end, len, bad);
411 ceph_encode_copy_safe(p, end, buf, len, bad);
412 } else {
413 ceph_encode_32_safe(p, end, 0, bad);
414 }
415
416 return 0;
417bad:
418 return -ERANGE;
419}
420
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100421static bool need_key(struct ceph_x_ticket_handler *th)
422{
423 if (!th->have_key)
424 return true;
425
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200426 return ktime_get_real_seconds() >= th->renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100427}
428
429static bool have_key(struct ceph_x_ticket_handler *th)
430{
431 if (th->have_key) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200432 if (ktime_get_real_seconds() >= th->expires)
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100433 th->have_key = false;
434 }
435
436 return th->have_key;
437}
438
Sage Weilec0994e2010-02-02 16:25:35 -0800439static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
440{
441 int want = ac->want_keys;
442 struct ceph_x_info *xi = ac->private;
443 int service;
444
445 *pneed = ac->want_keys & ~(xi->have_keys);
446
447 for (service = 1; service <= want; service <<= 1) {
448 struct ceph_x_ticket_handler *th;
449
450 if (!(ac->want_keys & service))
451 continue;
452
453 if (*pneed & service)
454 continue;
455
456 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200457 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800458 *pneed |= service;
459 continue;
460 }
461
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100462 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800463 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100464 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800465 xi->have_keys &= ~service;
466 }
467}
468
Sage Weilec0994e2010-02-02 16:25:35 -0800469static int ceph_x_build_request(struct ceph_auth_client *ac,
470 void *buf, void *end)
471{
472 struct ceph_x_info *xi = ac->private;
473 int need;
474 struct ceph_x_request_header *head = buf;
475 int ret;
476 struct ceph_x_ticket_handler *th =
477 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
478
Dan Carpenterb5457872010-08-26 11:12:38 +0200479 if (IS_ERR(th))
480 return PTR_ERR(th);
481
Sage Weilec0994e2010-02-02 16:25:35 -0800482 ceph_x_validate_tickets(ac, &need);
483
484 dout("build_request want %x have %x need %x\n",
485 ac->want_keys, xi->have_keys, need);
486
487 if (need & CEPH_ENTITY_TYPE_AUTH) {
488 struct ceph_x_authenticate *auth = (void *)(head + 1);
489 void *p = auth + 1;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100490 void *enc_buf = xi->auth_authorizer.enc_buf;
491 struct ceph_x_challenge_blob *blob = enc_buf +
492 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800493 u64 *u;
494
495 if (p > end)
496 return -ERANGE;
497
498 dout(" get_auth_session_key\n");
499 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
500
501 /* encrypt and hash */
502 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100503 blob->client_challenge = auth->client_challenge;
504 blob->server_challenge = cpu_to_le64(xi->server_challenge);
505 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
506 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800507 if (ret < 0)
508 return ret;
509
510 auth->struct_v = 1;
511 auth->key = 0;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100512 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700513 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800514 dout(" server_challenge %llx client_challenge %llx key %llx\n",
515 xi->server_challenge, le64_to_cpu(auth->client_challenge),
516 le64_to_cpu(auth->key));
517
518 /* now encode the old ticket if exists */
519 ret = ceph_x_encode_ticket(th, &p, end);
520 if (ret < 0)
521 return ret;
522
523 return p - buf;
524 }
525
526 if (need) {
527 void *p = head + 1;
528 struct ceph_x_service_ticket_request *req;
529
530 if (p > end)
531 return -ERANGE;
532 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
533
Sage Weilec0994e2010-02-02 16:25:35 -0800534 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
535 if (ret)
536 return ret;
537 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
538 xi->auth_authorizer.buf->vec.iov_len);
539
540 req = p;
541 req->keys = cpu_to_le32(need);
542 p += sizeof(*req);
543 return p - buf;
544 }
545
546 return 0;
547}
548
549static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
550 void *buf, void *end)
551{
552 struct ceph_x_info *xi = ac->private;
553 struct ceph_x_reply_header *head = buf;
554 struct ceph_x_ticket_handler *th;
555 int len = end - buf;
556 int op;
557 int ret;
558
559 if (result)
560 return result; /* XXX hmm? */
561
562 if (xi->starting) {
563 /* it's a hello */
564 struct ceph_x_server_challenge *sc = buf;
565
566 if (len != sizeof(*sc))
567 return -EINVAL;
568 xi->server_challenge = le64_to_cpu(sc->server_challenge);
569 dout("handle_reply got server challenge %llx\n",
570 xi->server_challenge);
571 xi->starting = false;
572 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
573 return -EAGAIN;
574 }
575
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700576 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800577 result = le32_to_cpu(head->result);
578 dout("handle_reply op %d result %d\n", op, result);
579 switch (op) {
580 case CEPHX_GET_AUTH_SESSION_KEY:
581 /* verify auth key */
582 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
583 buf + sizeof(*head), end);
584 break;
585
586 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
587 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200588 if (IS_ERR(th))
589 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800590 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
591 buf + sizeof(*head), end);
592 break;
593
594 default:
595 return -EINVAL;
596 }
597 if (ret)
598 return ret;
599 if (ac->want_keys == xi->have_keys)
600 return 0;
601 return -EAGAIN;
602}
603
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200604static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
605{
606 struct ceph_x_authorizer *au = (void *)a;
607
608 ceph_x_authorizer_cleanup(au);
609 kfree(au);
610}
611
Sage Weilec0994e2010-02-02 16:25:35 -0800612static int ceph_x_create_authorizer(
613 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500614 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800615{
616 struct ceph_x_authorizer *au;
617 struct ceph_x_ticket_handler *th;
618 int ret;
619
620 th = get_ticket_handler(ac, peer_type);
621 if (IS_ERR(th))
622 return PTR_ERR(th);
623
624 au = kzalloc(sizeof(*au), GFP_NOFS);
625 if (!au)
626 return -ENOMEM;
627
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200628 au->base.destroy = ceph_x_destroy_authorizer;
629
Sage Weilec0994e2010-02-02 16:25:35 -0800630 ret = ceph_x_build_authorizer(ac, th, au);
631 if (ret) {
632 kfree(au);
633 return ret;
634 }
635
Alex Elder74f18692012-05-16 15:16:39 -0500636 auth->authorizer = (struct ceph_authorizer *) au;
637 auth->authorizer_buf = au->buf->vec.iov_base;
638 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100639 auth->authorizer_reply_buf = au->enc_buf;
640 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800641 auth->sign_message = ac->ops->sign_message;
642 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500643
Sage Weilec0994e2010-02-02 16:25:35 -0800644 return 0;
645}
646
Sage Weil0bed9b52013-03-25 10:26:01 -0700647static int ceph_x_update_authorizer(
648 struct ceph_auth_client *ac, int peer_type,
649 struct ceph_auth_handshake *auth)
650{
651 struct ceph_x_authorizer *au;
652 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700653
654 th = get_ticket_handler(ac, peer_type);
655 if (IS_ERR(th))
656 return PTR_ERR(th);
657
658 au = (struct ceph_x_authorizer *)auth->authorizer;
659 if (au->secret_id < th->secret_id) {
660 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
661 au->service, au->secret_id, th->secret_id);
662 return ceph_x_build_authorizer(ac, th, au);
663 }
664 return 0;
665}
666
Sage Weilec0994e2010-02-02 16:25:35 -0800667static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
Ilya Dryomov0dde5842016-12-02 16:35:09 +0100668 struct ceph_authorizer *a)
Sage Weilec0994e2010-02-02 16:25:35 -0800669{
670 struct ceph_x_authorizer *au = (void *)a;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100671 void *p = au->enc_buf;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100672 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
673 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800674
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100675 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
Sage Weilec0994e2010-02-02 16:25:35 -0800676 if (ret < 0)
677 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100678 if (ret != sizeof(*reply))
Sage Weilec0994e2010-02-02 16:25:35 -0800679 return -EPERM;
680
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100681 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
Sage Weilec0994e2010-02-02 16:25:35 -0800682 ret = -EPERM;
683 else
684 ret = 0;
685 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100686 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
Sage Weilec0994e2010-02-02 16:25:35 -0800687 return ret;
688}
689
Sage Weilec0994e2010-02-02 16:25:35 -0800690static void ceph_x_reset(struct ceph_auth_client *ac)
691{
692 struct ceph_x_info *xi = ac->private;
693
694 dout("reset\n");
695 xi->starting = true;
696 xi->server_challenge = 0;
697}
698
699static void ceph_x_destroy(struct ceph_auth_client *ac)
700{
701 struct ceph_x_info *xi = ac->private;
702 struct rb_node *p;
703
704 dout("ceph_x_destroy %p\n", ac);
705 ceph_crypto_key_destroy(&xi->secret);
706
707 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
708 struct ceph_x_ticket_handler *th =
709 rb_entry(p, struct ceph_x_ticket_handler, node);
710 remove_ticket_handler(ac, th);
711 }
712
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100713 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700714
Sage Weilec0994e2010-02-02 16:25:35 -0800715 kfree(ac->private);
716 ac->private = NULL;
717}
718
Ilya Dryomov187d1312016-01-14 17:31:51 +0100719static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800720{
721 struct ceph_x_ticket_handler *th;
722
723 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200724 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100725 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800726}
727
Ilya Dryomov187d1312016-01-14 17:31:51 +0100728static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
729 int peer_type)
730{
731 /*
732 * We are to invalidate a service ticket in the hopes of
733 * getting a new, hopefully more valid, one. But, we won't get
734 * it unless our AUTH ticket is good, so invalidate AUTH ticket
735 * as well, just in case.
736 */
737 invalidate_ticket(ac, peer_type);
738 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
739}
740
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100741static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
742 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800743{
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100744 void *enc_buf = au->enc_buf;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100745 struct {
746 __le32 len;
747 __le32 header_crc;
748 __le32 front_crc;
749 __le32 middle_crc;
750 __le32 data_crc;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100751 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100752 int ret;
753
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100754 sigblock->len = cpu_to_le32(4*sizeof(u32));
755 sigblock->header_crc = msg->hdr.crc;
756 sigblock->front_crc = msg->footer.front_crc;
757 sigblock->middle_crc = msg->footer.middle_crc;
758 sigblock->data_crc = msg->footer.data_crc;
759 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
760 sizeof(*sigblock));
Yan, Zheng33d07332014-11-04 16:33:37 +0800761 if (ret < 0)
762 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100763
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100764 *psig = *(__le64 *)(enc_buf + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800765 return 0;
766}
767
768static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
769 struct ceph_msg *msg)
770{
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100771 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800772 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100773
Ilya Dryomova51983e2015-10-28 23:52:06 +0100774 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
775 return 0;
776
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100777 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
778 msg, &sig);
779 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800780 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100781
782 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800783 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
784 return 0;
785}
786
787static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
788 struct ceph_msg *msg)
789{
790 __le64 sig_check;
791 int ret;
792
Ilya Dryomova51983e2015-10-28 23:52:06 +0100793 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
794 return 0;
795
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100796 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
797 msg, &sig_check);
798 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800799 return ret;
800 if (sig_check == msg->footer.sig)
801 return 0;
802 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
803 dout("ceph_x_check_message_signature %p has signature %llx "
804 "expect %llx\n", msg, msg->footer.sig, sig_check);
805 else
806 dout("ceph_x_check_message_signature %p sender did not set "
807 "CEPH_MSG_FOOTER_SIGNED\n", msg);
808 return -EBADMSG;
809}
Sage Weilec0994e2010-02-02 16:25:35 -0800810
811static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700812 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800813 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700814 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800815 .build_request = ceph_x_build_request,
816 .handle_reply = ceph_x_handle_reply,
817 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700818 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800819 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800820 .invalidate_authorizer = ceph_x_invalidate_authorizer,
821 .reset = ceph_x_reset,
822 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800823 .sign_message = ceph_x_sign_message,
824 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800825};
826
827
828int ceph_x_init(struct ceph_auth_client *ac)
829{
830 struct ceph_x_info *xi;
831 int ret;
832
833 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700834 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800835 xi = kzalloc(sizeof(*xi), GFP_NOFS);
836 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700837 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800838
Sage Weilec0994e2010-02-02 16:25:35 -0800839 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700840 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800841 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700842 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800843 }
844
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700845 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
846 if (ret < 0) {
847 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700848 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700849 }
Sage Weilec0994e2010-02-02 16:25:35 -0800850
851 xi->starting = true;
852 xi->ticket_handlers = RB_ROOT;
853
854 ac->protocol = CEPH_AUTH_CEPHX;
855 ac->private = xi;
856 ac->ops = &ceph_x_ops;
857 return 0;
858
Sage Weilb0930f82010-04-29 13:26:53 -0700859out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800860 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700861out:
Sage Weilec0994e2010-02-02 16:25:35 -0800862 return ret;
863}