Commit | Line | Data |
---|---|---|
ec0994e4 | 1 | |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
ec0994e4 SW |
3 | |
4 | #include <linux/err.h> | |
5 | #include <linux/module.h> | |
6 | #include <linux/random.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
ec0994e4 | 8 | |
3d14c5d2 YS |
9 | #include <linux/ceph/decode.h> |
10 | #include <linux/ceph/auth.h> | |
33d07337 | 11 | #include <linux/ceph/messenger.h> |
3d14c5d2 YS |
12 | |
13 | #include "crypto.h" | |
ec0994e4 SW |
14 | #include "auth_x.h" |
15 | #include "auth_x_protocol.h" | |
ec0994e4 | 16 | |
ec0994e4 SW |
17 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); |
18 | ||
19 | static 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 | ||
a41359fa SW |
30 | static 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 | ||
807c86e2 SW |
41 | static int ceph_x_encrypt_buflen(int ilen) |
42 | { | |
43 | return sizeof(struct ceph_x_encrypt_header) + ilen + 16 + | |
44 | sizeof(u32); | |
45 | } | |
46 | ||
ec0994e4 SW |
47 | static 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 | ||
65 | static int ceph_x_decrypt(struct ceph_crypto_key *secret, | |
c27a3e4d | 66 | void **p, void *end, void **obuf, size_t olen) |
ec0994e4 SW |
67 | { |
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); | |
c27a3e4d ID |
77 | 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); | |
ec0994e4 SW |
85 | 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 | */ | |
cd84db6e YS |
96 | static struct ceph_x_ticket_handler * |
97 | get_ticket_handler(struct ceph_auth_client *ac, int service) | |
ec0994e4 SW |
98 | { |
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 | ||
124 | static 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 | ||
597cda35 ID |
137 | static int process_one_ticket(struct ceph_auth_client *ac, |
138 | struct ceph_crypto_key *secret, | |
c27a3e4d | 139 | void **p, void *end) |
597cda35 ID |
140 | { |
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; | |
c27a3e4d | 145 | void *dbuf = NULL; |
597cda35 ID |
146 | void *dp, *dend; |
147 | int dlen; | |
148 | char is_enc; | |
149 | struct timespec validity; | |
150 | struct ceph_crypto_key old_key; | |
c27a3e4d | 151 | void *ticket_buf = NULL; |
597cda35 | 152 | void *tp, *tpend; |
e9226d7c | 153 | void **ptp; |
597cda35 ID |
154 | 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 */ | |
c27a3e4d | 177 | dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0); |
597cda35 ID |
178 | 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); | |
597cda35 ID |
204 | if (is_enc) { |
205 | /* encrypted */ | |
206 | dout(" encrypted ticket\n"); | |
c27a3e4d | 207 | dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0); |
597cda35 ID |
208 | if (dlen < 0) { |
209 | ret = dlen; | |
210 | goto out; | |
211 | } | |
c27a3e4d | 212 | tp = ticket_buf; |
e9226d7c ID |
213 | ptp = &tp; |
214 | tpend = *ptp + dlen; | |
597cda35 ID |
215 | } else { |
216 | /* unencrypted */ | |
e9226d7c ID |
217 | ptp = p; |
218 | tpend = end; | |
597cda35 | 219 | } |
e9226d7c | 220 | ceph_decode_32_safe(ptp, tpend, dlen, bad); |
597cda35 | 221 | dout(" ticket blob is %d bytes\n", dlen); |
e9226d7c ID |
222 | 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); | |
597cda35 ID |
226 | 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 | ||
244 | out: | |
c27a3e4d ID |
245 | kfree(ticket_buf); |
246 | kfree(dbuf); | |
597cda35 ID |
247 | return ret; |
248 | ||
249 | bad: | |
250 | ret = -EINVAL; | |
251 | goto out; | |
252 | } | |
253 | ||
ec0994e4 SW |
254 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, |
255 | struct ceph_crypto_key *secret, | |
256 | void *buf, void *end) | |
257 | { | |
ec0994e4 | 258 | void *p = buf; |
b736b3d9 | 259 | u8 reply_struct_v; |
597cda35 ID |
260 | u32 num; |
261 | int ret; | |
ec0994e4 | 262 | |
597cda35 | 263 | ceph_decode_8_safe(&p, end, reply_struct_v, bad); |
b736b3d9 | 264 | if (reply_struct_v != 1) |
597cda35 | 265 | return -EINVAL; |
ec0994e4 | 266 | |
597cda35 ID |
267 | ceph_decode_32_safe(&p, end, num, bad); |
268 | dout("%d tickets\n", num); | |
ec0994e4 | 269 | |
597cda35 | 270 | while (num--) { |
c27a3e4d | 271 | ret = process_one_ticket(ac, secret, &p, end); |
ec0994e4 | 272 | if (ret) |
c27a3e4d | 273 | return ret; |
ec0994e4 SW |
274 | } |
275 | ||
c27a3e4d | 276 | return 0; |
ec0994e4 SW |
277 | |
278 | bad: | |
c27a3e4d | 279 | return -EINVAL; |
ec0994e4 SW |
280 | } |
281 | ||
cbf99a11 ID |
282 | static 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 | ||
ec0994e4 SW |
291 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac, |
292 | struct ceph_x_ticket_handler *th, | |
293 | struct ceph_x_authorizer *au) | |
294 | { | |
807c86e2 | 295 | int maxlen; |
ec0994e4 SW |
296 | 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 | ||
ae385eaf YZ |
306 | ceph_crypto_key_destroy(&au->session_key); |
307 | ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); | |
308 | if (ret) | |
cbf99a11 | 309 | goto out_au; |
ae385eaf | 310 | |
807c86e2 SW |
311 | 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) { | |
ec0994e4 SW |
315 | ceph_buffer_put(au->buf); |
316 | au->buf = NULL; | |
317 | } | |
318 | if (!au->buf) { | |
807c86e2 | 319 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); |
ae385eaf | 320 | if (!au->buf) { |
cbf99a11 ID |
321 | ret = -ENOMEM; |
322 | goto out_au; | |
ae385eaf | 323 | } |
ec0994e4 SW |
324 | } |
325 | au->service = th->service; | |
0bed9b5c | 326 | au->secret_id = th->secret_id; |
ec0994e4 SW |
327 | |
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); | |
ae385eaf | 349 | ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b), |
ec0994e4 SW |
350 | p, end - p); |
351 | if (ret < 0) | |
cbf99a11 | 352 | goto out_au; |
ec0994e4 SW |
353 | 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); | |
807c86e2 | 357 | BUG_ON(au->buf->vec.iov_len > maxlen); |
ec0994e4 SW |
358 | return 0; |
359 | ||
cbf99a11 ID |
360 | out_au: |
361 | ceph_x_authorizer_cleanup(au); | |
ec0994e4 SW |
362 | return ret; |
363 | } | |
364 | ||
365 | static 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; | |
382 | bad: | |
383 | return -ERANGE; | |
384 | } | |
385 | ||
386 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) | |
387 | { | |
388 | int want = ac->want_keys; | |
389 | struct ceph_x_info *xi = ac->private; | |
390 | int service; | |
391 | ||
392 | *pneed = ac->want_keys & ~(xi->have_keys); | |
393 | ||
394 | for (service = 1; service <= want; service <<= 1) { | |
395 | struct ceph_x_ticket_handler *th; | |
396 | ||
397 | if (!(ac->want_keys & service)) | |
398 | continue; | |
399 | ||
400 | if (*pneed & service) | |
401 | continue; | |
402 | ||
403 | th = get_ticket_handler(ac, service); | |
404 | ||
b545787d | 405 | if (IS_ERR(th)) { |
ec0994e4 SW |
406 | *pneed |= service; |
407 | continue; | |
408 | } | |
409 | ||
410 | if (get_seconds() >= th->renew_after) | |
411 | *pneed |= service; | |
412 | if (get_seconds() >= th->expires) | |
413 | xi->have_keys &= ~service; | |
414 | } | |
415 | } | |
416 | ||
417 | ||
418 | static int ceph_x_build_request(struct ceph_auth_client *ac, | |
419 | void *buf, void *end) | |
420 | { | |
421 | struct ceph_x_info *xi = ac->private; | |
422 | int need; | |
423 | struct ceph_x_request_header *head = buf; | |
424 | int ret; | |
425 | struct ceph_x_ticket_handler *th = | |
426 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | |
427 | ||
b545787d DC |
428 | if (IS_ERR(th)) |
429 | return PTR_ERR(th); | |
430 | ||
ec0994e4 SW |
431 | ceph_x_validate_tickets(ac, &need); |
432 | ||
433 | dout("build_request want %x have %x need %x\n", | |
434 | ac->want_keys, xi->have_keys, need); | |
435 | ||
436 | if (need & CEPH_ENTITY_TYPE_AUTH) { | |
437 | struct ceph_x_authenticate *auth = (void *)(head + 1); | |
438 | void *p = auth + 1; | |
439 | struct ceph_x_challenge_blob tmp; | |
440 | char tmp_enc[40]; | |
441 | u64 *u; | |
442 | ||
443 | if (p > end) | |
444 | return -ERANGE; | |
445 | ||
446 | dout(" get_auth_session_key\n"); | |
447 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); | |
448 | ||
449 | /* encrypt and hash */ | |
450 | get_random_bytes(&auth->client_challenge, sizeof(u64)); | |
451 | tmp.client_challenge = auth->client_challenge; | |
452 | tmp.server_challenge = cpu_to_le64(xi->server_challenge); | |
453 | ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp), | |
454 | tmp_enc, sizeof(tmp_enc)); | |
455 | if (ret < 0) | |
456 | return ret; | |
457 | ||
458 | auth->struct_v = 1; | |
459 | auth->key = 0; | |
460 | for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++) | |
cd84db6e | 461 | auth->key ^= *(__le64 *)u; |
ec0994e4 SW |
462 | dout(" server_challenge %llx client_challenge %llx key %llx\n", |
463 | xi->server_challenge, le64_to_cpu(auth->client_challenge), | |
464 | le64_to_cpu(auth->key)); | |
465 | ||
466 | /* now encode the old ticket if exists */ | |
467 | ret = ceph_x_encode_ticket(th, &p, end); | |
468 | if (ret < 0) | |
469 | return ret; | |
470 | ||
471 | return p - buf; | |
472 | } | |
473 | ||
474 | if (need) { | |
475 | void *p = head + 1; | |
476 | struct ceph_x_service_ticket_request *req; | |
477 | ||
478 | if (p > end) | |
479 | return -ERANGE; | |
480 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); | |
481 | ||
ec0994e4 SW |
482 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); |
483 | if (ret) | |
484 | return ret; | |
485 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, | |
486 | xi->auth_authorizer.buf->vec.iov_len); | |
487 | ||
488 | req = p; | |
489 | req->keys = cpu_to_le32(need); | |
490 | p += sizeof(*req); | |
491 | return p - buf; | |
492 | } | |
493 | ||
494 | return 0; | |
495 | } | |
496 | ||
497 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, | |
498 | void *buf, void *end) | |
499 | { | |
500 | struct ceph_x_info *xi = ac->private; | |
501 | struct ceph_x_reply_header *head = buf; | |
502 | struct ceph_x_ticket_handler *th; | |
503 | int len = end - buf; | |
504 | int op; | |
505 | int ret; | |
506 | ||
507 | if (result) | |
508 | return result; /* XXX hmm? */ | |
509 | ||
510 | if (xi->starting) { | |
511 | /* it's a hello */ | |
512 | struct ceph_x_server_challenge *sc = buf; | |
513 | ||
514 | if (len != sizeof(*sc)) | |
515 | return -EINVAL; | |
516 | xi->server_challenge = le64_to_cpu(sc->server_challenge); | |
517 | dout("handle_reply got server challenge %llx\n", | |
518 | xi->server_challenge); | |
519 | xi->starting = false; | |
520 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; | |
521 | return -EAGAIN; | |
522 | } | |
523 | ||
0cf5537b | 524 | op = le16_to_cpu(head->op); |
ec0994e4 SW |
525 | result = le32_to_cpu(head->result); |
526 | dout("handle_reply op %d result %d\n", op, result); | |
527 | switch (op) { | |
528 | case CEPHX_GET_AUTH_SESSION_KEY: | |
529 | /* verify auth key */ | |
530 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, | |
531 | buf + sizeof(*head), end); | |
532 | break; | |
533 | ||
534 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: | |
535 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | |
b545787d DC |
536 | if (IS_ERR(th)) |
537 | return PTR_ERR(th); | |
ec0994e4 SW |
538 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, |
539 | buf + sizeof(*head), end); | |
540 | break; | |
541 | ||
542 | default: | |
543 | return -EINVAL; | |
544 | } | |
545 | if (ret) | |
546 | return ret; | |
547 | if (ac->want_keys == xi->have_keys) | |
548 | return 0; | |
549 | return -EAGAIN; | |
550 | } | |
551 | ||
552 | static int ceph_x_create_authorizer( | |
553 | struct ceph_auth_client *ac, int peer_type, | |
74f1869f | 554 | struct ceph_auth_handshake *auth) |
ec0994e4 SW |
555 | { |
556 | struct ceph_x_authorizer *au; | |
557 | struct ceph_x_ticket_handler *th; | |
558 | int ret; | |
559 | ||
560 | th = get_ticket_handler(ac, peer_type); | |
561 | if (IS_ERR(th)) | |
562 | return PTR_ERR(th); | |
563 | ||
564 | au = kzalloc(sizeof(*au), GFP_NOFS); | |
565 | if (!au) | |
566 | return -ENOMEM; | |
567 | ||
568 | ret = ceph_x_build_authorizer(ac, th, au); | |
569 | if (ret) { | |
570 | kfree(au); | |
571 | return ret; | |
572 | } | |
573 | ||
74f1869f AE |
574 | auth->authorizer = (struct ceph_authorizer *) au; |
575 | auth->authorizer_buf = au->buf->vec.iov_base; | |
576 | auth->authorizer_buf_len = au->buf->vec.iov_len; | |
577 | auth->authorizer_reply_buf = au->reply_buf; | |
578 | auth->authorizer_reply_buf_len = sizeof (au->reply_buf); | |
33d07337 YZ |
579 | auth->sign_message = ac->ops->sign_message; |
580 | auth->check_message_signature = ac->ops->check_message_signature; | |
74f1869f | 581 | |
ec0994e4 SW |
582 | return 0; |
583 | } | |
584 | ||
0bed9b5c SW |
585 | static int ceph_x_update_authorizer( |
586 | struct ceph_auth_client *ac, int peer_type, | |
587 | struct ceph_auth_handshake *auth) | |
588 | { | |
589 | struct ceph_x_authorizer *au; | |
590 | struct ceph_x_ticket_handler *th; | |
0bed9b5c SW |
591 | |
592 | th = get_ticket_handler(ac, peer_type); | |
593 | if (IS_ERR(th)) | |
594 | return PTR_ERR(th); | |
595 | ||
596 | au = (struct ceph_x_authorizer *)auth->authorizer; | |
597 | if (au->secret_id < th->secret_id) { | |
598 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", | |
599 | au->service, au->secret_id, th->secret_id); | |
600 | return ceph_x_build_authorizer(ac, th, au); | |
601 | } | |
602 | return 0; | |
603 | } | |
604 | ||
ec0994e4 SW |
605 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, |
606 | struct ceph_authorizer *a, size_t len) | |
607 | { | |
608 | struct ceph_x_authorizer *au = (void *)a; | |
ec0994e4 SW |
609 | int ret = 0; |
610 | struct ceph_x_authorize_reply reply; | |
c27a3e4d | 611 | void *preply = &reply; |
ec0994e4 SW |
612 | void *p = au->reply_buf; |
613 | void *end = p + sizeof(au->reply_buf); | |
614 | ||
ae385eaf | 615 | ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply)); |
ec0994e4 SW |
616 | if (ret < 0) |
617 | return ret; | |
618 | if (ret != sizeof(reply)) | |
619 | return -EPERM; | |
620 | ||
621 | if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one)) | |
622 | ret = -EPERM; | |
623 | else | |
624 | ret = 0; | |
625 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", | |
626 | au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); | |
627 | return ret; | |
628 | } | |
629 | ||
630 | static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac, | |
631 | struct ceph_authorizer *a) | |
632 | { | |
633 | struct ceph_x_authorizer *au = (void *)a; | |
634 | ||
cbf99a11 | 635 | ceph_x_authorizer_cleanup(au); |
ec0994e4 SW |
636 | kfree(au); |
637 | } | |
638 | ||
639 | ||
640 | static void ceph_x_reset(struct ceph_auth_client *ac) | |
641 | { | |
642 | struct ceph_x_info *xi = ac->private; | |
643 | ||
644 | dout("reset\n"); | |
645 | xi->starting = true; | |
646 | xi->server_challenge = 0; | |
647 | } | |
648 | ||
649 | static void ceph_x_destroy(struct ceph_auth_client *ac) | |
650 | { | |
651 | struct ceph_x_info *xi = ac->private; | |
652 | struct rb_node *p; | |
653 | ||
654 | dout("ceph_x_destroy %p\n", ac); | |
655 | ceph_crypto_key_destroy(&xi->secret); | |
656 | ||
657 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { | |
658 | struct ceph_x_ticket_handler *th = | |
659 | rb_entry(p, struct ceph_x_ticket_handler, node); | |
660 | remove_ticket_handler(ac, th); | |
661 | } | |
662 | ||
cbf99a11 | 663 | ceph_x_authorizer_cleanup(&xi->auth_authorizer); |
22b1de06 | 664 | |
ec0994e4 SW |
665 | kfree(ac->private); |
666 | ac->private = NULL; | |
667 | } | |
668 | ||
669 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, | |
670 | int peer_type) | |
671 | { | |
672 | struct ceph_x_ticket_handler *th; | |
673 | ||
674 | th = get_ticket_handler(ac, peer_type); | |
b545787d | 675 | if (!IS_ERR(th)) |
4b8e8b5d | 676 | memset(&th->validity, 0, sizeof(th->validity)); |
ec0994e4 SW |
677 | } |
678 | ||
33d07337 YZ |
679 | static int calcu_signature(struct ceph_x_authorizer *au, |
680 | struct ceph_msg *msg, __le64 *sig) | |
681 | { | |
682 | int ret; | |
683 | char tmp_enc[40]; | |
684 | __le32 tmp[5] = { | |
d7d5a007 | 685 | cpu_to_le32(16), msg->hdr.crc, msg->footer.front_crc, |
33d07337 YZ |
686 | msg->footer.middle_crc, msg->footer.data_crc, |
687 | }; | |
688 | ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp), | |
689 | tmp_enc, sizeof(tmp_enc)); | |
690 | if (ret < 0) | |
691 | return ret; | |
692 | *sig = *(__le64*)(tmp_enc + 4); | |
693 | return 0; | |
694 | } | |
695 | ||
696 | static int ceph_x_sign_message(struct ceph_auth_handshake *auth, | |
697 | struct ceph_msg *msg) | |
698 | { | |
699 | int ret; | |
700 | if (!auth->authorizer) | |
701 | return 0; | |
702 | ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer, | |
703 | msg, &msg->footer.sig); | |
704 | if (ret < 0) | |
705 | return ret; | |
706 | msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED; | |
707 | return 0; | |
708 | } | |
709 | ||
710 | static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth, | |
711 | struct ceph_msg *msg) | |
712 | { | |
713 | __le64 sig_check; | |
714 | int ret; | |
715 | ||
716 | if (!auth->authorizer) | |
717 | return 0; | |
718 | ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer, | |
719 | msg, &sig_check); | |
720 | if (ret < 0) | |
721 | return ret; | |
722 | if (sig_check == msg->footer.sig) | |
723 | return 0; | |
724 | if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED) | |
725 | dout("ceph_x_check_message_signature %p has signature %llx " | |
726 | "expect %llx\n", msg, msg->footer.sig, sig_check); | |
727 | else | |
728 | dout("ceph_x_check_message_signature %p sender did not set " | |
729 | "CEPH_MSG_FOOTER_SIGNED\n", msg); | |
730 | return -EBADMSG; | |
731 | } | |
ec0994e4 SW |
732 | |
733 | static const struct ceph_auth_client_ops ceph_x_ops = { | |
559c1e00 | 734 | .name = "x", |
ec0994e4 | 735 | .is_authenticated = ceph_x_is_authenticated, |
a41359fa | 736 | .should_authenticate = ceph_x_should_authenticate, |
ec0994e4 SW |
737 | .build_request = ceph_x_build_request, |
738 | .handle_reply = ceph_x_handle_reply, | |
739 | .create_authorizer = ceph_x_create_authorizer, | |
0bed9b5c | 740 | .update_authorizer = ceph_x_update_authorizer, |
ec0994e4 SW |
741 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, |
742 | .destroy_authorizer = ceph_x_destroy_authorizer, | |
743 | .invalidate_authorizer = ceph_x_invalidate_authorizer, | |
744 | .reset = ceph_x_reset, | |
745 | .destroy = ceph_x_destroy, | |
33d07337 YZ |
746 | .sign_message = ceph_x_sign_message, |
747 | .check_message_signature = ceph_x_check_message_signature, | |
ec0994e4 SW |
748 | }; |
749 | ||
750 | ||
751 | int ceph_x_init(struct ceph_auth_client *ac) | |
752 | { | |
753 | struct ceph_x_info *xi; | |
754 | int ret; | |
755 | ||
756 | dout("ceph_x_init %p\n", ac); | |
b0930f8d | 757 | ret = -ENOMEM; |
ec0994e4 SW |
758 | xi = kzalloc(sizeof(*xi), GFP_NOFS); |
759 | if (!xi) | |
b0930f8d | 760 | goto out; |
ec0994e4 | 761 | |
ec0994e4 | 762 | ret = -EINVAL; |
8323c3aa | 763 | if (!ac->key) { |
ec0994e4 | 764 | pr_err("no secret set (for auth_x protocol)\n"); |
b0930f8d | 765 | goto out_nomem; |
ec0994e4 SW |
766 | } |
767 | ||
8323c3aa TV |
768 | ret = ceph_crypto_key_clone(&xi->secret, ac->key); |
769 | if (ret < 0) { | |
770 | pr_err("cannot clone key: %d\n", ret); | |
b0930f8d | 771 | goto out_nomem; |
8323c3aa | 772 | } |
ec0994e4 SW |
773 | |
774 | xi->starting = true; | |
775 | xi->ticket_handlers = RB_ROOT; | |
776 | ||
777 | ac->protocol = CEPH_AUTH_CEPHX; | |
778 | ac->private = xi; | |
779 | ac->ops = &ceph_x_ops; | |
780 | return 0; | |
781 | ||
b0930f8d | 782 | out_nomem: |
ec0994e4 | 783 | kfree(xi); |
b0930f8d | 784 | out: |
ec0994e4 SW |
785 | return ret; |
786 | } | |
787 | ||
788 |