blob: a0905f04bd13f3250f51de5a7600f4089d3053a4 [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
Sage Weil807c86e2010-03-15 15:52:17 -070042static int ceph_x_encrypt_buflen(int ilen)
43{
44 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
45 sizeof(u32);
46}
47
Sage Weilec0994e2010-02-02 16:25:35 -080048static int ceph_x_encrypt(struct ceph_crypto_key *secret,
49 void *ibuf, int ilen, void *obuf, size_t olen)
50{
51 struct ceph_x_encrypt_header head = {
52 .struct_v = 1,
53 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
54 };
55 size_t len = olen - sizeof(u32);
56 int ret;
57
58 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
59 &head, sizeof(head), ibuf, ilen);
60 if (ret)
61 return ret;
62 ceph_encode_32(&obuf, len);
63 return len + sizeof(u32);
64}
65
66static int ceph_x_decrypt(struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040067 void **p, void *end, void **obuf, size_t olen)
Sage Weilec0994e2010-02-02 16:25:35 -080068{
69 struct ceph_x_encrypt_header head;
70 size_t head_len = sizeof(head);
71 int len, ret;
72
73 len = ceph_decode_32(p);
74 if (*p + len > end)
75 return -EINVAL;
76
77 dout("ceph_x_decrypt len %d\n", len);
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040078 if (*obuf == NULL) {
79 *obuf = kmalloc(len, GFP_NOFS);
80 if (!*obuf)
81 return -ENOMEM;
82 olen = len;
83 }
84
85 ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
Sage Weilec0994e2010-02-02 16:25:35 -080086 if (ret)
87 return ret;
88 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
89 return -EPERM;
90 *p += len;
91 return olen;
92}
93
94/*
95 * get existing (or insert new) ticket handler
96 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -070097static struct ceph_x_ticket_handler *
98get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -080099{
100 struct ceph_x_ticket_handler *th;
101 struct ceph_x_info *xi = ac->private;
102 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
103
104 while (*p) {
105 parent = *p;
106 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
107 if (service < th->service)
108 p = &(*p)->rb_left;
109 else if (service > th->service)
110 p = &(*p)->rb_right;
111 else
112 return th;
113 }
114
115 /* add it */
116 th = kzalloc(sizeof(*th), GFP_NOFS);
117 if (!th)
118 return ERR_PTR(-ENOMEM);
119 th->service = service;
120 rb_link_node(&th->node, parent, p);
121 rb_insert_color(&th->node, &xi->ticket_handlers);
122 return th;
123}
124
125static void remove_ticket_handler(struct ceph_auth_client *ac,
126 struct ceph_x_ticket_handler *th)
127{
128 struct ceph_x_info *xi = ac->private;
129
130 dout("remove_ticket_handler %p %d\n", th, th->service);
131 rb_erase(&th->node, &xi->ticket_handlers);
132 ceph_crypto_key_destroy(&th->session_key);
133 if (th->ticket_blob)
134 ceph_buffer_put(th->ticket_blob);
135 kfree(th);
136}
137
Ilya Dryomov597cda32014-09-08 17:25:34 +0400138static int process_one_ticket(struct ceph_auth_client *ac,
139 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400140 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400141{
142 struct ceph_x_info *xi = ac->private;
143 int type;
144 u8 tkt_struct_v, blob_struct_v;
145 struct ceph_x_ticket_handler *th;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400146 void *dbuf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400147 void *dp, *dend;
148 int dlen;
149 char is_enc;
150 struct timespec validity;
151 struct ceph_crypto_key old_key;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400152 void *ticket_buf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400153 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400154 void **ptp;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400155 struct ceph_crypto_key new_session_key;
156 struct ceph_buffer *new_ticket_blob;
157 unsigned long new_expires, new_renew_after;
158 u64 new_secret_id;
159 int ret;
160
161 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
162
163 type = ceph_decode_32(p);
164 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
165
166 tkt_struct_v = ceph_decode_8(p);
167 if (tkt_struct_v != 1)
168 goto bad;
169
170 th = get_ticket_handler(ac, type);
171 if (IS_ERR(th)) {
172 ret = PTR_ERR(th);
173 goto out;
174 }
175
176 /* blob for me */
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400177 dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400178 if (dlen <= 0) {
179 ret = dlen;
180 goto out;
181 }
182 dout(" decrypted %d bytes\n", dlen);
183 dp = dbuf;
184 dend = dp + dlen;
185
186 tkt_struct_v = ceph_decode_8(&dp);
187 if (tkt_struct_v != 1)
188 goto bad;
189
190 memcpy(&old_key, &th->session_key, sizeof(old_key));
191 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
192 if (ret)
193 goto out;
194
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100195 ceph_decode_timespec(&validity, dp);
196 dp += sizeof(struct ceph_timespec);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400197 new_expires = get_seconds() + validity.tv_sec;
198 new_renew_after = new_expires - (validity.tv_sec / 4);
199 dout(" expires=%lu renew_after=%lu\n", new_expires,
200 new_renew_after);
201
202 /* ticket blob for service */
203 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400204 if (is_enc) {
205 /* encrypted */
206 dout(" encrypted ticket\n");
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400207 dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400208 if (dlen < 0) {
209 ret = dlen;
210 goto out;
211 }
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400212 tp = ticket_buf;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400213 ptp = &tp;
214 tpend = *ptp + dlen;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400215 } else {
216 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400217 ptp = p;
218 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400219 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400220 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400221 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400222 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
223 blob_struct_v = ceph_decode_8(ptp);
224 new_secret_id = ceph_decode_64(ptp);
225 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400226 if (ret)
227 goto out;
228
229 /* all is well, update our ticket */
230 ceph_crypto_key_destroy(&th->session_key);
231 if (th->ticket_blob)
232 ceph_buffer_put(th->ticket_blob);
233 th->session_key = new_session_key;
234 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400235 th->secret_id = new_secret_id;
236 th->expires = new_expires;
237 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100238 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400239 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
240 type, ceph_entity_type_name(type), th->secret_id,
241 (int)th->ticket_blob->vec.iov_len);
242 xi->have_keys |= th->service;
243
244out:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400245 kfree(ticket_buf);
246 kfree(dbuf);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400247 return ret;
248
249bad:
250 ret = -EINVAL;
251 goto out;
252}
253
Sage Weilec0994e2010-02-02 16:25:35 -0800254static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
255 struct ceph_crypto_key *secret,
256 void *buf, void *end)
257{
Sage Weilec0994e2010-02-02 16:25:35 -0800258 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700259 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400260 u32 num;
261 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800262
Ilya Dryomov597cda32014-09-08 17:25:34 +0400263 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700264 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400265 return -EINVAL;
266
267 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800268 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400269
Sage Weilec0994e2010-02-02 16:25:35 -0800270 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400271 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800272 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400273 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800274 }
275
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400276 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800277
278bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400279 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800280}
281
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100282static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
283{
284 ceph_crypto_key_destroy(&au->session_key);
285 if (au->buf) {
286 ceph_buffer_put(au->buf);
287 au->buf = NULL;
288 }
289}
290
Sage Weilec0994e2010-02-02 16:25:35 -0800291static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
292 struct ceph_x_ticket_handler *th,
293 struct ceph_x_authorizer *au)
294{
Sage Weil807c86e2010-03-15 15:52:17 -0700295 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800296 struct ceph_x_authorize_a *msg_a;
297 struct ceph_x_authorize_b msg_b;
298 void *p, *end;
299 int ret;
300 int ticket_blob_len =
301 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
302
303 dout("build_authorizer for %s %p\n",
304 ceph_entity_type_name(th->service), au);
305
Yan, Zhengae385ea2014-11-04 16:32:35 +0800306 ceph_crypto_key_destroy(&au->session_key);
307 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
308 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100309 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800310
Sage Weil807c86e2010-03-15 15:52:17 -0700311 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
312 ceph_x_encrypt_buflen(ticket_blob_len);
313 dout(" need len %d\n", maxlen);
314 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800315 ceph_buffer_put(au->buf);
316 au->buf = NULL;
317 }
318 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700319 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800320 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100321 ret = -ENOMEM;
322 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800323 }
Sage Weilec0994e2010-02-02 16:25:35 -0800324 }
325 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700326 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800327
328 msg_a = au->buf->vec.iov_base;
329 msg_a->struct_v = 1;
330 msg_a->global_id = cpu_to_le64(ac->global_id);
331 msg_a->service_id = cpu_to_le32(th->service);
332 msg_a->ticket_blob.struct_v = 1;
333 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
334 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
335 if (ticket_blob_len) {
336 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
337 th->ticket_blob->vec.iov_len);
338 }
339 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
340 le64_to_cpu(msg_a->ticket_blob.secret_id));
341
342 p = msg_a + 1;
343 p += ticket_blob_len;
344 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
345
346 get_random_bytes(&au->nonce, sizeof(au->nonce));
347 msg_b.struct_v = 1;
348 msg_b.nonce = cpu_to_le64(au->nonce);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800349 ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
Sage Weilec0994e2010-02-02 16:25:35 -0800350 p, end - p);
351 if (ret < 0)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100352 goto out_au;
Sage Weilec0994e2010-02-02 16:25:35 -0800353 p += ret;
354 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
355 dout(" built authorizer nonce %llx len %d\n", au->nonce,
356 (int)au->buf->vec.iov_len);
Sage Weil807c86e2010-03-15 15:52:17 -0700357 BUG_ON(au->buf->vec.iov_len > maxlen);
Sage Weilec0994e2010-02-02 16:25:35 -0800358 return 0;
359
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100360out_au:
361 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800362 return ret;
363}
364
365static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
366 void **p, void *end)
367{
368 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
369 ceph_encode_8(p, 1);
370 ceph_encode_64(p, th->secret_id);
371 if (th->ticket_blob) {
372 const char *buf = th->ticket_blob->vec.iov_base;
373 u32 len = th->ticket_blob->vec.iov_len;
374
375 ceph_encode_32_safe(p, end, len, bad);
376 ceph_encode_copy_safe(p, end, buf, len, bad);
377 } else {
378 ceph_encode_32_safe(p, end, 0, bad);
379 }
380
381 return 0;
382bad:
383 return -ERANGE;
384}
385
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100386static bool need_key(struct ceph_x_ticket_handler *th)
387{
388 if (!th->have_key)
389 return true;
390
391 return get_seconds() >= th->renew_after;
392}
393
394static bool have_key(struct ceph_x_ticket_handler *th)
395{
396 if (th->have_key) {
397 if (get_seconds() >= th->expires)
398 th->have_key = false;
399 }
400
401 return th->have_key;
402}
403
Sage Weilec0994e2010-02-02 16:25:35 -0800404static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
405{
406 int want = ac->want_keys;
407 struct ceph_x_info *xi = ac->private;
408 int service;
409
410 *pneed = ac->want_keys & ~(xi->have_keys);
411
412 for (service = 1; service <= want; service <<= 1) {
413 struct ceph_x_ticket_handler *th;
414
415 if (!(ac->want_keys & service))
416 continue;
417
418 if (*pneed & service)
419 continue;
420
421 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200422 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800423 *pneed |= service;
424 continue;
425 }
426
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100427 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800428 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100429 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800430 xi->have_keys &= ~service;
431 }
432}
433
Sage Weilec0994e2010-02-02 16:25:35 -0800434static int ceph_x_build_request(struct ceph_auth_client *ac,
435 void *buf, void *end)
436{
437 struct ceph_x_info *xi = ac->private;
438 int need;
439 struct ceph_x_request_header *head = buf;
440 int ret;
441 struct ceph_x_ticket_handler *th =
442 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
443
Dan Carpenterb5457872010-08-26 11:12:38 +0200444 if (IS_ERR(th))
445 return PTR_ERR(th);
446
Sage Weilec0994e2010-02-02 16:25:35 -0800447 ceph_x_validate_tickets(ac, &need);
448
449 dout("build_request want %x have %x need %x\n",
450 ac->want_keys, xi->have_keys, need);
451
452 if (need & CEPH_ENTITY_TYPE_AUTH) {
453 struct ceph_x_authenticate *auth = (void *)(head + 1);
454 void *p = auth + 1;
455 struct ceph_x_challenge_blob tmp;
456 char tmp_enc[40];
457 u64 *u;
458
459 if (p > end)
460 return -ERANGE;
461
462 dout(" get_auth_session_key\n");
463 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
464
465 /* encrypt and hash */
466 get_random_bytes(&auth->client_challenge, sizeof(u64));
467 tmp.client_challenge = auth->client_challenge;
468 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
469 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
470 tmp_enc, sizeof(tmp_enc));
471 if (ret < 0)
472 return ret;
473
474 auth->struct_v = 1;
475 auth->key = 0;
476 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700477 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800478 dout(" server_challenge %llx client_challenge %llx key %llx\n",
479 xi->server_challenge, le64_to_cpu(auth->client_challenge),
480 le64_to_cpu(auth->key));
481
482 /* now encode the old ticket if exists */
483 ret = ceph_x_encode_ticket(th, &p, end);
484 if (ret < 0)
485 return ret;
486
487 return p - buf;
488 }
489
490 if (need) {
491 void *p = head + 1;
492 struct ceph_x_service_ticket_request *req;
493
494 if (p > end)
495 return -ERANGE;
496 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
497
Sage Weilec0994e2010-02-02 16:25:35 -0800498 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
499 if (ret)
500 return ret;
501 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
502 xi->auth_authorizer.buf->vec.iov_len);
503
504 req = p;
505 req->keys = cpu_to_le32(need);
506 p += sizeof(*req);
507 return p - buf;
508 }
509
510 return 0;
511}
512
513static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
514 void *buf, void *end)
515{
516 struct ceph_x_info *xi = ac->private;
517 struct ceph_x_reply_header *head = buf;
518 struct ceph_x_ticket_handler *th;
519 int len = end - buf;
520 int op;
521 int ret;
522
523 if (result)
524 return result; /* XXX hmm? */
525
526 if (xi->starting) {
527 /* it's a hello */
528 struct ceph_x_server_challenge *sc = buf;
529
530 if (len != sizeof(*sc))
531 return -EINVAL;
532 xi->server_challenge = le64_to_cpu(sc->server_challenge);
533 dout("handle_reply got server challenge %llx\n",
534 xi->server_challenge);
535 xi->starting = false;
536 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
537 return -EAGAIN;
538 }
539
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700540 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800541 result = le32_to_cpu(head->result);
542 dout("handle_reply op %d result %d\n", op, result);
543 switch (op) {
544 case CEPHX_GET_AUTH_SESSION_KEY:
545 /* verify auth key */
546 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
547 buf + sizeof(*head), end);
548 break;
549
550 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
551 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200552 if (IS_ERR(th))
553 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800554 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
555 buf + sizeof(*head), end);
556 break;
557
558 default:
559 return -EINVAL;
560 }
561 if (ret)
562 return ret;
563 if (ac->want_keys == xi->have_keys)
564 return 0;
565 return -EAGAIN;
566}
567
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200568static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
569{
570 struct ceph_x_authorizer *au = (void *)a;
571
572 ceph_x_authorizer_cleanup(au);
573 kfree(au);
574}
575
Sage Weilec0994e2010-02-02 16:25:35 -0800576static int ceph_x_create_authorizer(
577 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500578 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800579{
580 struct ceph_x_authorizer *au;
581 struct ceph_x_ticket_handler *th;
582 int ret;
583
584 th = get_ticket_handler(ac, peer_type);
585 if (IS_ERR(th))
586 return PTR_ERR(th);
587
588 au = kzalloc(sizeof(*au), GFP_NOFS);
589 if (!au)
590 return -ENOMEM;
591
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200592 au->base.destroy = ceph_x_destroy_authorizer;
593
Sage Weilec0994e2010-02-02 16:25:35 -0800594 ret = ceph_x_build_authorizer(ac, th, au);
595 if (ret) {
596 kfree(au);
597 return ret;
598 }
599
Alex Elder74f18692012-05-16 15:16:39 -0500600 auth->authorizer = (struct ceph_authorizer *) au;
601 auth->authorizer_buf = au->buf->vec.iov_base;
602 auth->authorizer_buf_len = au->buf->vec.iov_len;
603 auth->authorizer_reply_buf = au->reply_buf;
604 auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
Yan, Zheng33d07332014-11-04 16:33:37 +0800605 auth->sign_message = ac->ops->sign_message;
606 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500607
Sage Weilec0994e2010-02-02 16:25:35 -0800608 return 0;
609}
610
Sage Weil0bed9b52013-03-25 10:26:01 -0700611static int ceph_x_update_authorizer(
612 struct ceph_auth_client *ac, int peer_type,
613 struct ceph_auth_handshake *auth)
614{
615 struct ceph_x_authorizer *au;
616 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700617
618 th = get_ticket_handler(ac, peer_type);
619 if (IS_ERR(th))
620 return PTR_ERR(th);
621
622 au = (struct ceph_x_authorizer *)auth->authorizer;
623 if (au->secret_id < th->secret_id) {
624 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
625 au->service, au->secret_id, th->secret_id);
626 return ceph_x_build_authorizer(ac, th, au);
627 }
628 return 0;
629}
630
Sage Weilec0994e2010-02-02 16:25:35 -0800631static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
632 struct ceph_authorizer *a, size_t len)
633{
634 struct ceph_x_authorizer *au = (void *)a;
Sage Weilec0994e2010-02-02 16:25:35 -0800635 int ret = 0;
636 struct ceph_x_authorize_reply reply;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400637 void *preply = &reply;
Sage Weilec0994e2010-02-02 16:25:35 -0800638 void *p = au->reply_buf;
639 void *end = p + sizeof(au->reply_buf);
640
Yan, Zhengae385ea2014-11-04 16:32:35 +0800641 ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply));
Sage Weilec0994e2010-02-02 16:25:35 -0800642 if (ret < 0)
643 return ret;
644 if (ret != sizeof(reply))
645 return -EPERM;
646
647 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
648 ret = -EPERM;
649 else
650 ret = 0;
651 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
652 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
653 return ret;
654}
655
Sage Weilec0994e2010-02-02 16:25:35 -0800656static void ceph_x_reset(struct ceph_auth_client *ac)
657{
658 struct ceph_x_info *xi = ac->private;
659
660 dout("reset\n");
661 xi->starting = true;
662 xi->server_challenge = 0;
663}
664
665static void ceph_x_destroy(struct ceph_auth_client *ac)
666{
667 struct ceph_x_info *xi = ac->private;
668 struct rb_node *p;
669
670 dout("ceph_x_destroy %p\n", ac);
671 ceph_crypto_key_destroy(&xi->secret);
672
673 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
674 struct ceph_x_ticket_handler *th =
675 rb_entry(p, struct ceph_x_ticket_handler, node);
676 remove_ticket_handler(ac, th);
677 }
678
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100679 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700680
Sage Weilec0994e2010-02-02 16:25:35 -0800681 kfree(ac->private);
682 ac->private = NULL;
683}
684
Ilya Dryomov187d1312016-01-14 17:31:51 +0100685static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800686{
687 struct ceph_x_ticket_handler *th;
688
689 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200690 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100691 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800692}
693
Ilya Dryomov187d1312016-01-14 17:31:51 +0100694static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
695 int peer_type)
696{
697 /*
698 * We are to invalidate a service ticket in the hopes of
699 * getting a new, hopefully more valid, one. But, we won't get
700 * it unless our AUTH ticket is good, so invalidate AUTH ticket
701 * as well, just in case.
702 */
703 invalidate_ticket(ac, peer_type);
704 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
705}
706
Yan, Zheng33d07332014-11-04 16:33:37 +0800707static int calcu_signature(struct ceph_x_authorizer *au,
708 struct ceph_msg *msg, __le64 *sig)
709{
710 int ret;
711 char tmp_enc[40];
712 __le32 tmp[5] = {
Ilya Dryomovd7d5a002014-12-19 14:00:41 +0300713 cpu_to_le32(16), msg->hdr.crc, msg->footer.front_crc,
Yan, Zheng33d07332014-11-04 16:33:37 +0800714 msg->footer.middle_crc, msg->footer.data_crc,
715 };
716 ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp),
717 tmp_enc, sizeof(tmp_enc));
718 if (ret < 0)
719 return ret;
720 *sig = *(__le64*)(tmp_enc + 4);
721 return 0;
722}
723
724static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
725 struct ceph_msg *msg)
726{
727 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100728
Ilya Dryomova51983e2015-10-28 23:52:06 +0100729 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
730 return 0;
731
Yan, Zheng33d07332014-11-04 16:33:37 +0800732 ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
733 msg, &msg->footer.sig);
734 if (ret < 0)
735 return ret;
736 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
737 return 0;
738}
739
740static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
741 struct ceph_msg *msg)
742{
743 __le64 sig_check;
744 int ret;
745
Ilya Dryomova51983e2015-10-28 23:52:06 +0100746 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
747 return 0;
748
Yan, Zheng33d07332014-11-04 16:33:37 +0800749 ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
750 msg, &sig_check);
751 if (ret < 0)
752 return ret;
753 if (sig_check == msg->footer.sig)
754 return 0;
755 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
756 dout("ceph_x_check_message_signature %p has signature %llx "
757 "expect %llx\n", msg, msg->footer.sig, sig_check);
758 else
759 dout("ceph_x_check_message_signature %p sender did not set "
760 "CEPH_MSG_FOOTER_SIGNED\n", msg);
761 return -EBADMSG;
762}
Sage Weilec0994e2010-02-02 16:25:35 -0800763
764static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700765 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800766 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700767 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800768 .build_request = ceph_x_build_request,
769 .handle_reply = ceph_x_handle_reply,
770 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700771 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800772 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800773 .invalidate_authorizer = ceph_x_invalidate_authorizer,
774 .reset = ceph_x_reset,
775 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800776 .sign_message = ceph_x_sign_message,
777 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800778};
779
780
781int ceph_x_init(struct ceph_auth_client *ac)
782{
783 struct ceph_x_info *xi;
784 int ret;
785
786 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700787 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800788 xi = kzalloc(sizeof(*xi), GFP_NOFS);
789 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700790 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800791
Sage Weilec0994e2010-02-02 16:25:35 -0800792 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700793 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800794 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700795 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800796 }
797
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700798 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
799 if (ret < 0) {
800 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700801 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700802 }
Sage Weilec0994e2010-02-02 16:25:35 -0800803
804 xi->starting = true;
805 xi->ticket_handlers = RB_ROOT;
806
807 ac->protocol = CEPH_AUTH_CEPHX;
808 ac->private = xi;
809 ac->ops = &ceph_x_ops;
810 return 0;
811
Sage Weilb0930f82010-04-29 13:26:53 -0700812out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800813 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700814out:
Sage Weilec0994e2010-02-02 16:25:35 -0800815 return ret;
816}
817
818