]>
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> | |
a51983e4 | 11 | #include <linux/ceph/libceph.h> |
33d07337 | 12 | #include <linux/ceph/messenger.h> |
3d14c5d2 YS |
13 | |
14 | #include "crypto.h" | |
ec0994e4 SW |
15 | #include "auth_x.h" |
16 | #include "auth_x_protocol.h" | |
ec0994e4 | 17 | |
ec0994e4 SW |
18 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); |
19 | ||
20 | static 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 | ||
a41359fa SW |
31 | static 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 | ||
55d9cc83 ID |
42 | static int ceph_x_encrypt_offset(void) |
43 | { | |
44 | return sizeof(u32) + sizeof(struct ceph_x_encrypt_header); | |
45 | } | |
46 | ||
807c86e2 SW |
47 | static int ceph_x_encrypt_buflen(int ilen) |
48 | { | |
55d9cc83 | 49 | return ceph_x_encrypt_offset() + ilen + 16; |
807c86e2 SW |
50 | } |
51 | ||
d03857c6 ID |
52 | static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf, |
53 | int buf_len, int plaintext_len) | |
ec0994e4 | 54 | { |
d03857c6 ID |
55 | struct ceph_x_encrypt_header *hdr = buf + sizeof(u32); |
56 | int ciphertext_len; | |
ec0994e4 SW |
57 | int ret; |
58 | ||
d03857c6 ID |
59 | hdr->struct_v = 1; |
60 | hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC); | |
61 | ||
62 | ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32), | |
63 | plaintext_len + sizeof(struct ceph_x_encrypt_header), | |
64 | &ciphertext_len); | |
ec0994e4 SW |
65 | if (ret) |
66 | return ret; | |
d03857c6 ID |
67 | |
68 | ceph_encode_32(&buf, ciphertext_len); | |
69 | return sizeof(u32) + ciphertext_len; | |
ec0994e4 SW |
70 | } |
71 | ||
72 | static int ceph_x_decrypt(struct ceph_crypto_key *secret, | |
c27a3e4d | 73 | void **p, void *end, void **obuf, size_t olen) |
ec0994e4 SW |
74 | { |
75 | struct ceph_x_encrypt_header head; | |
76 | size_t head_len = sizeof(head); | |
77 | int len, ret; | |
78 | ||
79 | len = ceph_decode_32(p); | |
80 | if (*p + len > end) | |
81 | return -EINVAL; | |
82 | ||
83 | dout("ceph_x_decrypt len %d\n", len); | |
c27a3e4d ID |
84 | if (*obuf == NULL) { |
85 | *obuf = kmalloc(len, GFP_NOFS); | |
86 | if (!*obuf) | |
87 | return -ENOMEM; | |
88 | olen = len; | |
89 | } | |
90 | ||
91 | ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len); | |
ec0994e4 SW |
92 | if (ret) |
93 | return ret; | |
94 | if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC) | |
95 | return -EPERM; | |
96 | *p += len; | |
97 | return olen; | |
98 | } | |
99 | ||
100 | /* | |
101 | * get existing (or insert new) ticket handler | |
102 | */ | |
cd84db6e YS |
103 | static struct ceph_x_ticket_handler * |
104 | get_ticket_handler(struct ceph_auth_client *ac, int service) | |
ec0994e4 SW |
105 | { |
106 | struct ceph_x_ticket_handler *th; | |
107 | struct ceph_x_info *xi = ac->private; | |
108 | struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; | |
109 | ||
110 | while (*p) { | |
111 | parent = *p; | |
112 | th = rb_entry(parent, struct ceph_x_ticket_handler, node); | |
113 | if (service < th->service) | |
114 | p = &(*p)->rb_left; | |
115 | else if (service > th->service) | |
116 | p = &(*p)->rb_right; | |
117 | else | |
118 | return th; | |
119 | } | |
120 | ||
121 | /* add it */ | |
122 | th = kzalloc(sizeof(*th), GFP_NOFS); | |
123 | if (!th) | |
124 | return ERR_PTR(-ENOMEM); | |
125 | th->service = service; | |
126 | rb_link_node(&th->node, parent, p); | |
127 | rb_insert_color(&th->node, &xi->ticket_handlers); | |
128 | return th; | |
129 | } | |
130 | ||
131 | static void remove_ticket_handler(struct ceph_auth_client *ac, | |
132 | struct ceph_x_ticket_handler *th) | |
133 | { | |
134 | struct ceph_x_info *xi = ac->private; | |
135 | ||
136 | dout("remove_ticket_handler %p %d\n", th, th->service); | |
137 | rb_erase(&th->node, &xi->ticket_handlers); | |
138 | ceph_crypto_key_destroy(&th->session_key); | |
139 | if (th->ticket_blob) | |
140 | ceph_buffer_put(th->ticket_blob); | |
141 | kfree(th); | |
142 | } | |
143 | ||
597cda35 ID |
144 | static int process_one_ticket(struct ceph_auth_client *ac, |
145 | struct ceph_crypto_key *secret, | |
c27a3e4d | 146 | void **p, void *end) |
597cda35 ID |
147 | { |
148 | struct ceph_x_info *xi = ac->private; | |
149 | int type; | |
150 | u8 tkt_struct_v, blob_struct_v; | |
151 | struct ceph_x_ticket_handler *th; | |
c27a3e4d | 152 | void *dbuf = NULL; |
597cda35 ID |
153 | void *dp, *dend; |
154 | int dlen; | |
155 | char is_enc; | |
156 | struct timespec validity; | |
c27a3e4d | 157 | void *ticket_buf = NULL; |
597cda35 | 158 | void *tp, *tpend; |
e9226d7c | 159 | void **ptp; |
597cda35 ID |
160 | struct ceph_crypto_key new_session_key; |
161 | struct ceph_buffer *new_ticket_blob; | |
162 | unsigned long new_expires, new_renew_after; | |
163 | u64 new_secret_id; | |
164 | int ret; | |
165 | ||
166 | ceph_decode_need(p, end, sizeof(u32) + 1, bad); | |
167 | ||
168 | type = ceph_decode_32(p); | |
169 | dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); | |
170 | ||
171 | tkt_struct_v = ceph_decode_8(p); | |
172 | if (tkt_struct_v != 1) | |
173 | goto bad; | |
174 | ||
175 | th = get_ticket_handler(ac, type); | |
176 | if (IS_ERR(th)) { | |
177 | ret = PTR_ERR(th); | |
178 | goto out; | |
179 | } | |
180 | ||
181 | /* blob for me */ | |
c27a3e4d | 182 | dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0); |
597cda35 ID |
183 | if (dlen <= 0) { |
184 | ret = dlen; | |
185 | goto out; | |
186 | } | |
187 | dout(" decrypted %d bytes\n", dlen); | |
188 | dp = dbuf; | |
189 | dend = dp + dlen; | |
190 | ||
191 | tkt_struct_v = ceph_decode_8(&dp); | |
192 | if (tkt_struct_v != 1) | |
193 | goto bad; | |
194 | ||
597cda35 ID |
195 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); |
196 | if (ret) | |
197 | goto out; | |
198 | ||
f6cdb292 ID |
199 | ceph_decode_timespec(&validity, dp); |
200 | dp += sizeof(struct ceph_timespec); | |
597cda35 ID |
201 | new_expires = get_seconds() + validity.tv_sec; |
202 | new_renew_after = new_expires - (validity.tv_sec / 4); | |
203 | dout(" expires=%lu renew_after=%lu\n", new_expires, | |
204 | new_renew_after); | |
205 | ||
206 | /* ticket blob for service */ | |
207 | ceph_decode_8_safe(p, end, is_enc, bad); | |
597cda35 ID |
208 | if (is_enc) { |
209 | /* encrypted */ | |
210 | dout(" encrypted ticket\n"); | |
462e6504 | 211 | dlen = ceph_x_decrypt(&th->session_key, p, end, &ticket_buf, 0); |
597cda35 ID |
212 | if (dlen < 0) { |
213 | ret = dlen; | |
214 | goto out; | |
215 | } | |
c27a3e4d | 216 | tp = ticket_buf; |
e9226d7c ID |
217 | ptp = &tp; |
218 | tpend = *ptp + dlen; | |
597cda35 ID |
219 | } else { |
220 | /* unencrypted */ | |
e9226d7c ID |
221 | ptp = p; |
222 | tpend = end; | |
597cda35 | 223 | } |
e9226d7c | 224 | ceph_decode_32_safe(ptp, tpend, dlen, bad); |
597cda35 | 225 | dout(" ticket blob is %d bytes\n", dlen); |
e9226d7c ID |
226 | ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad); |
227 | blob_struct_v = ceph_decode_8(ptp); | |
228 | new_secret_id = ceph_decode_64(ptp); | |
229 | ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend); | |
597cda35 ID |
230 | if (ret) |
231 | goto out; | |
232 | ||
233 | /* all is well, update our ticket */ | |
234 | ceph_crypto_key_destroy(&th->session_key); | |
235 | if (th->ticket_blob) | |
236 | ceph_buffer_put(th->ticket_blob); | |
237 | th->session_key = new_session_key; | |
238 | th->ticket_blob = new_ticket_blob; | |
597cda35 ID |
239 | th->secret_id = new_secret_id; |
240 | th->expires = new_expires; | |
241 | th->renew_after = new_renew_after; | |
6abe097d | 242 | th->have_key = true; |
597cda35 ID |
243 | dout(" got ticket service %d (%s) secret_id %lld len %d\n", |
244 | type, ceph_entity_type_name(type), th->secret_id, | |
245 | (int)th->ticket_blob->vec.iov_len); | |
246 | xi->have_keys |= th->service; | |
247 | ||
248 | out: | |
c27a3e4d ID |
249 | kfree(ticket_buf); |
250 | kfree(dbuf); | |
597cda35 ID |
251 | return ret; |
252 | ||
253 | bad: | |
254 | ret = -EINVAL; | |
255 | goto out; | |
256 | } | |
257 | ||
ec0994e4 SW |
258 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, |
259 | struct ceph_crypto_key *secret, | |
260 | void *buf, void *end) | |
261 | { | |
ec0994e4 | 262 | void *p = buf; |
b736b3d9 | 263 | u8 reply_struct_v; |
597cda35 ID |
264 | u32 num; |
265 | int ret; | |
ec0994e4 | 266 | |
597cda35 | 267 | ceph_decode_8_safe(&p, end, reply_struct_v, bad); |
b736b3d9 | 268 | if (reply_struct_v != 1) |
597cda35 | 269 | return -EINVAL; |
ec0994e4 | 270 | |
597cda35 ID |
271 | ceph_decode_32_safe(&p, end, num, bad); |
272 | dout("%d tickets\n", num); | |
ec0994e4 | 273 | |
597cda35 | 274 | while (num--) { |
c27a3e4d | 275 | ret = process_one_ticket(ac, secret, &p, end); |
ec0994e4 | 276 | if (ret) |
c27a3e4d | 277 | return ret; |
ec0994e4 SW |
278 | } |
279 | ||
c27a3e4d | 280 | return 0; |
ec0994e4 SW |
281 | |
282 | bad: | |
c27a3e4d | 283 | return -EINVAL; |
ec0994e4 SW |
284 | } |
285 | ||
cbf99a11 ID |
286 | static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au) |
287 | { | |
288 | ceph_crypto_key_destroy(&au->session_key); | |
289 | if (au->buf) { | |
290 | ceph_buffer_put(au->buf); | |
291 | au->buf = NULL; | |
292 | } | |
293 | } | |
294 | ||
ec0994e4 SW |
295 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac, |
296 | struct ceph_x_ticket_handler *th, | |
297 | struct ceph_x_authorizer *au) | |
298 | { | |
807c86e2 | 299 | int maxlen; |
ec0994e4 | 300 | struct ceph_x_authorize_a *msg_a; |
d03857c6 | 301 | struct ceph_x_authorize_b *msg_b; |
ec0994e4 SW |
302 | void *p, *end; |
303 | int ret; | |
304 | int ticket_blob_len = | |
305 | (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); | |
306 | ||
307 | dout("build_authorizer for %s %p\n", | |
308 | ceph_entity_type_name(th->service), au); | |
309 | ||
ae385eaf YZ |
310 | ceph_crypto_key_destroy(&au->session_key); |
311 | ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); | |
312 | if (ret) | |
cbf99a11 | 313 | goto out_au; |
ae385eaf | 314 | |
36721ece | 315 | maxlen = sizeof(*msg_a) + ticket_blob_len + |
d03857c6 | 316 | ceph_x_encrypt_buflen(sizeof(*msg_b)); |
807c86e2 SW |
317 | dout(" need len %d\n", maxlen); |
318 | if (au->buf && au->buf->alloc_len < maxlen) { | |
ec0994e4 SW |
319 | ceph_buffer_put(au->buf); |
320 | au->buf = NULL; | |
321 | } | |
322 | if (!au->buf) { | |
807c86e2 | 323 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); |
ae385eaf | 324 | if (!au->buf) { |
cbf99a11 ID |
325 | ret = -ENOMEM; |
326 | goto out_au; | |
ae385eaf | 327 | } |
ec0994e4 SW |
328 | } |
329 | au->service = th->service; | |
0bed9b5c | 330 | au->secret_id = th->secret_id; |
ec0994e4 SW |
331 | |
332 | msg_a = au->buf->vec.iov_base; | |
333 | msg_a->struct_v = 1; | |
334 | msg_a->global_id = cpu_to_le64(ac->global_id); | |
335 | msg_a->service_id = cpu_to_le32(th->service); | |
336 | msg_a->ticket_blob.struct_v = 1; | |
337 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); | |
338 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); | |
339 | if (ticket_blob_len) { | |
340 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, | |
341 | th->ticket_blob->vec.iov_len); | |
342 | } | |
343 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id, | |
344 | le64_to_cpu(msg_a->ticket_blob.secret_id)); | |
345 | ||
346 | p = msg_a + 1; | |
347 | p += ticket_blob_len; | |
348 | end = au->buf->vec.iov_base + au->buf->vec.iov_len; | |
349 | ||
d03857c6 ID |
350 | msg_b = p + ceph_x_encrypt_offset(); |
351 | msg_b->struct_v = 1; | |
ec0994e4 | 352 | get_random_bytes(&au->nonce, sizeof(au->nonce)); |
d03857c6 ID |
353 | msg_b->nonce = cpu_to_le64(au->nonce); |
354 | ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b)); | |
ec0994e4 | 355 | if (ret < 0) |
cbf99a11 | 356 | goto out_au; |
36721ece | 357 | |
ec0994e4 | 358 | p += ret; |
36721ece | 359 | WARN_ON(p > end); |
ec0994e4 SW |
360 | au->buf->vec.iov_len = p - au->buf->vec.iov_base; |
361 | dout(" built authorizer nonce %llx len %d\n", au->nonce, | |
362 | (int)au->buf->vec.iov_len); | |
363 | return 0; | |
364 | ||
cbf99a11 ID |
365 | out_au: |
366 | ceph_x_authorizer_cleanup(au); | |
ec0994e4 SW |
367 | return ret; |
368 | } | |
369 | ||
370 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, | |
371 | void **p, void *end) | |
372 | { | |
373 | ceph_decode_need(p, end, 1 + sizeof(u64), bad); | |
374 | ceph_encode_8(p, 1); | |
375 | ceph_encode_64(p, th->secret_id); | |
376 | if (th->ticket_blob) { | |
377 | const char *buf = th->ticket_blob->vec.iov_base; | |
378 | u32 len = th->ticket_blob->vec.iov_len; | |
379 | ||
380 | ceph_encode_32_safe(p, end, len, bad); | |
381 | ceph_encode_copy_safe(p, end, buf, len, bad); | |
382 | } else { | |
383 | ceph_encode_32_safe(p, end, 0, bad); | |
384 | } | |
385 | ||
386 | return 0; | |
387 | bad: | |
388 | return -ERANGE; | |
389 | } | |
390 | ||
6abe097d ID |
391 | static bool need_key(struct ceph_x_ticket_handler *th) |
392 | { | |
393 | if (!th->have_key) | |
394 | return true; | |
395 | ||
396 | return get_seconds() >= th->renew_after; | |
397 | } | |
398 | ||
399 | static bool have_key(struct ceph_x_ticket_handler *th) | |
400 | { | |
401 | if (th->have_key) { | |
402 | if (get_seconds() >= th->expires) | |
403 | th->have_key = false; | |
404 | } | |
405 | ||
406 | return th->have_key; | |
407 | } | |
408 | ||
ec0994e4 SW |
409 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) |
410 | { | |
411 | int want = ac->want_keys; | |
412 | struct ceph_x_info *xi = ac->private; | |
413 | int service; | |
414 | ||
415 | *pneed = ac->want_keys & ~(xi->have_keys); | |
416 | ||
417 | for (service = 1; service <= want; service <<= 1) { | |
418 | struct ceph_x_ticket_handler *th; | |
419 | ||
420 | if (!(ac->want_keys & service)) | |
421 | continue; | |
422 | ||
423 | if (*pneed & service) | |
424 | continue; | |
425 | ||
426 | th = get_ticket_handler(ac, service); | |
b545787d | 427 | if (IS_ERR(th)) { |
ec0994e4 SW |
428 | *pneed |= service; |
429 | continue; | |
430 | } | |
431 | ||
6abe097d | 432 | if (need_key(th)) |
ec0994e4 | 433 | *pneed |= service; |
6abe097d | 434 | if (!have_key(th)) |
ec0994e4 SW |
435 | xi->have_keys &= ~service; |
436 | } | |
437 | } | |
438 | ||
ec0994e4 SW |
439 | static int ceph_x_build_request(struct ceph_auth_client *ac, |
440 | void *buf, void *end) | |
441 | { | |
442 | struct ceph_x_info *xi = ac->private; | |
443 | int need; | |
444 | struct ceph_x_request_header *head = buf; | |
445 | int ret; | |
446 | struct ceph_x_ticket_handler *th = | |
447 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | |
448 | ||
b545787d DC |
449 | if (IS_ERR(th)) |
450 | return PTR_ERR(th); | |
451 | ||
ec0994e4 SW |
452 | ceph_x_validate_tickets(ac, &need); |
453 | ||
454 | dout("build_request want %x have %x need %x\n", | |
455 | ac->want_keys, xi->have_keys, need); | |
456 | ||
457 | if (need & CEPH_ENTITY_TYPE_AUTH) { | |
458 | struct ceph_x_authenticate *auth = (void *)(head + 1); | |
459 | void *p = auth + 1; | |
d03857c6 ID |
460 | void *enc_buf = xi->auth_authorizer.enc_buf; |
461 | struct ceph_x_challenge_blob *blob = enc_buf + | |
462 | ceph_x_encrypt_offset(); | |
ec0994e4 SW |
463 | u64 *u; |
464 | ||
465 | if (p > end) | |
466 | return -ERANGE; | |
467 | ||
468 | dout(" get_auth_session_key\n"); | |
469 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); | |
470 | ||
471 | /* encrypt and hash */ | |
472 | get_random_bytes(&auth->client_challenge, sizeof(u64)); | |
d03857c6 ID |
473 | blob->client_challenge = auth->client_challenge; |
474 | blob->server_challenge = cpu_to_le64(xi->server_challenge); | |
475 | ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN, | |
476 | sizeof(*blob)); | |
ec0994e4 SW |
477 | if (ret < 0) |
478 | return ret; | |
479 | ||
480 | auth->struct_v = 1; | |
481 | auth->key = 0; | |
d03857c6 | 482 | for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++) |
cd84db6e | 483 | auth->key ^= *(__le64 *)u; |
ec0994e4 SW |
484 | dout(" server_challenge %llx client_challenge %llx key %llx\n", |
485 | xi->server_challenge, le64_to_cpu(auth->client_challenge), | |
486 | le64_to_cpu(auth->key)); | |
487 | ||
488 | /* now encode the old ticket if exists */ | |
489 | ret = ceph_x_encode_ticket(th, &p, end); | |
490 | if (ret < 0) | |
491 | return ret; | |
492 | ||
493 | return p - buf; | |
494 | } | |
495 | ||
496 | if (need) { | |
497 | void *p = head + 1; | |
498 | struct ceph_x_service_ticket_request *req; | |
499 | ||
500 | if (p > end) | |
501 | return -ERANGE; | |
502 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); | |
503 | ||
ec0994e4 SW |
504 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); |
505 | if (ret) | |
506 | return ret; | |
507 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, | |
508 | xi->auth_authorizer.buf->vec.iov_len); | |
509 | ||
510 | req = p; | |
511 | req->keys = cpu_to_le32(need); | |
512 | p += sizeof(*req); | |
513 | return p - buf; | |
514 | } | |
515 | ||
516 | return 0; | |
517 | } | |
518 | ||
519 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, | |
520 | void *buf, void *end) | |
521 | { | |
522 | struct ceph_x_info *xi = ac->private; | |
523 | struct ceph_x_reply_header *head = buf; | |
524 | struct ceph_x_ticket_handler *th; | |
525 | int len = end - buf; | |
526 | int op; | |
527 | int ret; | |
528 | ||
529 | if (result) | |
530 | return result; /* XXX hmm? */ | |
531 | ||
532 | if (xi->starting) { | |
533 | /* it's a hello */ | |
534 | struct ceph_x_server_challenge *sc = buf; | |
535 | ||
536 | if (len != sizeof(*sc)) | |
537 | return -EINVAL; | |
538 | xi->server_challenge = le64_to_cpu(sc->server_challenge); | |
539 | dout("handle_reply got server challenge %llx\n", | |
540 | xi->server_challenge); | |
541 | xi->starting = false; | |
542 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; | |
543 | return -EAGAIN; | |
544 | } | |
545 | ||
0cf5537b | 546 | op = le16_to_cpu(head->op); |
ec0994e4 SW |
547 | result = le32_to_cpu(head->result); |
548 | dout("handle_reply op %d result %d\n", op, result); | |
549 | switch (op) { | |
550 | case CEPHX_GET_AUTH_SESSION_KEY: | |
551 | /* verify auth key */ | |
552 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, | |
553 | buf + sizeof(*head), end); | |
554 | break; | |
555 | ||
556 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: | |
557 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | |
b545787d DC |
558 | if (IS_ERR(th)) |
559 | return PTR_ERR(th); | |
ec0994e4 SW |
560 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, |
561 | buf + sizeof(*head), end); | |
562 | break; | |
563 | ||
564 | default: | |
565 | return -EINVAL; | |
566 | } | |
567 | if (ret) | |
568 | return ret; | |
569 | if (ac->want_keys == xi->have_keys) | |
570 | return 0; | |
571 | return -EAGAIN; | |
572 | } | |
573 | ||
6c1ea260 ID |
574 | static void ceph_x_destroy_authorizer(struct ceph_authorizer *a) |
575 | { | |
576 | struct ceph_x_authorizer *au = (void *)a; | |
577 | ||
578 | ceph_x_authorizer_cleanup(au); | |
579 | kfree(au); | |
580 | } | |
581 | ||
ec0994e4 SW |
582 | static int ceph_x_create_authorizer( |
583 | struct ceph_auth_client *ac, int peer_type, | |
74f1869f | 584 | struct ceph_auth_handshake *auth) |
ec0994e4 SW |
585 | { |
586 | struct ceph_x_authorizer *au; | |
587 | struct ceph_x_ticket_handler *th; | |
588 | int ret; | |
589 | ||
590 | th = get_ticket_handler(ac, peer_type); | |
591 | if (IS_ERR(th)) | |
592 | return PTR_ERR(th); | |
593 | ||
594 | au = kzalloc(sizeof(*au), GFP_NOFS); | |
595 | if (!au) | |
596 | return -ENOMEM; | |
597 | ||
6c1ea260 ID |
598 | au->base.destroy = ceph_x_destroy_authorizer; |
599 | ||
ec0994e4 SW |
600 | ret = ceph_x_build_authorizer(ac, th, au); |
601 | if (ret) { | |
602 | kfree(au); | |
603 | return ret; | |
604 | } | |
605 | ||
74f1869f AE |
606 | auth->authorizer = (struct ceph_authorizer *) au; |
607 | auth->authorizer_buf = au->buf->vec.iov_base; | |
608 | auth->authorizer_buf_len = au->buf->vec.iov_len; | |
7882a26d ID |
609 | auth->authorizer_reply_buf = au->enc_buf; |
610 | auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN; | |
33d07337 YZ |
611 | auth->sign_message = ac->ops->sign_message; |
612 | auth->check_message_signature = ac->ops->check_message_signature; | |
74f1869f | 613 | |
ec0994e4 SW |
614 | return 0; |
615 | } | |
616 | ||
0bed9b5c SW |
617 | static int ceph_x_update_authorizer( |
618 | struct ceph_auth_client *ac, int peer_type, | |
619 | struct ceph_auth_handshake *auth) | |
620 | { | |
621 | struct ceph_x_authorizer *au; | |
622 | struct ceph_x_ticket_handler *th; | |
0bed9b5c SW |
623 | |
624 | th = get_ticket_handler(ac, peer_type); | |
625 | if (IS_ERR(th)) | |
626 | return PTR_ERR(th); | |
627 | ||
628 | au = (struct ceph_x_authorizer *)auth->authorizer; | |
629 | if (au->secret_id < th->secret_id) { | |
630 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", | |
631 | au->service, au->secret_id, th->secret_id); | |
632 | return ceph_x_build_authorizer(ac, th, au); | |
633 | } | |
634 | return 0; | |
635 | } | |
636 | ||
ec0994e4 SW |
637 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, |
638 | struct ceph_authorizer *a, size_t len) | |
639 | { | |
640 | struct ceph_x_authorizer *au = (void *)a; | |
ec0994e4 SW |
641 | int ret = 0; |
642 | struct ceph_x_authorize_reply reply; | |
c27a3e4d | 643 | void *preply = &reply; |
7882a26d | 644 | void *p = au->enc_buf; |
ec0994e4 | 645 | |
7882a26d ID |
646 | ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN, |
647 | &preply, sizeof(reply)); | |
ec0994e4 SW |
648 | if (ret < 0) |
649 | return ret; | |
650 | if (ret != sizeof(reply)) | |
651 | return -EPERM; | |
652 | ||
653 | if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one)) | |
654 | ret = -EPERM; | |
655 | else | |
656 | ret = 0; | |
657 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", | |
658 | au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); | |
659 | return ret; | |
660 | } | |
661 | ||
ec0994e4 SW |
662 | static void ceph_x_reset(struct ceph_auth_client *ac) |
663 | { | |
664 | struct ceph_x_info *xi = ac->private; | |
665 | ||
666 | dout("reset\n"); | |
667 | xi->starting = true; | |
668 | xi->server_challenge = 0; | |
669 | } | |
670 | ||
671 | static void ceph_x_destroy(struct ceph_auth_client *ac) | |
672 | { | |
673 | struct ceph_x_info *xi = ac->private; | |
674 | struct rb_node *p; | |
675 | ||
676 | dout("ceph_x_destroy %p\n", ac); | |
677 | ceph_crypto_key_destroy(&xi->secret); | |
678 | ||
679 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { | |
680 | struct ceph_x_ticket_handler *th = | |
681 | rb_entry(p, struct ceph_x_ticket_handler, node); | |
682 | remove_ticket_handler(ac, th); | |
683 | } | |
684 | ||
cbf99a11 | 685 | ceph_x_authorizer_cleanup(&xi->auth_authorizer); |
22b1de06 | 686 | |
ec0994e4 SW |
687 | kfree(ac->private); |
688 | ac->private = NULL; | |
689 | } | |
690 | ||
187d131d | 691 | static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type) |
ec0994e4 SW |
692 | { |
693 | struct ceph_x_ticket_handler *th; | |
694 | ||
695 | th = get_ticket_handler(ac, peer_type); | |
b545787d | 696 | if (!IS_ERR(th)) |
6abe097d | 697 | th->have_key = false; |
ec0994e4 SW |
698 | } |
699 | ||
187d131d ID |
700 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, |
701 | int peer_type) | |
702 | { | |
703 | /* | |
704 | * We are to invalidate a service ticket in the hopes of | |
705 | * getting a new, hopefully more valid, one. But, we won't get | |
706 | * it unless our AUTH ticket is good, so invalidate AUTH ticket | |
707 | * as well, just in case. | |
708 | */ | |
709 | invalidate_ticket(ac, peer_type); | |
710 | invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH); | |
711 | } | |
712 | ||
4eb4517c ID |
713 | static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, |
714 | __le64 *psig) | |
33d07337 | 715 | { |
d03857c6 | 716 | void *enc_buf = au->enc_buf; |
4eb4517c ID |
717 | struct { |
718 | __le32 len; | |
719 | __le32 header_crc; | |
720 | __le32 front_crc; | |
721 | __le32 middle_crc; | |
722 | __le32 data_crc; | |
d03857c6 | 723 | } __packed *sigblock = enc_buf + ceph_x_encrypt_offset(); |
4eb4517c ID |
724 | int ret; |
725 | ||
d03857c6 ID |
726 | sigblock->len = cpu_to_le32(4*sizeof(u32)); |
727 | sigblock->header_crc = msg->hdr.crc; | |
728 | sigblock->front_crc = msg->footer.front_crc; | |
729 | sigblock->middle_crc = msg->footer.middle_crc; | |
730 | sigblock->data_crc = msg->footer.data_crc; | |
731 | ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN, | |
732 | sizeof(*sigblock)); | |
33d07337 YZ |
733 | if (ret < 0) |
734 | return ret; | |
4eb4517c | 735 | |
d03857c6 | 736 | *psig = *(__le64 *)(enc_buf + sizeof(u32)); |
33d07337 YZ |
737 | return 0; |
738 | } | |
739 | ||
740 | static int ceph_x_sign_message(struct ceph_auth_handshake *auth, | |
741 | struct ceph_msg *msg) | |
742 | { | |
4eb4517c | 743 | __le64 sig; |
33d07337 | 744 | int ret; |
4199b8ee | 745 | |
a51983e4 ID |
746 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) |
747 | return 0; | |
748 | ||
4eb4517c ID |
749 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, |
750 | msg, &sig); | |
751 | if (ret) | |
33d07337 | 752 | return ret; |
4eb4517c ID |
753 | |
754 | msg->footer.sig = sig; | |
33d07337 YZ |
755 | msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED; |
756 | return 0; | |
757 | } | |
758 | ||
759 | static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth, | |
760 | struct ceph_msg *msg) | |
761 | { | |
762 | __le64 sig_check; | |
763 | int ret; | |
764 | ||
a51983e4 ID |
765 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) |
766 | return 0; | |
767 | ||
4eb4517c ID |
768 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, |
769 | msg, &sig_check); | |
770 | if (ret) | |
33d07337 YZ |
771 | return ret; |
772 | if (sig_check == msg->footer.sig) | |
773 | return 0; | |
774 | if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED) | |
775 | dout("ceph_x_check_message_signature %p has signature %llx " | |
776 | "expect %llx\n", msg, msg->footer.sig, sig_check); | |
777 | else | |
778 | dout("ceph_x_check_message_signature %p sender did not set " | |
779 | "CEPH_MSG_FOOTER_SIGNED\n", msg); | |
780 | return -EBADMSG; | |
781 | } | |
ec0994e4 SW |
782 | |
783 | static const struct ceph_auth_client_ops ceph_x_ops = { | |
559c1e00 | 784 | .name = "x", |
ec0994e4 | 785 | .is_authenticated = ceph_x_is_authenticated, |
a41359fa | 786 | .should_authenticate = ceph_x_should_authenticate, |
ec0994e4 SW |
787 | .build_request = ceph_x_build_request, |
788 | .handle_reply = ceph_x_handle_reply, | |
789 | .create_authorizer = ceph_x_create_authorizer, | |
0bed9b5c | 790 | .update_authorizer = ceph_x_update_authorizer, |
ec0994e4 | 791 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, |
ec0994e4 SW |
792 | .invalidate_authorizer = ceph_x_invalidate_authorizer, |
793 | .reset = ceph_x_reset, | |
794 | .destroy = ceph_x_destroy, | |
33d07337 YZ |
795 | .sign_message = ceph_x_sign_message, |
796 | .check_message_signature = ceph_x_check_message_signature, | |
ec0994e4 SW |
797 | }; |
798 | ||
799 | ||
800 | int ceph_x_init(struct ceph_auth_client *ac) | |
801 | { | |
802 | struct ceph_x_info *xi; | |
803 | int ret; | |
804 | ||
805 | dout("ceph_x_init %p\n", ac); | |
b0930f8d | 806 | ret = -ENOMEM; |
ec0994e4 SW |
807 | xi = kzalloc(sizeof(*xi), GFP_NOFS); |
808 | if (!xi) | |
b0930f8d | 809 | goto out; |
ec0994e4 | 810 | |
ec0994e4 | 811 | ret = -EINVAL; |
8323c3aa | 812 | if (!ac->key) { |
ec0994e4 | 813 | pr_err("no secret set (for auth_x protocol)\n"); |
b0930f8d | 814 | goto out_nomem; |
ec0994e4 SW |
815 | } |
816 | ||
8323c3aa TV |
817 | ret = ceph_crypto_key_clone(&xi->secret, ac->key); |
818 | if (ret < 0) { | |
819 | pr_err("cannot clone key: %d\n", ret); | |
b0930f8d | 820 | goto out_nomem; |
8323c3aa | 821 | } |
ec0994e4 SW |
822 | |
823 | xi->starting = true; | |
824 | xi->ticket_handlers = RB_ROOT; | |
825 | ||
826 | ac->protocol = CEPH_AUTH_CEPHX; | |
827 | ac->private = xi; | |
828 | ac->ops = &ceph_x_ops; | |
829 | return 0; | |
830 | ||
b0930f8d | 831 | out_nomem: |
ec0994e4 | 832 | kfree(xi); |
b0930f8d | 833 | out: |
ec0994e4 SW |
834 | return ret; |
835 | } | |
836 | ||
837 |