blob: 78c1675b0df7c1a41b85959cc2da210825945a69 [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 Dryomov55d9cc832016-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 Dryomov55d9cc832016-12-02 16:35:07 +010049 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070050}
51
Sage Weilec0994e2010-02-02 16:25:35 -080052static int ceph_x_encrypt(struct ceph_crypto_key *secret,
53 void *ibuf, int ilen, void *obuf, size_t olen)
54{
55 struct ceph_x_encrypt_header head = {
56 .struct_v = 1,
57 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
58 };
59 size_t len = olen - sizeof(u32);
60 int ret;
61
62 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
63 &head, sizeof(head), ibuf, ilen);
64 if (ret)
65 return ret;
66 ceph_encode_32(&obuf, len);
67 return len + sizeof(u32);
68}
69
70static int ceph_x_decrypt(struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040071 void **p, void *end, void **obuf, size_t olen)
Sage Weilec0994e2010-02-02 16:25:35 -080072{
73 struct ceph_x_encrypt_header head;
74 size_t head_len = sizeof(head);
75 int len, ret;
76
77 len = ceph_decode_32(p);
78 if (*p + len > end)
79 return -EINVAL;
80
81 dout("ceph_x_decrypt len %d\n", len);
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040082 if (*obuf == NULL) {
83 *obuf = kmalloc(len, GFP_NOFS);
84 if (!*obuf)
85 return -ENOMEM;
86 olen = len;
87 }
88
89 ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
Sage Weilec0994e2010-02-02 16:25:35 -080090 if (ret)
91 return ret;
92 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
93 return -EPERM;
94 *p += len;
95 return olen;
96}
97
98/*
99 * get existing (or insert new) ticket handler
100 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700101static struct ceph_x_ticket_handler *
102get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800103{
104 struct ceph_x_ticket_handler *th;
105 struct ceph_x_info *xi = ac->private;
106 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
107
108 while (*p) {
109 parent = *p;
110 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
111 if (service < th->service)
112 p = &(*p)->rb_left;
113 else if (service > th->service)
114 p = &(*p)->rb_right;
115 else
116 return th;
117 }
118
119 /* add it */
120 th = kzalloc(sizeof(*th), GFP_NOFS);
121 if (!th)
122 return ERR_PTR(-ENOMEM);
123 th->service = service;
124 rb_link_node(&th->node, parent, p);
125 rb_insert_color(&th->node, &xi->ticket_handlers);
126 return th;
127}
128
129static void remove_ticket_handler(struct ceph_auth_client *ac,
130 struct ceph_x_ticket_handler *th)
131{
132 struct ceph_x_info *xi = ac->private;
133
134 dout("remove_ticket_handler %p %d\n", th, th->service);
135 rb_erase(&th->node, &xi->ticket_handlers);
136 ceph_crypto_key_destroy(&th->session_key);
137 if (th->ticket_blob)
138 ceph_buffer_put(th->ticket_blob);
139 kfree(th);
140}
141
Ilya Dryomov597cda32014-09-08 17:25:34 +0400142static int process_one_ticket(struct ceph_auth_client *ac,
143 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400144 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400145{
146 struct ceph_x_info *xi = ac->private;
147 int type;
148 u8 tkt_struct_v, blob_struct_v;
149 struct ceph_x_ticket_handler *th;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400150 void *dbuf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400151 void *dp, *dend;
152 int dlen;
153 char is_enc;
154 struct timespec validity;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400155 void *ticket_buf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400156 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400157 void **ptp;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400158 struct ceph_crypto_key new_session_key;
159 struct ceph_buffer *new_ticket_blob;
160 unsigned long new_expires, new_renew_after;
161 u64 new_secret_id;
162 int ret;
163
164 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
165
166 type = ceph_decode_32(p);
167 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
168
169 tkt_struct_v = ceph_decode_8(p);
170 if (tkt_struct_v != 1)
171 goto bad;
172
173 th = get_ticket_handler(ac, type);
174 if (IS_ERR(th)) {
175 ret = PTR_ERR(th);
176 goto out;
177 }
178
179 /* blob for me */
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400180 dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400181 if (dlen <= 0) {
182 ret = dlen;
183 goto out;
184 }
185 dout(" decrypted %d bytes\n", dlen);
186 dp = dbuf;
187 dend = dp + dlen;
188
189 tkt_struct_v = ceph_decode_8(&dp);
190 if (tkt_struct_v != 1)
191 goto bad;
192
Ilya Dryomov597cda32014-09-08 17:25:34 +0400193 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
194 if (ret)
195 goto out;
196
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100197 ceph_decode_timespec(&validity, dp);
198 dp += sizeof(struct ceph_timespec);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400199 new_expires = get_seconds() + validity.tv_sec;
200 new_renew_after = new_expires - (validity.tv_sec / 4);
201 dout(" expires=%lu renew_after=%lu\n", new_expires,
202 new_renew_after);
203
204 /* ticket blob for service */
205 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400206 if (is_enc) {
207 /* encrypted */
208 dout(" encrypted ticket\n");
Ilya Dryomov462e6502016-12-02 16:35:06 +0100209 dlen = ceph_x_decrypt(&th->session_key, p, end, &ticket_buf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400210 if (dlen < 0) {
211 ret = dlen;
212 goto out;
213 }
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400214 tp = ticket_buf;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400215 ptp = &tp;
216 tpend = *ptp + dlen;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400217 } else {
218 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400219 ptp = p;
220 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400221 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400222 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400223 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400224 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
225 blob_struct_v = ceph_decode_8(ptp);
226 new_secret_id = ceph_decode_64(ptp);
227 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400228 if (ret)
229 goto out;
230
231 /* all is well, update our ticket */
232 ceph_crypto_key_destroy(&th->session_key);
233 if (th->ticket_blob)
234 ceph_buffer_put(th->ticket_blob);
235 th->session_key = new_session_key;
236 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400237 th->secret_id = new_secret_id;
238 th->expires = new_expires;
239 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100240 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400241 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
242 type, ceph_entity_type_name(type), th->secret_id,
243 (int)th->ticket_blob->vec.iov_len);
244 xi->have_keys |= th->service;
245
246out:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400247 kfree(ticket_buf);
248 kfree(dbuf);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400249 return ret;
250
251bad:
252 ret = -EINVAL;
253 goto out;
254}
255
Sage Weilec0994e2010-02-02 16:25:35 -0800256static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
257 struct ceph_crypto_key *secret,
258 void *buf, void *end)
259{
Sage Weilec0994e2010-02-02 16:25:35 -0800260 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700261 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400262 u32 num;
263 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800264
Ilya Dryomov597cda32014-09-08 17:25:34 +0400265 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700266 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400267 return -EINVAL;
268
269 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800270 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400271
Sage Weilec0994e2010-02-02 16:25:35 -0800272 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400273 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800274 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400275 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800276 }
277
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400278 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800279
280bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400281 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800282}
283
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100284static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
285{
286 ceph_crypto_key_destroy(&au->session_key);
287 if (au->buf) {
288 ceph_buffer_put(au->buf);
289 au->buf = NULL;
290 }
291}
292
Sage Weilec0994e2010-02-02 16:25:35 -0800293static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
294 struct ceph_x_ticket_handler *th,
295 struct ceph_x_authorizer *au)
296{
Sage Weil807c86e2010-03-15 15:52:17 -0700297 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800298 struct ceph_x_authorize_a *msg_a;
299 struct ceph_x_authorize_b msg_b;
300 void *p, *end;
301 int ret;
302 int ticket_blob_len =
303 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
304
305 dout("build_authorizer for %s %p\n",
306 ceph_entity_type_name(th->service), au);
307
Yan, Zhengae385ea2014-11-04 16:32:35 +0800308 ceph_crypto_key_destroy(&au->session_key);
309 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
310 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100311 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800312
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100313 maxlen = sizeof(*msg_a) + ticket_blob_len +
314 ceph_x_encrypt_buflen(sizeof(msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700315 dout(" need len %d\n", maxlen);
316 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800317 ceph_buffer_put(au->buf);
318 au->buf = NULL;
319 }
320 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700321 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800322 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100323 ret = -ENOMEM;
324 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800325 }
Sage Weilec0994e2010-02-02 16:25:35 -0800326 }
327 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700328 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800329
330 msg_a = au->buf->vec.iov_base;
331 msg_a->struct_v = 1;
332 msg_a->global_id = cpu_to_le64(ac->global_id);
333 msg_a->service_id = cpu_to_le32(th->service);
334 msg_a->ticket_blob.struct_v = 1;
335 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
336 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
337 if (ticket_blob_len) {
338 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
339 th->ticket_blob->vec.iov_len);
340 }
341 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
342 le64_to_cpu(msg_a->ticket_blob.secret_id));
343
344 p = msg_a + 1;
345 p += ticket_blob_len;
346 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
347
348 get_random_bytes(&au->nonce, sizeof(au->nonce));
349 msg_b.struct_v = 1;
350 msg_b.nonce = cpu_to_le64(au->nonce);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800351 ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
Sage Weilec0994e2010-02-02 16:25:35 -0800352 p, end - p);
353 if (ret < 0)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100354 goto out_au;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100355
Sage Weilec0994e2010-02-02 16:25:35 -0800356 p += ret;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100357 WARN_ON(p > end);
Sage Weilec0994e2010-02-02 16:25:35 -0800358 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
359 dout(" built authorizer nonce %llx len %d\n", au->nonce,
360 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800361 return 0;
362
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100363out_au:
364 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800365 return ret;
366}
367
368static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
369 void **p, void *end)
370{
371 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
372 ceph_encode_8(p, 1);
373 ceph_encode_64(p, th->secret_id);
374 if (th->ticket_blob) {
375 const char *buf = th->ticket_blob->vec.iov_base;
376 u32 len = th->ticket_blob->vec.iov_len;
377
378 ceph_encode_32_safe(p, end, len, bad);
379 ceph_encode_copy_safe(p, end, buf, len, bad);
380 } else {
381 ceph_encode_32_safe(p, end, 0, bad);
382 }
383
384 return 0;
385bad:
386 return -ERANGE;
387}
388
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100389static bool need_key(struct ceph_x_ticket_handler *th)
390{
391 if (!th->have_key)
392 return true;
393
394 return get_seconds() >= th->renew_after;
395}
396
397static bool have_key(struct ceph_x_ticket_handler *th)
398{
399 if (th->have_key) {
400 if (get_seconds() >= th->expires)
401 th->have_key = false;
402 }
403
404 return th->have_key;
405}
406
Sage Weilec0994e2010-02-02 16:25:35 -0800407static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
408{
409 int want = ac->want_keys;
410 struct ceph_x_info *xi = ac->private;
411 int service;
412
413 *pneed = ac->want_keys & ~(xi->have_keys);
414
415 for (service = 1; service <= want; service <<= 1) {
416 struct ceph_x_ticket_handler *th;
417
418 if (!(ac->want_keys & service))
419 continue;
420
421 if (*pneed & service)
422 continue;
423
424 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200425 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800426 *pneed |= service;
427 continue;
428 }
429
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100430 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800431 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100432 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800433 xi->have_keys &= ~service;
434 }
435}
436
Sage Weilec0994e2010-02-02 16:25:35 -0800437static int ceph_x_build_request(struct ceph_auth_client *ac,
438 void *buf, void *end)
439{
440 struct ceph_x_info *xi = ac->private;
441 int need;
442 struct ceph_x_request_header *head = buf;
443 int ret;
444 struct ceph_x_ticket_handler *th =
445 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
446
Dan Carpenterb5457872010-08-26 11:12:38 +0200447 if (IS_ERR(th))
448 return PTR_ERR(th);
449
Sage Weilec0994e2010-02-02 16:25:35 -0800450 ceph_x_validate_tickets(ac, &need);
451
452 dout("build_request want %x have %x need %x\n",
453 ac->want_keys, xi->have_keys, need);
454
455 if (need & CEPH_ENTITY_TYPE_AUTH) {
456 struct ceph_x_authenticate *auth = (void *)(head + 1);
457 void *p = auth + 1;
458 struct ceph_x_challenge_blob tmp;
459 char tmp_enc[40];
460 u64 *u;
461
462 if (p > end)
463 return -ERANGE;
464
465 dout(" get_auth_session_key\n");
466 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
467
468 /* encrypt and hash */
469 get_random_bytes(&auth->client_challenge, sizeof(u64));
470 tmp.client_challenge = auth->client_challenge;
471 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
472 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
473 tmp_enc, sizeof(tmp_enc));
474 if (ret < 0)
475 return ret;
476
477 auth->struct_v = 1;
478 auth->key = 0;
479 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700480 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800481 dout(" server_challenge %llx client_challenge %llx key %llx\n",
482 xi->server_challenge, le64_to_cpu(auth->client_challenge),
483 le64_to_cpu(auth->key));
484
485 /* now encode the old ticket if exists */
486 ret = ceph_x_encode_ticket(th, &p, end);
487 if (ret < 0)
488 return ret;
489
490 return p - buf;
491 }
492
493 if (need) {
494 void *p = head + 1;
495 struct ceph_x_service_ticket_request *req;
496
497 if (p > end)
498 return -ERANGE;
499 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
500
Sage Weilec0994e2010-02-02 16:25:35 -0800501 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
502 if (ret)
503 return ret;
504 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
505 xi->auth_authorizer.buf->vec.iov_len);
506
507 req = p;
508 req->keys = cpu_to_le32(need);
509 p += sizeof(*req);
510 return p - buf;
511 }
512
513 return 0;
514}
515
516static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
517 void *buf, void *end)
518{
519 struct ceph_x_info *xi = ac->private;
520 struct ceph_x_reply_header *head = buf;
521 struct ceph_x_ticket_handler *th;
522 int len = end - buf;
523 int op;
524 int ret;
525
526 if (result)
527 return result; /* XXX hmm? */
528
529 if (xi->starting) {
530 /* it's a hello */
531 struct ceph_x_server_challenge *sc = buf;
532
533 if (len != sizeof(*sc))
534 return -EINVAL;
535 xi->server_challenge = le64_to_cpu(sc->server_challenge);
536 dout("handle_reply got server challenge %llx\n",
537 xi->server_challenge);
538 xi->starting = false;
539 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
540 return -EAGAIN;
541 }
542
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700543 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800544 result = le32_to_cpu(head->result);
545 dout("handle_reply op %d result %d\n", op, result);
546 switch (op) {
547 case CEPHX_GET_AUTH_SESSION_KEY:
548 /* verify auth key */
549 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
550 buf + sizeof(*head), end);
551 break;
552
553 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
554 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200555 if (IS_ERR(th))
556 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800557 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
558 buf + sizeof(*head), end);
559 break;
560
561 default:
562 return -EINVAL;
563 }
564 if (ret)
565 return ret;
566 if (ac->want_keys == xi->have_keys)
567 return 0;
568 return -EAGAIN;
569}
570
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200571static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
572{
573 struct ceph_x_authorizer *au = (void *)a;
574
575 ceph_x_authorizer_cleanup(au);
576 kfree(au);
577}
578
Sage Weilec0994e2010-02-02 16:25:35 -0800579static int ceph_x_create_authorizer(
580 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500581 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800582{
583 struct ceph_x_authorizer *au;
584 struct ceph_x_ticket_handler *th;
585 int ret;
586
587 th = get_ticket_handler(ac, peer_type);
588 if (IS_ERR(th))
589 return PTR_ERR(th);
590
591 au = kzalloc(sizeof(*au), GFP_NOFS);
592 if (!au)
593 return -ENOMEM;
594
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200595 au->base.destroy = ceph_x_destroy_authorizer;
596
Sage Weilec0994e2010-02-02 16:25:35 -0800597 ret = ceph_x_build_authorizer(ac, th, au);
598 if (ret) {
599 kfree(au);
600 return ret;
601 }
602
Alex Elder74f18692012-05-16 15:16:39 -0500603 auth->authorizer = (struct ceph_authorizer *) au;
604 auth->authorizer_buf = au->buf->vec.iov_base;
605 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100606 auth->authorizer_reply_buf = au->enc_buf;
607 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800608 auth->sign_message = ac->ops->sign_message;
609 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500610
Sage Weilec0994e2010-02-02 16:25:35 -0800611 return 0;
612}
613
Sage Weil0bed9b52013-03-25 10:26:01 -0700614static int ceph_x_update_authorizer(
615 struct ceph_auth_client *ac, int peer_type,
616 struct ceph_auth_handshake *auth)
617{
618 struct ceph_x_authorizer *au;
619 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700620
621 th = get_ticket_handler(ac, peer_type);
622 if (IS_ERR(th))
623 return PTR_ERR(th);
624
625 au = (struct ceph_x_authorizer *)auth->authorizer;
626 if (au->secret_id < th->secret_id) {
627 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
628 au->service, au->secret_id, th->secret_id);
629 return ceph_x_build_authorizer(ac, th, au);
630 }
631 return 0;
632}
633
Sage Weilec0994e2010-02-02 16:25:35 -0800634static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
635 struct ceph_authorizer *a, size_t len)
636{
637 struct ceph_x_authorizer *au = (void *)a;
Sage Weilec0994e2010-02-02 16:25:35 -0800638 int ret = 0;
639 struct ceph_x_authorize_reply reply;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400640 void *preply = &reply;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100641 void *p = au->enc_buf;
Sage Weilec0994e2010-02-02 16:25:35 -0800642
Ilya Dryomov7882a262016-12-02 16:35:07 +0100643 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN,
644 &preply, sizeof(reply));
Sage Weilec0994e2010-02-02 16:25:35 -0800645 if (ret < 0)
646 return ret;
647 if (ret != sizeof(reply))
648 return -EPERM;
649
650 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
651 ret = -EPERM;
652 else
653 ret = 0;
654 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
655 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
656 return ret;
657}
658
Sage Weilec0994e2010-02-02 16:25:35 -0800659static void ceph_x_reset(struct ceph_auth_client *ac)
660{
661 struct ceph_x_info *xi = ac->private;
662
663 dout("reset\n");
664 xi->starting = true;
665 xi->server_challenge = 0;
666}
667
668static void ceph_x_destroy(struct ceph_auth_client *ac)
669{
670 struct ceph_x_info *xi = ac->private;
671 struct rb_node *p;
672
673 dout("ceph_x_destroy %p\n", ac);
674 ceph_crypto_key_destroy(&xi->secret);
675
676 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
677 struct ceph_x_ticket_handler *th =
678 rb_entry(p, struct ceph_x_ticket_handler, node);
679 remove_ticket_handler(ac, th);
680 }
681
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100682 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700683
Sage Weilec0994e2010-02-02 16:25:35 -0800684 kfree(ac->private);
685 ac->private = NULL;
686}
687
Ilya Dryomov187d1312016-01-14 17:31:51 +0100688static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800689{
690 struct ceph_x_ticket_handler *th;
691
692 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200693 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100694 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800695}
696
Ilya Dryomov187d1312016-01-14 17:31:51 +0100697static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
698 int peer_type)
699{
700 /*
701 * We are to invalidate a service ticket in the hopes of
702 * getting a new, hopefully more valid, one. But, we won't get
703 * it unless our AUTH ticket is good, so invalidate AUTH ticket
704 * as well, just in case.
705 */
706 invalidate_ticket(ac, peer_type);
707 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
708}
709
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100710static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
711 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800712{
Yan, Zheng33d07332014-11-04 16:33:37 +0800713 char tmp_enc[40];
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100714 struct {
715 __le32 len;
716 __le32 header_crc;
717 __le32 front_crc;
718 __le32 middle_crc;
719 __le32 data_crc;
720 } __packed sigblock;
721 int ret;
722
723 sigblock.len = cpu_to_le32(4*sizeof(u32));
724 sigblock.header_crc = msg->hdr.crc;
725 sigblock.front_crc = msg->footer.front_crc;
726 sigblock.middle_crc = msg->footer.middle_crc;
727 sigblock.data_crc = msg->footer.data_crc;
728 ret = ceph_x_encrypt(&au->session_key, &sigblock, sizeof(sigblock),
Yan, Zheng33d07332014-11-04 16:33:37 +0800729 tmp_enc, sizeof(tmp_enc));
730 if (ret < 0)
731 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100732
733 *psig = *(__le64 *)(tmp_enc + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800734 return 0;
735}
736
737static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
738 struct ceph_msg *msg)
739{
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100740 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800741 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100742
Ilya Dryomova51983e2015-10-28 23:52:06 +0100743 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
744 return 0;
745
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100746 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
747 msg, &sig);
748 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800749 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100750
751 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800752 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
753 return 0;
754}
755
756static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
757 struct ceph_msg *msg)
758{
759 __le64 sig_check;
760 int ret;
761
Ilya Dryomova51983e2015-10-28 23:52:06 +0100762 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
763 return 0;
764
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100765 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
766 msg, &sig_check);
767 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800768 return ret;
769 if (sig_check == msg->footer.sig)
770 return 0;
771 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
772 dout("ceph_x_check_message_signature %p has signature %llx "
773 "expect %llx\n", msg, msg->footer.sig, sig_check);
774 else
775 dout("ceph_x_check_message_signature %p sender did not set "
776 "CEPH_MSG_FOOTER_SIGNED\n", msg);
777 return -EBADMSG;
778}
Sage Weilec0994e2010-02-02 16:25:35 -0800779
780static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700781 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800782 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700783 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800784 .build_request = ceph_x_build_request,
785 .handle_reply = ceph_x_handle_reply,
786 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700787 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800788 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800789 .invalidate_authorizer = ceph_x_invalidate_authorizer,
790 .reset = ceph_x_reset,
791 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800792 .sign_message = ceph_x_sign_message,
793 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800794};
795
796
797int ceph_x_init(struct ceph_auth_client *ac)
798{
799 struct ceph_x_info *xi;
800 int ret;
801
802 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700803 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800804 xi = kzalloc(sizeof(*xi), GFP_NOFS);
805 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700806 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800807
Sage Weilec0994e2010-02-02 16:25:35 -0800808 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700809 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800810 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700811 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800812 }
813
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700814 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
815 if (ret < 0) {
816 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700817 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700818 }
Sage Weilec0994e2010-02-02 16:25:35 -0800819
820 xi->starting = true;
821 xi->ticket_handlers = RB_ROOT;
822
823 ac->protocol = CEPH_AUTH_CEPHX;
824 ac->private = xi;
825 ac->ops = &ceph_x_ops;
826 return 0;
827
Sage Weilb0930f82010-04-29 13:26:53 -0700828out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800829 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700830out:
Sage Weilec0994e2010-02-02 16:25:35 -0800831 return ret;
832}
833
834