blob: aeb47b867ccfe133b82f4b390b900e4aa297698b [file] [log] [blame]
Sage Weilec0994e2010-02-02 16:25:35 -08001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08003
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08008
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/decode.h>
10#include <linux/ceph/auth.h>
Ilya Dryomova51983e2015-10-28 23:52:06 +010011#include <linux/ceph/libceph.h>
Yan, Zheng33d07332014-11-04 16:33:37 +080012#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013
14#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080015#include "auth_x.h"
16#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080017
Sage Weilec0994e2010-02-02 16:25:35 -080018static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19
20static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
21{
22 struct ceph_x_info *xi = ac->private;
23 int need;
24
25 ceph_x_validate_tickets(ac, &need);
26 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
27 ac->want_keys, need, xi->have_keys);
28 return (ac->want_keys & xi->have_keys) == ac->want_keys;
29}
30
Sage Weila41359f2010-05-25 15:39:06 -070031static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
32{
33 struct ceph_x_info *xi = ac->private;
34 int need;
35
36 ceph_x_validate_tickets(ac, &need);
37 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
38 ac->want_keys, need, xi->have_keys);
39 return need != 0;
40}
41
Ilya Dryomov0548b822016-12-02 16:35:07 +010042static int ceph_x_encrypt_offset(void)
43{
44 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
45}
46
Sage Weil807c86e2010-03-15 15:52:17 -070047static int ceph_x_encrypt_buflen(int ilen)
48{
Ilya Dryomov0548b822016-12-02 16:35:07 +010049 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070050}
51
Ilya Dryomov717a1452016-12-02 16:35:07 +010052static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
53 int buf_len, int plaintext_len)
Sage Weilec0994e2010-02-02 16:25:35 -080054{
Ilya Dryomov717a1452016-12-02 16:35:07 +010055 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
56 int ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080057 int ret;
58
Ilya Dryomov717a1452016-12-02 16:35:07 +010059 hdr->struct_v = 1;
60 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
61
62 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
63 plaintext_len + sizeof(struct ceph_x_encrypt_header),
64 &ciphertext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080065 if (ret)
66 return ret;
Ilya Dryomov717a1452016-12-02 16:35:07 +010067
68 ceph_encode_32(&buf, ciphertext_len);
69 return sizeof(u32) + ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080070}
71
Ilya Dryomov9da9bb42018-07-26 18:05:43 +020072static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
73 int ciphertext_len)
74{
75 struct ceph_x_encrypt_header *hdr = p;
76 int plaintext_len;
77 int ret;
78
79 ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
80 &plaintext_len);
81 if (ret)
82 return ret;
83
84 if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
85 pr_err("%s bad magic\n", __func__);
86 return -EINVAL;
87 }
88
89 return plaintext_len - sizeof(*hdr);
90}
91
Ilya Dryomov2982b9c2016-12-02 16:35:08 +010092static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -080093{
Ilya Dryomov9da9bb42018-07-26 18:05:43 +020094 int ciphertext_len;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +010095 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -080096
Ilya Dryomov2982b9c2016-12-02 16:35:08 +010097 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
98 ceph_decode_need(p, end, ciphertext_len, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -080099
Ilya Dryomov9da9bb42018-07-26 18:05:43 +0200100 ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
101 if (ret < 0)
Sage Weilec0994e2010-02-02 16:25:35 -0800102 return ret;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100103
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100104 *p += ciphertext_len;
Ilya Dryomov9da9bb42018-07-26 18:05:43 +0200105 return ret;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100106
107e_inval:
108 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800109}
110
111/*
112 * get existing (or insert new) ticket handler
113 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700114static struct ceph_x_ticket_handler *
115get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800116{
117 struct ceph_x_ticket_handler *th;
118 struct ceph_x_info *xi = ac->private;
119 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
120
121 while (*p) {
122 parent = *p;
123 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
124 if (service < th->service)
125 p = &(*p)->rb_left;
126 else if (service > th->service)
127 p = &(*p)->rb_right;
128 else
129 return th;
130 }
131
132 /* add it */
133 th = kzalloc(sizeof(*th), GFP_NOFS);
134 if (!th)
135 return ERR_PTR(-ENOMEM);
136 th->service = service;
137 rb_link_node(&th->node, parent, p);
138 rb_insert_color(&th->node, &xi->ticket_handlers);
139 return th;
140}
141
142static void remove_ticket_handler(struct ceph_auth_client *ac,
143 struct ceph_x_ticket_handler *th)
144{
145 struct ceph_x_info *xi = ac->private;
146
147 dout("remove_ticket_handler %p %d\n", th, th->service);
148 rb_erase(&th->node, &xi->ticket_handlers);
149 ceph_crypto_key_destroy(&th->session_key);
150 if (th->ticket_blob)
151 ceph_buffer_put(th->ticket_blob);
152 kfree(th);
153}
154
Ilya Dryomov597cda32014-09-08 17:25:34 +0400155static int process_one_ticket(struct ceph_auth_client *ac,
156 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400157 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400158{
159 struct ceph_x_info *xi = ac->private;
160 int type;
161 u8 tkt_struct_v, blob_struct_v;
162 struct ceph_x_ticket_handler *th;
163 void *dp, *dend;
164 int dlen;
165 char is_enc;
166 struct timespec validity;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400167 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400168 void **ptp;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400169 struct ceph_crypto_key new_session_key;
170 struct ceph_buffer *new_ticket_blob;
171 unsigned long new_expires, new_renew_after;
172 u64 new_secret_id;
173 int ret;
174
175 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
176
177 type = ceph_decode_32(p);
178 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
179
180 tkt_struct_v = ceph_decode_8(p);
181 if (tkt_struct_v != 1)
182 goto bad;
183
184 th = get_ticket_handler(ac, type);
185 if (IS_ERR(th)) {
186 ret = PTR_ERR(th);
187 goto out;
188 }
189
190 /* blob for me */
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100191 dp = *p + ceph_x_encrypt_offset();
192 ret = ceph_x_decrypt(secret, p, end);
193 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400194 goto out;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100195 dout(" decrypted %d bytes\n", ret);
196 dend = dp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400197
198 tkt_struct_v = ceph_decode_8(&dp);
199 if (tkt_struct_v != 1)
200 goto bad;
201
Ilya Dryomov597cda32014-09-08 17:25:34 +0400202 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
203 if (ret)
204 goto out;
205
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100206 ceph_decode_timespec(&validity, dp);
207 dp += sizeof(struct ceph_timespec);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400208 new_expires = get_seconds() + validity.tv_sec;
209 new_renew_after = new_expires - (validity.tv_sec / 4);
210 dout(" expires=%lu renew_after=%lu\n", new_expires,
211 new_renew_after);
212
213 /* ticket blob for service */
214 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400215 if (is_enc) {
216 /* encrypted */
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100217 tp = *p + ceph_x_encrypt_offset();
218 ret = ceph_x_decrypt(&th->session_key, p, end);
219 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400220 goto out;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100221 dout(" encrypted ticket, decrypted %d bytes\n", ret);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400222 ptp = &tp;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100223 tpend = tp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400224 } else {
225 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400226 ptp = p;
227 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400228 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400229 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400230 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400231 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
232 blob_struct_v = ceph_decode_8(ptp);
233 new_secret_id = ceph_decode_64(ptp);
234 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400235 if (ret)
236 goto out;
237
238 /* all is well, update our ticket */
239 ceph_crypto_key_destroy(&th->session_key);
240 if (th->ticket_blob)
241 ceph_buffer_put(th->ticket_blob);
242 th->session_key = new_session_key;
243 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400244 th->secret_id = new_secret_id;
245 th->expires = new_expires;
246 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100247 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400248 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
249 type, ceph_entity_type_name(type), th->secret_id,
250 (int)th->ticket_blob->vec.iov_len);
251 xi->have_keys |= th->service;
252
253out:
254 return ret;
255
256bad:
257 ret = -EINVAL;
258 goto out;
259}
260
Sage Weilec0994e2010-02-02 16:25:35 -0800261static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
262 struct ceph_crypto_key *secret,
263 void *buf, void *end)
264{
Sage Weilec0994e2010-02-02 16:25:35 -0800265 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700266 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400267 u32 num;
268 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800269
Ilya Dryomov597cda32014-09-08 17:25:34 +0400270 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700271 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400272 return -EINVAL;
273
274 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800275 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400276
Sage Weilec0994e2010-02-02 16:25:35 -0800277 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400278 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800279 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400280 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800281 }
282
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400283 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800284
285bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400286 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800287}
288
Ilya Dryomov2e901ea2018-07-27 16:37:54 +0200289/*
290 * Encode and encrypt the second part (ceph_x_authorize_b) of the
291 * authorizer. The first part (ceph_x_authorize_a) should already be
292 * encoded.
293 */
294static int encrypt_authorizer(struct ceph_x_authorizer *au)
295{
296 struct ceph_x_authorize_a *msg_a;
297 struct ceph_x_authorize_b *msg_b;
298 void *p, *end;
299 int ret;
300
301 msg_a = au->buf->vec.iov_base;
302 WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
303 p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
304 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
305
306 msg_b = p + ceph_x_encrypt_offset();
307 msg_b->struct_v = 1;
308 msg_b->nonce = cpu_to_le64(au->nonce);
309
310 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
311 if (ret < 0)
312 return ret;
313
314 p += ret;
315 WARN_ON(p > end);
316 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
317
318 return 0;
319}
320
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100321static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
322{
323 ceph_crypto_key_destroy(&au->session_key);
324 if (au->buf) {
325 ceph_buffer_put(au->buf);
326 au->buf = NULL;
327 }
328}
329
Sage Weilec0994e2010-02-02 16:25:35 -0800330static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
331 struct ceph_x_ticket_handler *th,
332 struct ceph_x_authorizer *au)
333{
Sage Weil807c86e2010-03-15 15:52:17 -0700334 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800335 struct ceph_x_authorize_a *msg_a;
Ilya Dryomov717a1452016-12-02 16:35:07 +0100336 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800337 int ret;
338 int ticket_blob_len =
339 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
340
341 dout("build_authorizer for %s %p\n",
342 ceph_entity_type_name(th->service), au);
343
Yan, Zhengae385ea2014-11-04 16:32:35 +0800344 ceph_crypto_key_destroy(&au->session_key);
345 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
346 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100347 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800348
Ilya Dryomov2e62bf3c2016-12-02 16:35:06 +0100349 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomov717a1452016-12-02 16:35:07 +0100350 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700351 dout(" need len %d\n", maxlen);
352 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800353 ceph_buffer_put(au->buf);
354 au->buf = NULL;
355 }
356 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700357 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800358 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100359 ret = -ENOMEM;
360 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800361 }
Sage Weilec0994e2010-02-02 16:25:35 -0800362 }
363 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700364 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800365
366 msg_a = au->buf->vec.iov_base;
367 msg_a->struct_v = 1;
368 msg_a->global_id = cpu_to_le64(ac->global_id);
369 msg_a->service_id = cpu_to_le32(th->service);
370 msg_a->ticket_blob.struct_v = 1;
371 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
372 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
373 if (ticket_blob_len) {
374 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
375 th->ticket_blob->vec.iov_len);
376 }
377 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
378 le64_to_cpu(msg_a->ticket_blob.secret_id));
379
Sage Weilec0994e2010-02-02 16:25:35 -0800380 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomov2e901ea2018-07-27 16:37:54 +0200381 ret = encrypt_authorizer(au);
382 if (ret) {
383 pr_err("failed to encrypt authorizer: %d", ret);
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100384 goto out_au;
Ilya Dryomov2e901ea2018-07-27 16:37:54 +0200385 }
Ilya Dryomov2e62bf3c2016-12-02 16:35:06 +0100386
Sage Weilec0994e2010-02-02 16:25:35 -0800387 dout(" built authorizer nonce %llx len %d\n", au->nonce,
388 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800389 return 0;
390
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100391out_au:
392 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800393 return ret;
394}
395
396static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
397 void **p, void *end)
398{
399 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
400 ceph_encode_8(p, 1);
401 ceph_encode_64(p, th->secret_id);
402 if (th->ticket_blob) {
403 const char *buf = th->ticket_blob->vec.iov_base;
404 u32 len = th->ticket_blob->vec.iov_len;
405
406 ceph_encode_32_safe(p, end, len, bad);
407 ceph_encode_copy_safe(p, end, buf, len, bad);
408 } else {
409 ceph_encode_32_safe(p, end, 0, bad);
410 }
411
412 return 0;
413bad:
414 return -ERANGE;
415}
416
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100417static bool need_key(struct ceph_x_ticket_handler *th)
418{
419 if (!th->have_key)
420 return true;
421
422 return get_seconds() >= th->renew_after;
423}
424
425static bool have_key(struct ceph_x_ticket_handler *th)
426{
427 if (th->have_key) {
428 if (get_seconds() >= th->expires)
429 th->have_key = false;
430 }
431
432 return th->have_key;
433}
434
Sage Weilec0994e2010-02-02 16:25:35 -0800435static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
436{
437 int want = ac->want_keys;
438 struct ceph_x_info *xi = ac->private;
439 int service;
440
441 *pneed = ac->want_keys & ~(xi->have_keys);
442
443 for (service = 1; service <= want; service <<= 1) {
444 struct ceph_x_ticket_handler *th;
445
446 if (!(ac->want_keys & service))
447 continue;
448
449 if (*pneed & service)
450 continue;
451
452 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200453 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800454 *pneed |= service;
455 continue;
456 }
457
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100458 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800459 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100460 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800461 xi->have_keys &= ~service;
462 }
463}
464
Sage Weilec0994e2010-02-02 16:25:35 -0800465static int ceph_x_build_request(struct ceph_auth_client *ac,
466 void *buf, void *end)
467{
468 struct ceph_x_info *xi = ac->private;
469 int need;
470 struct ceph_x_request_header *head = buf;
471 int ret;
472 struct ceph_x_ticket_handler *th =
473 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
474
Dan Carpenterb5457872010-08-26 11:12:38 +0200475 if (IS_ERR(th))
476 return PTR_ERR(th);
477
Sage Weilec0994e2010-02-02 16:25:35 -0800478 ceph_x_validate_tickets(ac, &need);
479
480 dout("build_request want %x have %x need %x\n",
481 ac->want_keys, xi->have_keys, need);
482
483 if (need & CEPH_ENTITY_TYPE_AUTH) {
484 struct ceph_x_authenticate *auth = (void *)(head + 1);
485 void *p = auth + 1;
Ilya Dryomov717a1452016-12-02 16:35:07 +0100486 void *enc_buf = xi->auth_authorizer.enc_buf;
487 struct ceph_x_challenge_blob *blob = enc_buf +
488 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800489 u64 *u;
490
491 if (p > end)
492 return -ERANGE;
493
494 dout(" get_auth_session_key\n");
495 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
496
497 /* encrypt and hash */
498 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomov717a1452016-12-02 16:35:07 +0100499 blob->client_challenge = auth->client_challenge;
500 blob->server_challenge = cpu_to_le64(xi->server_challenge);
501 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
502 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800503 if (ret < 0)
504 return ret;
505
506 auth->struct_v = 1;
507 auth->key = 0;
Ilya Dryomov717a1452016-12-02 16:35:07 +0100508 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700509 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800510 dout(" server_challenge %llx client_challenge %llx key %llx\n",
511 xi->server_challenge, le64_to_cpu(auth->client_challenge),
512 le64_to_cpu(auth->key));
513
514 /* now encode the old ticket if exists */
515 ret = ceph_x_encode_ticket(th, &p, end);
516 if (ret < 0)
517 return ret;
518
519 return p - buf;
520 }
521
522 if (need) {
523 void *p = head + 1;
524 struct ceph_x_service_ticket_request *req;
525
526 if (p > end)
527 return -ERANGE;
528 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
529
Sage Weilec0994e2010-02-02 16:25:35 -0800530 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
531 if (ret)
532 return ret;
533 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
534 xi->auth_authorizer.buf->vec.iov_len);
535
536 req = p;
537 req->keys = cpu_to_le32(need);
538 p += sizeof(*req);
539 return p - buf;
540 }
541
542 return 0;
543}
544
545static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
546 void *buf, void *end)
547{
548 struct ceph_x_info *xi = ac->private;
549 struct ceph_x_reply_header *head = buf;
550 struct ceph_x_ticket_handler *th;
551 int len = end - buf;
552 int op;
553 int ret;
554
555 if (result)
556 return result; /* XXX hmm? */
557
558 if (xi->starting) {
559 /* it's a hello */
560 struct ceph_x_server_challenge *sc = buf;
561
562 if (len != sizeof(*sc))
563 return -EINVAL;
564 xi->server_challenge = le64_to_cpu(sc->server_challenge);
565 dout("handle_reply got server challenge %llx\n",
566 xi->server_challenge);
567 xi->starting = false;
568 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
569 return -EAGAIN;
570 }
571
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700572 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800573 result = le32_to_cpu(head->result);
574 dout("handle_reply op %d result %d\n", op, result);
575 switch (op) {
576 case CEPHX_GET_AUTH_SESSION_KEY:
577 /* verify auth key */
578 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
579 buf + sizeof(*head), end);
580 break;
581
582 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
583 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200584 if (IS_ERR(th))
585 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800586 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
587 buf + sizeof(*head), end);
588 break;
589
590 default:
591 return -EINVAL;
592 }
593 if (ret)
594 return ret;
595 if (ac->want_keys == xi->have_keys)
596 return 0;
597 return -EAGAIN;
598}
599
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200600static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
601{
602 struct ceph_x_authorizer *au = (void *)a;
603
604 ceph_x_authorizer_cleanup(au);
605 kfree(au);
606}
607
Sage Weilec0994e2010-02-02 16:25:35 -0800608static int ceph_x_create_authorizer(
609 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500610 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800611{
612 struct ceph_x_authorizer *au;
613 struct ceph_x_ticket_handler *th;
614 int ret;
615
616 th = get_ticket_handler(ac, peer_type);
617 if (IS_ERR(th))
618 return PTR_ERR(th);
619
620 au = kzalloc(sizeof(*au), GFP_NOFS);
621 if (!au)
622 return -ENOMEM;
623
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200624 au->base.destroy = ceph_x_destroy_authorizer;
625
Sage Weilec0994e2010-02-02 16:25:35 -0800626 ret = ceph_x_build_authorizer(ac, th, au);
627 if (ret) {
628 kfree(au);
629 return ret;
630 }
631
Alex Elder74f18692012-05-16 15:16:39 -0500632 auth->authorizer = (struct ceph_authorizer *) au;
633 auth->authorizer_buf = au->buf->vec.iov_base;
634 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov788a0bb2016-12-02 16:35:07 +0100635 auth->authorizer_reply_buf = au->enc_buf;
636 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800637 auth->sign_message = ac->ops->sign_message;
638 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500639
Sage Weilec0994e2010-02-02 16:25:35 -0800640 return 0;
641}
642
Sage Weil0bed9b52013-03-25 10:26:01 -0700643static int ceph_x_update_authorizer(
644 struct ceph_auth_client *ac, int peer_type,
645 struct ceph_auth_handshake *auth)
646{
647 struct ceph_x_authorizer *au;
648 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700649
650 th = get_ticket_handler(ac, peer_type);
651 if (IS_ERR(th))
652 return PTR_ERR(th);
653
654 au = (struct ceph_x_authorizer *)auth->authorizer;
655 if (au->secret_id < th->secret_id) {
656 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
657 au->service, au->secret_id, th->secret_id);
658 return ceph_x_build_authorizer(ac, th, au);
659 }
660 return 0;
661}
662
Sage Weilec0994e2010-02-02 16:25:35 -0800663static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
Ilya Dryomov9041d1e2016-12-02 16:35:09 +0100664 struct ceph_authorizer *a)
Sage Weilec0994e2010-02-02 16:25:35 -0800665{
666 struct ceph_x_authorizer *au = (void *)a;
Ilya Dryomov788a0bb2016-12-02 16:35:07 +0100667 void *p = au->enc_buf;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100668 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
669 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800670
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100671 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
Sage Weilec0994e2010-02-02 16:25:35 -0800672 if (ret < 0)
673 return ret;
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100674 if (ret != sizeof(*reply))
Sage Weilec0994e2010-02-02 16:25:35 -0800675 return -EPERM;
676
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100677 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
Sage Weilec0994e2010-02-02 16:25:35 -0800678 ret = -EPERM;
679 else
680 ret = 0;
681 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
Ilya Dryomov2982b9c2016-12-02 16:35:08 +0100682 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
Sage Weilec0994e2010-02-02 16:25:35 -0800683 return ret;
684}
685
Sage Weilec0994e2010-02-02 16:25:35 -0800686static void ceph_x_reset(struct ceph_auth_client *ac)
687{
688 struct ceph_x_info *xi = ac->private;
689
690 dout("reset\n");
691 xi->starting = true;
692 xi->server_challenge = 0;
693}
694
695static void ceph_x_destroy(struct ceph_auth_client *ac)
696{
697 struct ceph_x_info *xi = ac->private;
698 struct rb_node *p;
699
700 dout("ceph_x_destroy %p\n", ac);
701 ceph_crypto_key_destroy(&xi->secret);
702
703 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
704 struct ceph_x_ticket_handler *th =
705 rb_entry(p, struct ceph_x_ticket_handler, node);
706 remove_ticket_handler(ac, th);
707 }
708
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100709 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700710
Sage Weilec0994e2010-02-02 16:25:35 -0800711 kfree(ac->private);
712 ac->private = NULL;
713}
714
Ilya Dryomov187d1312016-01-14 17:31:51 +0100715static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800716{
717 struct ceph_x_ticket_handler *th;
718
719 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200720 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100721 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800722}
723
Ilya Dryomov187d1312016-01-14 17:31:51 +0100724static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
725 int peer_type)
726{
727 /*
728 * We are to invalidate a service ticket in the hopes of
729 * getting a new, hopefully more valid, one. But, we won't get
730 * it unless our AUTH ticket is good, so invalidate AUTH ticket
731 * as well, just in case.
732 */
733 invalidate_ticket(ac, peer_type);
734 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
735}
736
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100737static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
738 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800739{
Ilya Dryomov717a1452016-12-02 16:35:07 +0100740 void *enc_buf = au->enc_buf;
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100741 struct {
742 __le32 len;
743 __le32 header_crc;
744 __le32 front_crc;
745 __le32 middle_crc;
746 __le32 data_crc;
Ilya Dryomov717a1452016-12-02 16:35:07 +0100747 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100748 int ret;
749
Ilya Dryomov717a1452016-12-02 16:35:07 +0100750 sigblock->len = cpu_to_le32(4*sizeof(u32));
751 sigblock->header_crc = msg->hdr.crc;
752 sigblock->front_crc = msg->footer.front_crc;
753 sigblock->middle_crc = msg->footer.middle_crc;
754 sigblock->data_crc = msg->footer.data_crc;
755 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
756 sizeof(*sigblock));
Yan, Zheng33d07332014-11-04 16:33:37 +0800757 if (ret < 0)
758 return ret;
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100759
Ilya Dryomov717a1452016-12-02 16:35:07 +0100760 *psig = *(__le64 *)(enc_buf + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800761 return 0;
762}
763
764static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
765 struct ceph_msg *msg)
766{
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100767 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800768 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100769
Ilya Dryomova51983e2015-10-28 23:52:06 +0100770 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
771 return 0;
772
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100773 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
774 msg, &sig);
775 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800776 return ret;
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100777
778 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800779 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
780 return 0;
781}
782
783static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
784 struct ceph_msg *msg)
785{
786 __le64 sig_check;
787 int ret;
788
Ilya Dryomova51983e2015-10-28 23:52:06 +0100789 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
790 return 0;
791
Ilya Dryomov6e371f92016-12-02 16:35:07 +0100792 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
793 msg, &sig_check);
794 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800795 return ret;
796 if (sig_check == msg->footer.sig)
797 return 0;
798 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
799 dout("ceph_x_check_message_signature %p has signature %llx "
800 "expect %llx\n", msg, msg->footer.sig, sig_check);
801 else
802 dout("ceph_x_check_message_signature %p sender did not set "
803 "CEPH_MSG_FOOTER_SIGNED\n", msg);
804 return -EBADMSG;
805}
Sage Weilec0994e2010-02-02 16:25:35 -0800806
807static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700808 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800809 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700810 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800811 .build_request = ceph_x_build_request,
812 .handle_reply = ceph_x_handle_reply,
813 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700814 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800815 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800816 .invalidate_authorizer = ceph_x_invalidate_authorizer,
817 .reset = ceph_x_reset,
818 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800819 .sign_message = ceph_x_sign_message,
820 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800821};
822
823
824int ceph_x_init(struct ceph_auth_client *ac)
825{
826 struct ceph_x_info *xi;
827 int ret;
828
829 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700830 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800831 xi = kzalloc(sizeof(*xi), GFP_NOFS);
832 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700833 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800834
Sage Weilec0994e2010-02-02 16:25:35 -0800835 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700836 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800837 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700838 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800839 }
840
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700841 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
842 if (ret < 0) {
843 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700844 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700845 }
Sage Weilec0994e2010-02-02 16:25:35 -0800846
847 xi->starting = true;
848 xi->ticket_handlers = RB_ROOT;
849
850 ac->protocol = CEPH_AUTH_CEPHX;
851 ac->private = xi;
852 ac->ops = &ceph_x_ops;
853 return 0;
854
Sage Weilb0930f82010-04-29 13:26:53 -0700855out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800856 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700857out:
Sage Weilec0994e2010-02-02 16:25:35 -0800858 return ret;
859}
860
861