blob: 2034fb9266700e6b456b141db9b5d264aed77d62 [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 Dryomov55d9cc82016-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 Dryomov55d9cc82016-12-02 16:35:07 +010049 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070050}
51
Ilya Dryomovd03857c2016-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 Dryomovd03857c2016-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 Dryomovd03857c2016-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 Dryomovd03857c2016-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 Dryomove15fd0a2016-12-02 16:35:08 +010072static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -080073{
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010074 struct ceph_x_encrypt_header *hdr = *p + sizeof(u32);
75 int ciphertext_len, plaintext_len;
76 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -080077
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010078 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
79 ceph_decode_need(p, end, ciphertext_len, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -080080
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010081 ret = ceph_crypt(secret, false, *p, end - *p, ciphertext_len,
82 &plaintext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080083 if (ret)
84 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010085
86 if (hdr->struct_v != 1 || le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC)
Sage Weilec0994e2010-02-02 16:25:35 -080087 return -EPERM;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010088
89 *p += ciphertext_len;
90 return plaintext_len - sizeof(struct ceph_x_encrypt_header);
91
92e_inval:
93 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -080094}
95
96/*
97 * get existing (or insert new) ticket handler
98 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -070099static struct ceph_x_ticket_handler *
100get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800101{
102 struct ceph_x_ticket_handler *th;
103 struct ceph_x_info *xi = ac->private;
104 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
105
106 while (*p) {
107 parent = *p;
108 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
109 if (service < th->service)
110 p = &(*p)->rb_left;
111 else if (service > th->service)
112 p = &(*p)->rb_right;
113 else
114 return th;
115 }
116
117 /* add it */
118 th = kzalloc(sizeof(*th), GFP_NOFS);
119 if (!th)
120 return ERR_PTR(-ENOMEM);
121 th->service = service;
122 rb_link_node(&th->node, parent, p);
123 rb_insert_color(&th->node, &xi->ticket_handlers);
124 return th;
125}
126
127static void remove_ticket_handler(struct ceph_auth_client *ac,
128 struct ceph_x_ticket_handler *th)
129{
130 struct ceph_x_info *xi = ac->private;
131
132 dout("remove_ticket_handler %p %d\n", th, th->service);
133 rb_erase(&th->node, &xi->ticket_handlers);
134 ceph_crypto_key_destroy(&th->session_key);
135 if (th->ticket_blob)
136 ceph_buffer_put(th->ticket_blob);
137 kfree(th);
138}
139
Ilya Dryomov597cda32014-09-08 17:25:34 +0400140static int process_one_ticket(struct ceph_auth_client *ac,
141 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400142 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400143{
144 struct ceph_x_info *xi = ac->private;
145 int type;
146 u8 tkt_struct_v, blob_struct_v;
147 struct ceph_x_ticket_handler *th;
148 void *dp, *dend;
149 int dlen;
150 char is_enc;
151 struct timespec validity;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400152 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400153 void **ptp;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400154 struct ceph_crypto_key new_session_key;
155 struct ceph_buffer *new_ticket_blob;
156 unsigned long new_expires, new_renew_after;
157 u64 new_secret_id;
158 int ret;
159
160 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
161
162 type = ceph_decode_32(p);
163 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
164
165 tkt_struct_v = ceph_decode_8(p);
166 if (tkt_struct_v != 1)
167 goto bad;
168
169 th = get_ticket_handler(ac, type);
170 if (IS_ERR(th)) {
171 ret = PTR_ERR(th);
172 goto out;
173 }
174
175 /* blob for me */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100176 dp = *p + ceph_x_encrypt_offset();
177 ret = ceph_x_decrypt(secret, p, end);
178 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400179 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100180 dout(" decrypted %d bytes\n", ret);
181 dend = dp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400182
183 tkt_struct_v = ceph_decode_8(&dp);
184 if (tkt_struct_v != 1)
185 goto bad;
186
Ilya Dryomov597cda32014-09-08 17:25:34 +0400187 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
188 if (ret)
189 goto out;
190
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100191 ceph_decode_timespec(&validity, dp);
192 dp += sizeof(struct ceph_timespec);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400193 new_expires = get_seconds() + validity.tv_sec;
194 new_renew_after = new_expires - (validity.tv_sec / 4);
195 dout(" expires=%lu renew_after=%lu\n", new_expires,
196 new_renew_after);
197
198 /* ticket blob for service */
199 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400200 if (is_enc) {
201 /* encrypted */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100202 tp = *p + ceph_x_encrypt_offset();
203 ret = ceph_x_decrypt(&th->session_key, p, end);
204 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400205 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100206 dout(" encrypted ticket, decrypted %d bytes\n", ret);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400207 ptp = &tp;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100208 tpend = tp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400209 } else {
210 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400211 ptp = p;
212 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400213 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400214 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400215 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400216 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
217 blob_struct_v = ceph_decode_8(ptp);
218 new_secret_id = ceph_decode_64(ptp);
219 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400220 if (ret)
221 goto out;
222
223 /* all is well, update our ticket */
224 ceph_crypto_key_destroy(&th->session_key);
225 if (th->ticket_blob)
226 ceph_buffer_put(th->ticket_blob);
227 th->session_key = new_session_key;
228 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400229 th->secret_id = new_secret_id;
230 th->expires = new_expires;
231 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100232 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400233 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
234 type, ceph_entity_type_name(type), th->secret_id,
235 (int)th->ticket_blob->vec.iov_len);
236 xi->have_keys |= th->service;
237
238out:
239 return ret;
240
241bad:
242 ret = -EINVAL;
243 goto out;
244}
245
Sage Weilec0994e2010-02-02 16:25:35 -0800246static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
247 struct ceph_crypto_key *secret,
248 void *buf, void *end)
249{
Sage Weilec0994e2010-02-02 16:25:35 -0800250 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700251 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400252 u32 num;
253 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800254
Ilya Dryomov597cda32014-09-08 17:25:34 +0400255 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700256 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400257 return -EINVAL;
258
259 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800260 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400261
Sage Weilec0994e2010-02-02 16:25:35 -0800262 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400263 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800264 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400265 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800266 }
267
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400268 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800269
270bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400271 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800272}
273
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100274static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
275{
276 ceph_crypto_key_destroy(&au->session_key);
277 if (au->buf) {
278 ceph_buffer_put(au->buf);
279 au->buf = NULL;
280 }
281}
282
Sage Weilec0994e2010-02-02 16:25:35 -0800283static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
284 struct ceph_x_ticket_handler *th,
285 struct ceph_x_authorizer *au)
286{
Sage Weil807c86e2010-03-15 15:52:17 -0700287 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800288 struct ceph_x_authorize_a *msg_a;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100289 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800290 void *p, *end;
291 int ret;
292 int ticket_blob_len =
293 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
294
295 dout("build_authorizer for %s %p\n",
296 ceph_entity_type_name(th->service), au);
297
Yan, Zhengae385ea2014-11-04 16:32:35 +0800298 ceph_crypto_key_destroy(&au->session_key);
299 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
300 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100301 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800302
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100303 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100304 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700305 dout(" need len %d\n", maxlen);
306 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800307 ceph_buffer_put(au->buf);
308 au->buf = NULL;
309 }
310 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700311 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800312 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100313 ret = -ENOMEM;
314 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800315 }
Sage Weilec0994e2010-02-02 16:25:35 -0800316 }
317 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700318 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800319
320 msg_a = au->buf->vec.iov_base;
321 msg_a->struct_v = 1;
322 msg_a->global_id = cpu_to_le64(ac->global_id);
323 msg_a->service_id = cpu_to_le32(th->service);
324 msg_a->ticket_blob.struct_v = 1;
325 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
326 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
327 if (ticket_blob_len) {
328 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
329 th->ticket_blob->vec.iov_len);
330 }
331 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
332 le64_to_cpu(msg_a->ticket_blob.secret_id));
333
334 p = msg_a + 1;
335 p += ticket_blob_len;
336 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
337
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100338 msg_b = p + ceph_x_encrypt_offset();
339 msg_b->struct_v = 1;
Sage Weilec0994e2010-02-02 16:25:35 -0800340 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100341 msg_b->nonce = cpu_to_le64(au->nonce);
342 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
Sage Weilec0994e2010-02-02 16:25:35 -0800343 if (ret < 0)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100344 goto out_au;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100345
Sage Weilec0994e2010-02-02 16:25:35 -0800346 p += ret;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100347 WARN_ON(p > end);
Sage Weilec0994e2010-02-02 16:25:35 -0800348 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
349 dout(" built authorizer nonce %llx len %d\n", au->nonce,
350 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800351 return 0;
352
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100353out_au:
354 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800355 return ret;
356}
357
358static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
359 void **p, void *end)
360{
361 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
362 ceph_encode_8(p, 1);
363 ceph_encode_64(p, th->secret_id);
364 if (th->ticket_blob) {
365 const char *buf = th->ticket_blob->vec.iov_base;
366 u32 len = th->ticket_blob->vec.iov_len;
367
368 ceph_encode_32_safe(p, end, len, bad);
369 ceph_encode_copy_safe(p, end, buf, len, bad);
370 } else {
371 ceph_encode_32_safe(p, end, 0, bad);
372 }
373
374 return 0;
375bad:
376 return -ERANGE;
377}
378
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100379static bool need_key(struct ceph_x_ticket_handler *th)
380{
381 if (!th->have_key)
382 return true;
383
384 return get_seconds() >= th->renew_after;
385}
386
387static bool have_key(struct ceph_x_ticket_handler *th)
388{
389 if (th->have_key) {
390 if (get_seconds() >= th->expires)
391 th->have_key = false;
392 }
393
394 return th->have_key;
395}
396
Sage Weilec0994e2010-02-02 16:25:35 -0800397static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
398{
399 int want = ac->want_keys;
400 struct ceph_x_info *xi = ac->private;
401 int service;
402
403 *pneed = ac->want_keys & ~(xi->have_keys);
404
405 for (service = 1; service <= want; service <<= 1) {
406 struct ceph_x_ticket_handler *th;
407
408 if (!(ac->want_keys & service))
409 continue;
410
411 if (*pneed & service)
412 continue;
413
414 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200415 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800416 *pneed |= service;
417 continue;
418 }
419
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100420 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800421 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100422 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800423 xi->have_keys &= ~service;
424 }
425}
426
Sage Weilec0994e2010-02-02 16:25:35 -0800427static int ceph_x_build_request(struct ceph_auth_client *ac,
428 void *buf, void *end)
429{
430 struct ceph_x_info *xi = ac->private;
431 int need;
432 struct ceph_x_request_header *head = buf;
433 int ret;
434 struct ceph_x_ticket_handler *th =
435 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
436
Dan Carpenterb5457872010-08-26 11:12:38 +0200437 if (IS_ERR(th))
438 return PTR_ERR(th);
439
Sage Weilec0994e2010-02-02 16:25:35 -0800440 ceph_x_validate_tickets(ac, &need);
441
442 dout("build_request want %x have %x need %x\n",
443 ac->want_keys, xi->have_keys, need);
444
445 if (need & CEPH_ENTITY_TYPE_AUTH) {
446 struct ceph_x_authenticate *auth = (void *)(head + 1);
447 void *p = auth + 1;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100448 void *enc_buf = xi->auth_authorizer.enc_buf;
449 struct ceph_x_challenge_blob *blob = enc_buf +
450 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800451 u64 *u;
452
453 if (p > end)
454 return -ERANGE;
455
456 dout(" get_auth_session_key\n");
457 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
458
459 /* encrypt and hash */
460 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100461 blob->client_challenge = auth->client_challenge;
462 blob->server_challenge = cpu_to_le64(xi->server_challenge);
463 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
464 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800465 if (ret < 0)
466 return ret;
467
468 auth->struct_v = 1;
469 auth->key = 0;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100470 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700471 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800472 dout(" server_challenge %llx client_challenge %llx key %llx\n",
473 xi->server_challenge, le64_to_cpu(auth->client_challenge),
474 le64_to_cpu(auth->key));
475
476 /* now encode the old ticket if exists */
477 ret = ceph_x_encode_ticket(th, &p, end);
478 if (ret < 0)
479 return ret;
480
481 return p - buf;
482 }
483
484 if (need) {
485 void *p = head + 1;
486 struct ceph_x_service_ticket_request *req;
487
488 if (p > end)
489 return -ERANGE;
490 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
491
Sage Weilec0994e2010-02-02 16:25:35 -0800492 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
493 if (ret)
494 return ret;
495 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
496 xi->auth_authorizer.buf->vec.iov_len);
497
498 req = p;
499 req->keys = cpu_to_le32(need);
500 p += sizeof(*req);
501 return p - buf;
502 }
503
504 return 0;
505}
506
507static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
508 void *buf, void *end)
509{
510 struct ceph_x_info *xi = ac->private;
511 struct ceph_x_reply_header *head = buf;
512 struct ceph_x_ticket_handler *th;
513 int len = end - buf;
514 int op;
515 int ret;
516
517 if (result)
518 return result; /* XXX hmm? */
519
520 if (xi->starting) {
521 /* it's a hello */
522 struct ceph_x_server_challenge *sc = buf;
523
524 if (len != sizeof(*sc))
525 return -EINVAL;
526 xi->server_challenge = le64_to_cpu(sc->server_challenge);
527 dout("handle_reply got server challenge %llx\n",
528 xi->server_challenge);
529 xi->starting = false;
530 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
531 return -EAGAIN;
532 }
533
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700534 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800535 result = le32_to_cpu(head->result);
536 dout("handle_reply op %d result %d\n", op, result);
537 switch (op) {
538 case CEPHX_GET_AUTH_SESSION_KEY:
539 /* verify auth key */
540 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
541 buf + sizeof(*head), end);
542 break;
543
544 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
545 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200546 if (IS_ERR(th))
547 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800548 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
549 buf + sizeof(*head), end);
550 break;
551
552 default:
553 return -EINVAL;
554 }
555 if (ret)
556 return ret;
557 if (ac->want_keys == xi->have_keys)
558 return 0;
559 return -EAGAIN;
560}
561
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200562static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
563{
564 struct ceph_x_authorizer *au = (void *)a;
565
566 ceph_x_authorizer_cleanup(au);
567 kfree(au);
568}
569
Sage Weilec0994e2010-02-02 16:25:35 -0800570static int ceph_x_create_authorizer(
571 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500572 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800573{
574 struct ceph_x_authorizer *au;
575 struct ceph_x_ticket_handler *th;
576 int ret;
577
578 th = get_ticket_handler(ac, peer_type);
579 if (IS_ERR(th))
580 return PTR_ERR(th);
581
582 au = kzalloc(sizeof(*au), GFP_NOFS);
583 if (!au)
584 return -ENOMEM;
585
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200586 au->base.destroy = ceph_x_destroy_authorizer;
587
Sage Weilec0994e2010-02-02 16:25:35 -0800588 ret = ceph_x_build_authorizer(ac, th, au);
589 if (ret) {
590 kfree(au);
591 return ret;
592 }
593
Alex Elder74f18692012-05-16 15:16:39 -0500594 auth->authorizer = (struct ceph_authorizer *) au;
595 auth->authorizer_buf = au->buf->vec.iov_base;
596 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100597 auth->authorizer_reply_buf = au->enc_buf;
598 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800599 auth->sign_message = ac->ops->sign_message;
600 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500601
Sage Weilec0994e2010-02-02 16:25:35 -0800602 return 0;
603}
604
Sage Weil0bed9b52013-03-25 10:26:01 -0700605static int ceph_x_update_authorizer(
606 struct ceph_auth_client *ac, int peer_type,
607 struct ceph_auth_handshake *auth)
608{
609 struct ceph_x_authorizer *au;
610 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700611
612 th = get_ticket_handler(ac, peer_type);
613 if (IS_ERR(th))
614 return PTR_ERR(th);
615
616 au = (struct ceph_x_authorizer *)auth->authorizer;
617 if (au->secret_id < th->secret_id) {
618 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
619 au->service, au->secret_id, th->secret_id);
620 return ceph_x_build_authorizer(ac, th, au);
621 }
622 return 0;
623}
624
Sage Weilec0994e2010-02-02 16:25:35 -0800625static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
Ilya Dryomov0dde5842016-12-02 16:35:09 +0100626 struct ceph_authorizer *a)
Sage Weilec0994e2010-02-02 16:25:35 -0800627{
628 struct ceph_x_authorizer *au = (void *)a;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100629 void *p = au->enc_buf;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100630 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
631 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800632
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100633 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
Sage Weilec0994e2010-02-02 16:25:35 -0800634 if (ret < 0)
635 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100636 if (ret != sizeof(*reply))
Sage Weilec0994e2010-02-02 16:25:35 -0800637 return -EPERM;
638
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100639 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
Sage Weilec0994e2010-02-02 16:25:35 -0800640 ret = -EPERM;
641 else
642 ret = 0;
643 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100644 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
Sage Weilec0994e2010-02-02 16:25:35 -0800645 return ret;
646}
647
Sage Weilec0994e2010-02-02 16:25:35 -0800648static void ceph_x_reset(struct ceph_auth_client *ac)
649{
650 struct ceph_x_info *xi = ac->private;
651
652 dout("reset\n");
653 xi->starting = true;
654 xi->server_challenge = 0;
655}
656
657static void ceph_x_destroy(struct ceph_auth_client *ac)
658{
659 struct ceph_x_info *xi = ac->private;
660 struct rb_node *p;
661
662 dout("ceph_x_destroy %p\n", ac);
663 ceph_crypto_key_destroy(&xi->secret);
664
665 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
666 struct ceph_x_ticket_handler *th =
667 rb_entry(p, struct ceph_x_ticket_handler, node);
668 remove_ticket_handler(ac, th);
669 }
670
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100671 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700672
Sage Weilec0994e2010-02-02 16:25:35 -0800673 kfree(ac->private);
674 ac->private = NULL;
675}
676
Ilya Dryomov187d1312016-01-14 17:31:51 +0100677static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800678{
679 struct ceph_x_ticket_handler *th;
680
681 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200682 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100683 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800684}
685
Ilya Dryomov187d1312016-01-14 17:31:51 +0100686static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
687 int peer_type)
688{
689 /*
690 * We are to invalidate a service ticket in the hopes of
691 * getting a new, hopefully more valid, one. But, we won't get
692 * it unless our AUTH ticket is good, so invalidate AUTH ticket
693 * as well, just in case.
694 */
695 invalidate_ticket(ac, peer_type);
696 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
697}
698
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100699static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
700 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800701{
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100702 void *enc_buf = au->enc_buf;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100703 struct {
704 __le32 len;
705 __le32 header_crc;
706 __le32 front_crc;
707 __le32 middle_crc;
708 __le32 data_crc;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100709 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100710 int ret;
711
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100712 sigblock->len = cpu_to_le32(4*sizeof(u32));
713 sigblock->header_crc = msg->hdr.crc;
714 sigblock->front_crc = msg->footer.front_crc;
715 sigblock->middle_crc = msg->footer.middle_crc;
716 sigblock->data_crc = msg->footer.data_crc;
717 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
718 sizeof(*sigblock));
Yan, Zheng33d07332014-11-04 16:33:37 +0800719 if (ret < 0)
720 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100721
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100722 *psig = *(__le64 *)(enc_buf + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800723 return 0;
724}
725
726static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
727 struct ceph_msg *msg)
728{
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100729 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800730 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100731
Ilya Dryomova51983e2015-10-28 23:52:06 +0100732 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
733 return 0;
734
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100735 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
736 msg, &sig);
737 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800738 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100739
740 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800741 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
742 return 0;
743}
744
745static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
746 struct ceph_msg *msg)
747{
748 __le64 sig_check;
749 int ret;
750
Ilya Dryomova51983e2015-10-28 23:52:06 +0100751 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
752 return 0;
753
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100754 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
755 msg, &sig_check);
756 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800757 return ret;
758 if (sig_check == msg->footer.sig)
759 return 0;
760 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
761 dout("ceph_x_check_message_signature %p has signature %llx "
762 "expect %llx\n", msg, msg->footer.sig, sig_check);
763 else
764 dout("ceph_x_check_message_signature %p sender did not set "
765 "CEPH_MSG_FOOTER_SIGNED\n", msg);
766 return -EBADMSG;
767}
Sage Weilec0994e2010-02-02 16:25:35 -0800768
769static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700770 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800771 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700772 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800773 .build_request = ceph_x_build_request,
774 .handle_reply = ceph_x_handle_reply,
775 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700776 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800777 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800778 .invalidate_authorizer = ceph_x_invalidate_authorizer,
779 .reset = ceph_x_reset,
780 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800781 .sign_message = ceph_x_sign_message,
782 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800783};
784
785
786int ceph_x_init(struct ceph_auth_client *ac)
787{
788 struct ceph_x_info *xi;
789 int ret;
790
791 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700792 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800793 xi = kzalloc(sizeof(*xi), GFP_NOFS);
794 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700795 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800796
Sage Weilec0994e2010-02-02 16:25:35 -0800797 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700798 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800799 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700800 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800801 }
802
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700803 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
804 if (ret < 0) {
805 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700806 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700807 }
Sage Weilec0994e2010-02-02 16:25:35 -0800808
809 xi->starting = true;
810 xi->ticket_handlers = RB_ROOT;
811
812 ac->protocol = CEPH_AUTH_CEPHX;
813 ac->private = xi;
814 ac->ops = &ceph_x_ops;
815 return 0;
816
Sage Weilb0930f82010-04-29 13:26:53 -0700817out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800818 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700819out:
Sage Weilec0994e2010-02-02 16:25:35 -0800820 return ret;
821}
822
823