blob: 15845814a0f25eaefb95590cb848aa6032a8e038 [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>
Yan, Zheng33d07332014-11-04 16:33:37 +080011#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070012
13#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080014#include "auth_x.h"
15#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080016
Sage Weilec0994e2010-02-02 16:25:35 -080017static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
18
19static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
20{
21 struct ceph_x_info *xi = ac->private;
22 int need;
23
24 ceph_x_validate_tickets(ac, &need);
25 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
26 ac->want_keys, need, xi->have_keys);
27 return (ac->want_keys & xi->have_keys) == ac->want_keys;
28}
29
Sage Weila41359f2010-05-25 15:39:06 -070030static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
31{
32 struct ceph_x_info *xi = ac->private;
33 int need;
34
35 ceph_x_validate_tickets(ac, &need);
36 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
37 ac->want_keys, need, xi->have_keys);
38 return need != 0;
39}
40
Sage Weil807c86e2010-03-15 15:52:17 -070041static int ceph_x_encrypt_buflen(int ilen)
42{
43 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
44 sizeof(u32);
45}
46
Sage Weilec0994e2010-02-02 16:25:35 -080047static int ceph_x_encrypt(struct ceph_crypto_key *secret,
48 void *ibuf, int ilen, void *obuf, size_t olen)
49{
50 struct ceph_x_encrypt_header head = {
51 .struct_v = 1,
52 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
53 };
54 size_t len = olen - sizeof(u32);
55 int ret;
56
57 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
58 &head, sizeof(head), ibuf, ilen);
59 if (ret)
60 return ret;
61 ceph_encode_32(&obuf, len);
62 return len + sizeof(u32);
63}
64
65static int ceph_x_decrypt(struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040066 void **p, void *end, void **obuf, size_t olen)
Sage Weilec0994e2010-02-02 16:25:35 -080067{
68 struct ceph_x_encrypt_header head;
69 size_t head_len = sizeof(head);
70 int len, ret;
71
72 len = ceph_decode_32(p);
73 if (*p + len > end)
74 return -EINVAL;
75
76 dout("ceph_x_decrypt len %d\n", len);
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040077 if (*obuf == NULL) {
78 *obuf = kmalloc(len, GFP_NOFS);
79 if (!*obuf)
80 return -ENOMEM;
81 olen = len;
82 }
83
84 ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
Sage Weilec0994e2010-02-02 16:25:35 -080085 if (ret)
86 return ret;
87 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
88 return -EPERM;
89 *p += len;
90 return olen;
91}
92
93/*
94 * get existing (or insert new) ticket handler
95 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -070096static struct ceph_x_ticket_handler *
97get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -080098{
99 struct ceph_x_ticket_handler *th;
100 struct ceph_x_info *xi = ac->private;
101 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
102
103 while (*p) {
104 parent = *p;
105 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
106 if (service < th->service)
107 p = &(*p)->rb_left;
108 else if (service > th->service)
109 p = &(*p)->rb_right;
110 else
111 return th;
112 }
113
114 /* add it */
115 th = kzalloc(sizeof(*th), GFP_NOFS);
116 if (!th)
117 return ERR_PTR(-ENOMEM);
118 th->service = service;
119 rb_link_node(&th->node, parent, p);
120 rb_insert_color(&th->node, &xi->ticket_handlers);
121 return th;
122}
123
124static void remove_ticket_handler(struct ceph_auth_client *ac,
125 struct ceph_x_ticket_handler *th)
126{
127 struct ceph_x_info *xi = ac->private;
128
129 dout("remove_ticket_handler %p %d\n", th, th->service);
130 rb_erase(&th->node, &xi->ticket_handlers);
131 ceph_crypto_key_destroy(&th->session_key);
132 if (th->ticket_blob)
133 ceph_buffer_put(th->ticket_blob);
134 kfree(th);
135}
136
Ilya Dryomov597cda32014-09-08 17:25:34 +0400137static int process_one_ticket(struct ceph_auth_client *ac,
138 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400139 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400140{
141 struct ceph_x_info *xi = ac->private;
142 int type;
143 u8 tkt_struct_v, blob_struct_v;
144 struct ceph_x_ticket_handler *th;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400145 void *dbuf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400146 void *dp, *dend;
147 int dlen;
148 char is_enc;
149 struct timespec validity;
150 struct ceph_crypto_key old_key;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400151 void *ticket_buf = NULL;
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_timespec new_validity;
155 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
195 ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
196 ceph_decode_timespec(&validity, &new_validity);
197 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;
235 th->validity = new_validity;
236 th->secret_id = new_secret_id;
237 th->expires = new_expires;
238 th->renew_after = new_renew_after;
239 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
282static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
283 struct ceph_x_ticket_handler *th,
284 struct ceph_x_authorizer *au)
285{
Sage Weil807c86e2010-03-15 15:52:17 -0700286 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800287 struct ceph_x_authorize_a *msg_a;
288 struct ceph_x_authorize_b msg_b;
289 void *p, *end;
290 int ret;
291 int ticket_blob_len =
292 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
293
294 dout("build_authorizer for %s %p\n",
295 ceph_entity_type_name(th->service), au);
296
Yan, Zhengae385ea2014-11-04 16:32:35 +0800297 ceph_crypto_key_destroy(&au->session_key);
298 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
299 if (ret)
300 return ret;
301
Sage Weil807c86e2010-03-15 15:52:17 -0700302 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
303 ceph_x_encrypt_buflen(ticket_blob_len);
304 dout(" need len %d\n", maxlen);
305 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800306 ceph_buffer_put(au->buf);
307 au->buf = NULL;
308 }
309 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700310 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800311 if (!au->buf) {
312 ceph_crypto_key_destroy(&au->session_key);
Sage Weilec0994e2010-02-02 16:25:35 -0800313 return -ENOMEM;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800314 }
Sage Weilec0994e2010-02-02 16:25:35 -0800315 }
316 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700317 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800318
319 msg_a = au->buf->vec.iov_base;
320 msg_a->struct_v = 1;
321 msg_a->global_id = cpu_to_le64(ac->global_id);
322 msg_a->service_id = cpu_to_le32(th->service);
323 msg_a->ticket_blob.struct_v = 1;
324 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
325 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
326 if (ticket_blob_len) {
327 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
328 th->ticket_blob->vec.iov_len);
329 }
330 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
331 le64_to_cpu(msg_a->ticket_blob.secret_id));
332
333 p = msg_a + 1;
334 p += ticket_blob_len;
335 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
336
337 get_random_bytes(&au->nonce, sizeof(au->nonce));
338 msg_b.struct_v = 1;
339 msg_b.nonce = cpu_to_le64(au->nonce);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800340 ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
Sage Weilec0994e2010-02-02 16:25:35 -0800341 p, end - p);
342 if (ret < 0)
343 goto out_buf;
344 p += ret;
345 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
346 dout(" built authorizer nonce %llx len %d\n", au->nonce,
347 (int)au->buf->vec.iov_len);
Sage Weil807c86e2010-03-15 15:52:17 -0700348 BUG_ON(au->buf->vec.iov_len > maxlen);
Sage Weilec0994e2010-02-02 16:25:35 -0800349 return 0;
350
351out_buf:
352 ceph_buffer_put(au->buf);
353 au->buf = NULL;
354 return ret;
355}
356
357static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
358 void **p, void *end)
359{
360 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
361 ceph_encode_8(p, 1);
362 ceph_encode_64(p, th->secret_id);
363 if (th->ticket_blob) {
364 const char *buf = th->ticket_blob->vec.iov_base;
365 u32 len = th->ticket_blob->vec.iov_len;
366
367 ceph_encode_32_safe(p, end, len, bad);
368 ceph_encode_copy_safe(p, end, buf, len, bad);
369 } else {
370 ceph_encode_32_safe(p, end, 0, bad);
371 }
372
373 return 0;
374bad:
375 return -ERANGE;
376}
377
378static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
379{
380 int want = ac->want_keys;
381 struct ceph_x_info *xi = ac->private;
382 int service;
383
384 *pneed = ac->want_keys & ~(xi->have_keys);
385
386 for (service = 1; service <= want; service <<= 1) {
387 struct ceph_x_ticket_handler *th;
388
389 if (!(ac->want_keys & service))
390 continue;
391
392 if (*pneed & service)
393 continue;
394
395 th = get_ticket_handler(ac, service);
396
Dan Carpenterb5457872010-08-26 11:12:38 +0200397 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800398 *pneed |= service;
399 continue;
400 }
401
402 if (get_seconds() >= th->renew_after)
403 *pneed |= service;
404 if (get_seconds() >= th->expires)
405 xi->have_keys &= ~service;
406 }
407}
408
409
410static int ceph_x_build_request(struct ceph_auth_client *ac,
411 void *buf, void *end)
412{
413 struct ceph_x_info *xi = ac->private;
414 int need;
415 struct ceph_x_request_header *head = buf;
416 int ret;
417 struct ceph_x_ticket_handler *th =
418 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
419
Dan Carpenterb5457872010-08-26 11:12:38 +0200420 if (IS_ERR(th))
421 return PTR_ERR(th);
422
Sage Weilec0994e2010-02-02 16:25:35 -0800423 ceph_x_validate_tickets(ac, &need);
424
425 dout("build_request want %x have %x need %x\n",
426 ac->want_keys, xi->have_keys, need);
427
428 if (need & CEPH_ENTITY_TYPE_AUTH) {
429 struct ceph_x_authenticate *auth = (void *)(head + 1);
430 void *p = auth + 1;
431 struct ceph_x_challenge_blob tmp;
432 char tmp_enc[40];
433 u64 *u;
434
435 if (p > end)
436 return -ERANGE;
437
438 dout(" get_auth_session_key\n");
439 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
440
441 /* encrypt and hash */
442 get_random_bytes(&auth->client_challenge, sizeof(u64));
443 tmp.client_challenge = auth->client_challenge;
444 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
445 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
446 tmp_enc, sizeof(tmp_enc));
447 if (ret < 0)
448 return ret;
449
450 auth->struct_v = 1;
451 auth->key = 0;
452 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700453 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800454 dout(" server_challenge %llx client_challenge %llx key %llx\n",
455 xi->server_challenge, le64_to_cpu(auth->client_challenge),
456 le64_to_cpu(auth->key));
457
458 /* now encode the old ticket if exists */
459 ret = ceph_x_encode_ticket(th, &p, end);
460 if (ret < 0)
461 return ret;
462
463 return p - buf;
464 }
465
466 if (need) {
467 void *p = head + 1;
468 struct ceph_x_service_ticket_request *req;
469
470 if (p > end)
471 return -ERANGE;
472 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
473
Sage Weilec0994e2010-02-02 16:25:35 -0800474 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
475 if (ret)
476 return ret;
477 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
478 xi->auth_authorizer.buf->vec.iov_len);
479
480 req = p;
481 req->keys = cpu_to_le32(need);
482 p += sizeof(*req);
483 return p - buf;
484 }
485
486 return 0;
487}
488
489static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
490 void *buf, void *end)
491{
492 struct ceph_x_info *xi = ac->private;
493 struct ceph_x_reply_header *head = buf;
494 struct ceph_x_ticket_handler *th;
495 int len = end - buf;
496 int op;
497 int ret;
498
499 if (result)
500 return result; /* XXX hmm? */
501
502 if (xi->starting) {
503 /* it's a hello */
504 struct ceph_x_server_challenge *sc = buf;
505
506 if (len != sizeof(*sc))
507 return -EINVAL;
508 xi->server_challenge = le64_to_cpu(sc->server_challenge);
509 dout("handle_reply got server challenge %llx\n",
510 xi->server_challenge);
511 xi->starting = false;
512 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
513 return -EAGAIN;
514 }
515
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700516 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800517 result = le32_to_cpu(head->result);
518 dout("handle_reply op %d result %d\n", op, result);
519 switch (op) {
520 case CEPHX_GET_AUTH_SESSION_KEY:
521 /* verify auth key */
522 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
523 buf + sizeof(*head), end);
524 break;
525
526 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
527 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200528 if (IS_ERR(th))
529 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800530 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
531 buf + sizeof(*head), end);
532 break;
533
534 default:
535 return -EINVAL;
536 }
537 if (ret)
538 return ret;
539 if (ac->want_keys == xi->have_keys)
540 return 0;
541 return -EAGAIN;
542}
543
544static int ceph_x_create_authorizer(
545 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500546 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800547{
548 struct ceph_x_authorizer *au;
549 struct ceph_x_ticket_handler *th;
550 int ret;
551
552 th = get_ticket_handler(ac, peer_type);
553 if (IS_ERR(th))
554 return PTR_ERR(th);
555
556 au = kzalloc(sizeof(*au), GFP_NOFS);
557 if (!au)
558 return -ENOMEM;
559
560 ret = ceph_x_build_authorizer(ac, th, au);
561 if (ret) {
562 kfree(au);
563 return ret;
564 }
565
Alex Elder74f18692012-05-16 15:16:39 -0500566 auth->authorizer = (struct ceph_authorizer *) au;
567 auth->authorizer_buf = au->buf->vec.iov_base;
568 auth->authorizer_buf_len = au->buf->vec.iov_len;
569 auth->authorizer_reply_buf = au->reply_buf;
570 auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
Yan, Zheng33d07332014-11-04 16:33:37 +0800571 auth->sign_message = ac->ops->sign_message;
572 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500573
Sage Weilec0994e2010-02-02 16:25:35 -0800574 return 0;
575}
576
Sage Weil0bed9b52013-03-25 10:26:01 -0700577static int ceph_x_update_authorizer(
578 struct ceph_auth_client *ac, int peer_type,
579 struct ceph_auth_handshake *auth)
580{
581 struct ceph_x_authorizer *au;
582 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700583
584 th = get_ticket_handler(ac, peer_type);
585 if (IS_ERR(th))
586 return PTR_ERR(th);
587
588 au = (struct ceph_x_authorizer *)auth->authorizer;
589 if (au->secret_id < th->secret_id) {
590 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
591 au->service, au->secret_id, th->secret_id);
592 return ceph_x_build_authorizer(ac, th, au);
593 }
594 return 0;
595}
596
Sage Weilec0994e2010-02-02 16:25:35 -0800597static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
598 struct ceph_authorizer *a, size_t len)
599{
600 struct ceph_x_authorizer *au = (void *)a;
Sage Weilec0994e2010-02-02 16:25:35 -0800601 int ret = 0;
602 struct ceph_x_authorize_reply reply;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400603 void *preply = &reply;
Sage Weilec0994e2010-02-02 16:25:35 -0800604 void *p = au->reply_buf;
605 void *end = p + sizeof(au->reply_buf);
606
Yan, Zhengae385ea2014-11-04 16:32:35 +0800607 ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply));
Sage Weilec0994e2010-02-02 16:25:35 -0800608 if (ret < 0)
609 return ret;
610 if (ret != sizeof(reply))
611 return -EPERM;
612
613 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
614 ret = -EPERM;
615 else
616 ret = 0;
617 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
618 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
619 return ret;
620}
621
622static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
623 struct ceph_authorizer *a)
624{
625 struct ceph_x_authorizer *au = (void *)a;
626
Yan, Zhengae385ea2014-11-04 16:32:35 +0800627 ceph_crypto_key_destroy(&au->session_key);
Sage Weilec0994e2010-02-02 16:25:35 -0800628 ceph_buffer_put(au->buf);
629 kfree(au);
630}
631
632
633static void ceph_x_reset(struct ceph_auth_client *ac)
634{
635 struct ceph_x_info *xi = ac->private;
636
637 dout("reset\n");
638 xi->starting = true;
639 xi->server_challenge = 0;
640}
641
642static void ceph_x_destroy(struct ceph_auth_client *ac)
643{
644 struct ceph_x_info *xi = ac->private;
645 struct rb_node *p;
646
647 dout("ceph_x_destroy %p\n", ac);
648 ceph_crypto_key_destroy(&xi->secret);
649
650 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
651 struct ceph_x_ticket_handler *th =
652 rb_entry(p, struct ceph_x_ticket_handler, node);
653 remove_ticket_handler(ac, th);
654 }
655
Sage Weil22b1de02010-07-05 15:36:49 -0700656 if (xi->auth_authorizer.buf)
657 ceph_buffer_put(xi->auth_authorizer.buf);
658
Sage Weilec0994e2010-02-02 16:25:35 -0800659 kfree(ac->private);
660 ac->private = NULL;
661}
662
663static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
664 int peer_type)
665{
666 struct ceph_x_ticket_handler *th;
667
668 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200669 if (!IS_ERR(th))
Sage Weil4b8e8b52013-03-25 10:25:49 -0700670 memset(&th->validity, 0, sizeof(th->validity));
Sage Weilec0994e2010-02-02 16:25:35 -0800671}
672
Yan, Zheng33d07332014-11-04 16:33:37 +0800673static int calcu_signature(struct ceph_x_authorizer *au,
674 struct ceph_msg *msg, __le64 *sig)
675{
676 int ret;
677 char tmp_enc[40];
678 __le32 tmp[5] = {
679 16u, msg->hdr.crc, msg->footer.front_crc,
680 msg->footer.middle_crc, msg->footer.data_crc,
681 };
682 ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp),
683 tmp_enc, sizeof(tmp_enc));
684 if (ret < 0)
685 return ret;
686 *sig = *(__le64*)(tmp_enc + 4);
687 return 0;
688}
689
690static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
691 struct ceph_msg *msg)
692{
693 int ret;
694 if (!auth->authorizer)
695 return 0;
696 ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
697 msg, &msg->footer.sig);
698 if (ret < 0)
699 return ret;
700 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
701 return 0;
702}
703
704static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
705 struct ceph_msg *msg)
706{
707 __le64 sig_check;
708 int ret;
709
710 if (!auth->authorizer)
711 return 0;
712 ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
713 msg, &sig_check);
714 if (ret < 0)
715 return ret;
716 if (sig_check == msg->footer.sig)
717 return 0;
718 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
719 dout("ceph_x_check_message_signature %p has signature %llx "
720 "expect %llx\n", msg, msg->footer.sig, sig_check);
721 else
722 dout("ceph_x_check_message_signature %p sender did not set "
723 "CEPH_MSG_FOOTER_SIGNED\n", msg);
724 return -EBADMSG;
725}
Sage Weilec0994e2010-02-02 16:25:35 -0800726
727static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700728 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800729 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700730 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800731 .build_request = ceph_x_build_request,
732 .handle_reply = ceph_x_handle_reply,
733 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700734 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800735 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
736 .destroy_authorizer = ceph_x_destroy_authorizer,
737 .invalidate_authorizer = ceph_x_invalidate_authorizer,
738 .reset = ceph_x_reset,
739 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800740 .sign_message = ceph_x_sign_message,
741 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800742};
743
744
745int ceph_x_init(struct ceph_auth_client *ac)
746{
747 struct ceph_x_info *xi;
748 int ret;
749
750 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700751 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800752 xi = kzalloc(sizeof(*xi), GFP_NOFS);
753 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700754 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800755
Sage Weilec0994e2010-02-02 16:25:35 -0800756 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700757 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800758 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700759 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800760 }
761
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700762 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
763 if (ret < 0) {
764 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700765 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700766 }
Sage Weilec0994e2010-02-02 16:25:35 -0800767
768 xi->starting = true;
769 xi->ticket_handlers = RB_ROOT;
770
771 ac->protocol = CEPH_AUTH_CEPHX;
772 ac->private = xi;
773 ac->ops = &ceph_x_ops;
774 return 0;
775
Sage Weilb0930f82010-04-29 13:26:53 -0700776out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800777 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700778out:
Sage Weilec0994e2010-02-02 16:25:35 -0800779 return ret;
780}
781
782