2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/module.h>
6 #include <linux/random.h>
7 #include <linux/slab.h>
9 #include <linux/ceph/decode.h>
10 #include <linux/ceph/auth.h>
11 #include <linux/ceph/messenger.h>
15 #include "auth_x_protocol.h"
17 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
);
19 static int ceph_x_is_authenticated(struct ceph_auth_client
*ac
)
21 struct ceph_x_info
*xi
= ac
->private;
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
;
30 static int ceph_x_should_authenticate(struct ceph_auth_client
*ac
)
32 struct ceph_x_info
*xi
= ac
->private;
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
);
41 static int ceph_x_encrypt_buflen(int ilen
)
43 return sizeof(struct ceph_x_encrypt_header
) + ilen
+ 16 +
47 static int ceph_x_encrypt(struct ceph_crypto_key
*secret
,
48 void *ibuf
, int ilen
, void *obuf
, size_t olen
)
50 struct ceph_x_encrypt_header head
= {
52 .magic
= cpu_to_le64(CEPHX_ENC_MAGIC
)
54 size_t len
= olen
- sizeof(u32
);
57 ret
= ceph_encrypt2(secret
, obuf
+ sizeof(u32
), &len
,
58 &head
, sizeof(head
), ibuf
, ilen
);
61 ceph_encode_32(&obuf
, len
);
62 return len
+ sizeof(u32
);
65 static int ceph_x_decrypt(struct ceph_crypto_key
*secret
,
66 void **p
, void *end
, void **obuf
, size_t olen
)
68 struct ceph_x_encrypt_header head
;
69 size_t head_len
= sizeof(head
);
72 len
= ceph_decode_32(p
);
76 dout("ceph_x_decrypt len %d\n", len
);
78 *obuf
= kmalloc(len
, GFP_NOFS
);
84 ret
= ceph_decrypt2(secret
, &head
, &head_len
, *obuf
, &olen
, *p
, len
);
87 if (head
.struct_v
!= 1 || le64_to_cpu(head
.magic
) != CEPHX_ENC_MAGIC
)
94 * get existing (or insert new) ticket handler
96 static struct ceph_x_ticket_handler
*
97 get_ticket_handler(struct ceph_auth_client
*ac
, int service
)
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
;
105 th
= rb_entry(parent
, struct ceph_x_ticket_handler
, node
);
106 if (service
< th
->service
)
108 else if (service
> th
->service
)
115 th
= kzalloc(sizeof(*th
), GFP_NOFS
);
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
);
124 static void remove_ticket_handler(struct ceph_auth_client
*ac
,
125 struct ceph_x_ticket_handler
*th
)
127 struct ceph_x_info
*xi
= ac
->private;
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
);
133 ceph_buffer_put(th
->ticket_blob
);
137 static int process_one_ticket(struct ceph_auth_client
*ac
,
138 struct ceph_crypto_key
*secret
,
141 struct ceph_x_info
*xi
= ac
->private;
143 u8 tkt_struct_v
, blob_struct_v
;
144 struct ceph_x_ticket_handler
*th
;
149 struct timespec validity
;
150 struct ceph_crypto_key old_key
;
151 void *ticket_buf
= NULL
;
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
;
161 ceph_decode_need(p
, end
, sizeof(u32
) + 1, bad
);
163 type
= ceph_decode_32(p
);
164 dout(" ticket type %d %s\n", type
, ceph_entity_type_name(type
));
166 tkt_struct_v
= ceph_decode_8(p
);
167 if (tkt_struct_v
!= 1)
170 th
= get_ticket_handler(ac
, type
);
177 dlen
= ceph_x_decrypt(secret
, p
, end
, &dbuf
, 0);
182 dout(" decrypted %d bytes\n", dlen
);
186 tkt_struct_v
= ceph_decode_8(&dp
);
187 if (tkt_struct_v
!= 1)
190 memcpy(&old_key
, &th
->session_key
, sizeof(old_key
));
191 ret
= ceph_crypto_key_decode(&new_session_key
, &dp
, dend
);
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
,
202 /* ticket blob for service */
203 ceph_decode_8_safe(p
, end
, is_enc
, bad
);
206 dout(" encrypted ticket\n");
207 dlen
= ceph_x_decrypt(&old_key
, p
, end
, &ticket_buf
, 0);
220 ceph_decode_32_safe(ptp
, tpend
, dlen
, bad
);
221 dout(" ticket blob is %d bytes\n", dlen
);
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
);
229 /* all is well, update our ticket */
230 ceph_crypto_key_destroy(&th
->session_key
);
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
;
254 static int ceph_x_proc_ticket_reply(struct ceph_auth_client
*ac
,
255 struct ceph_crypto_key
*secret
,
256 void *buf
, void *end
)
263 ceph_decode_8_safe(&p
, end
, reply_struct_v
, bad
);
264 if (reply_struct_v
!= 1)
267 ceph_decode_32_safe(&p
, end
, num
, bad
);
268 dout("%d tickets\n", num
);
271 ret
= process_one_ticket(ac
, secret
, &p
, end
);
282 static int ceph_x_build_authorizer(struct ceph_auth_client
*ac
,
283 struct ceph_x_ticket_handler
*th
,
284 struct ceph_x_authorizer
*au
)
287 struct ceph_x_authorize_a
*msg_a
;
288 struct ceph_x_authorize_b msg_b
;
291 int ticket_blob_len
=
292 (th
->ticket_blob
? th
->ticket_blob
->vec
.iov_len
: 0);
294 dout("build_authorizer for %s %p\n",
295 ceph_entity_type_name(th
->service
), au
);
297 ceph_crypto_key_destroy(&au
->session_key
);
298 ret
= ceph_crypto_key_clone(&au
->session_key
, &th
->session_key
);
302 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
) {
306 ceph_buffer_put(au
->buf
);
310 au
->buf
= ceph_buffer_new(maxlen
, GFP_NOFS
);
312 ceph_crypto_key_destroy(&au
->session_key
);
316 au
->service
= th
->service
;
317 au
->secret_id
= th
->secret_id
;
319 msg_a
= au
->buf
->vec
.iov_base
;
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
);
330 dout(" th %p secret_id %lld %lld\n", th
, th
->secret_id
,
331 le64_to_cpu(msg_a
->ticket_blob
.secret_id
));
334 p
+= ticket_blob_len
;
335 end
= au
->buf
->vec
.iov_base
+ au
->buf
->vec
.iov_len
;
337 get_random_bytes(&au
->nonce
, sizeof(au
->nonce
));
339 msg_b
.nonce
= cpu_to_le64(au
->nonce
);
340 ret
= ceph_x_encrypt(&au
->session_key
, &msg_b
, sizeof(msg_b
),
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
);
348 BUG_ON(au
->buf
->vec
.iov_len
> maxlen
);
352 ceph_buffer_put(au
->buf
);
357 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler
*th
,
360 ceph_decode_need(p
, end
, 1 + sizeof(u64
), bad
);
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
;
367 ceph_encode_32_safe(p
, end
, len
, bad
);
368 ceph_encode_copy_safe(p
, end
, buf
, len
, bad
);
370 ceph_encode_32_safe(p
, end
, 0, bad
);
378 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
)
380 int want
= ac
->want_keys
;
381 struct ceph_x_info
*xi
= ac
->private;
384 *pneed
= ac
->want_keys
& ~(xi
->have_keys
);
386 for (service
= 1; service
<= want
; service
<<= 1) {
387 struct ceph_x_ticket_handler
*th
;
389 if (!(ac
->want_keys
& service
))
392 if (*pneed
& service
)
395 th
= get_ticket_handler(ac
, service
);
402 if (get_seconds() >= th
->renew_after
)
404 if (get_seconds() >= th
->expires
)
405 xi
->have_keys
&= ~service
;
410 static int ceph_x_build_request(struct ceph_auth_client
*ac
,
411 void *buf
, void *end
)
413 struct ceph_x_info
*xi
= ac
->private;
415 struct ceph_x_request_header
*head
= buf
;
417 struct ceph_x_ticket_handler
*th
=
418 get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
423 ceph_x_validate_tickets(ac
, &need
);
425 dout("build_request want %x have %x need %x\n",
426 ac
->want_keys
, xi
->have_keys
, need
);
428 if (need
& CEPH_ENTITY_TYPE_AUTH
) {
429 struct ceph_x_authenticate
*auth
= (void *)(head
+ 1);
431 struct ceph_x_challenge_blob tmp
;
438 dout(" get_auth_session_key\n");
439 head
->op
= cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY
);
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
));
452 for (u
= (u64
*)tmp_enc
; u
+ 1 <= (u64
*)(tmp_enc
+ ret
); u
++)
453 auth
->key
^= *(__le64
*)u
;
454 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
));
458 /* now encode the old ticket if exists */
459 ret
= ceph_x_encode_ticket(th
, &p
, end
);
468 struct ceph_x_service_ticket_request
*req
;
472 head
->op
= cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY
);
474 ret
= ceph_x_build_authorizer(ac
, th
, &xi
->auth_authorizer
);
477 ceph_encode_copy(&p
, xi
->auth_authorizer
.buf
->vec
.iov_base
,
478 xi
->auth_authorizer
.buf
->vec
.iov_len
);
481 req
->keys
= cpu_to_le32(need
);
489 static int ceph_x_handle_reply(struct ceph_auth_client
*ac
, int result
,
490 void *buf
, void *end
)
492 struct ceph_x_info
*xi
= ac
->private;
493 struct ceph_x_reply_header
*head
= buf
;
494 struct ceph_x_ticket_handler
*th
;
500 return result
; /* XXX hmm? */
504 struct ceph_x_server_challenge
*sc
= buf
;
506 if (len
!= sizeof(*sc
))
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
;
516 op
= le16_to_cpu(head
->op
);
517 result
= le32_to_cpu(head
->result
);
518 dout("handle_reply op %d result %d\n", op
, result
);
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
);
526 case CEPHX_GET_PRINCIPAL_SESSION_KEY
:
527 th
= get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
530 ret
= ceph_x_proc_ticket_reply(ac
, &th
->session_key
,
531 buf
+ sizeof(*head
), end
);
539 if (ac
->want_keys
== xi
->have_keys
)
544 static int ceph_x_create_authorizer(
545 struct ceph_auth_client
*ac
, int peer_type
,
546 struct ceph_auth_handshake
*auth
)
548 struct ceph_x_authorizer
*au
;
549 struct ceph_x_ticket_handler
*th
;
552 th
= get_ticket_handler(ac
, peer_type
);
556 au
= kzalloc(sizeof(*au
), GFP_NOFS
);
560 ret
= ceph_x_build_authorizer(ac
, th
, au
);
566 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
);
571 auth
->sign_message
= ac
->ops
->sign_message
;
572 auth
->check_message_signature
= ac
->ops
->check_message_signature
;
577 static int ceph_x_update_authorizer(
578 struct ceph_auth_client
*ac
, int peer_type
,
579 struct ceph_auth_handshake
*auth
)
581 struct ceph_x_authorizer
*au
;
582 struct ceph_x_ticket_handler
*th
;
584 th
= get_ticket_handler(ac
, peer_type
);
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
);
597 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client
*ac
,
598 struct ceph_authorizer
*a
, size_t len
)
600 struct ceph_x_authorizer
*au
= (void *)a
;
602 struct ceph_x_authorize_reply reply
;
603 void *preply
= &reply
;
604 void *p
= au
->reply_buf
;
605 void *end
= p
+ sizeof(au
->reply_buf
);
607 ret
= ceph_x_decrypt(&au
->session_key
, &p
, end
, &preply
, sizeof(reply
));
610 if (ret
!= sizeof(reply
))
613 if (au
->nonce
+ 1 != le64_to_cpu(reply
.nonce_plus_one
))
617 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
618 au
->nonce
, le64_to_cpu(reply
.nonce_plus_one
), ret
);
622 static void ceph_x_destroy_authorizer(struct ceph_auth_client
*ac
,
623 struct ceph_authorizer
*a
)
625 struct ceph_x_authorizer
*au
= (void *)a
;
627 ceph_crypto_key_destroy(&au
->session_key
);
628 ceph_buffer_put(au
->buf
);
633 static void ceph_x_reset(struct ceph_auth_client
*ac
)
635 struct ceph_x_info
*xi
= ac
->private;
639 xi
->server_challenge
= 0;
642 static void ceph_x_destroy(struct ceph_auth_client
*ac
)
644 struct ceph_x_info
*xi
= ac
->private;
647 dout("ceph_x_destroy %p\n", ac
);
648 ceph_crypto_key_destroy(&xi
->secret
);
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
);
656 if (xi
->auth_authorizer
.buf
)
657 ceph_buffer_put(xi
->auth_authorizer
.buf
);
663 static void ceph_x_invalidate_authorizer(struct ceph_auth_client
*ac
,
666 struct ceph_x_ticket_handler
*th
;
668 th
= get_ticket_handler(ac
, peer_type
);
670 memset(&th
->validity
, 0, sizeof(th
->validity
));
673 static int calcu_signature(struct ceph_x_authorizer
*au
,
674 struct ceph_msg
*msg
, __le64
*sig
)
679 cpu_to_le32(16), msg
->hdr
.crc
, msg
->footer
.front_crc
,
680 msg
->footer
.middle_crc
, msg
->footer
.data_crc
,
682 ret
= ceph_x_encrypt(&au
->session_key
, &tmp
, sizeof(tmp
),
683 tmp_enc
, sizeof(tmp_enc
));
686 *sig
= *(__le64
*)(tmp_enc
+ 4);
690 static int ceph_x_sign_message(struct ceph_auth_handshake
*auth
,
691 struct ceph_msg
*msg
)
694 if (!auth
->authorizer
)
696 ret
= calcu_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
697 msg
, &msg
->footer
.sig
);
700 msg
->footer
.flags
|= CEPH_MSG_FOOTER_SIGNED
;
704 static int ceph_x_check_message_signature(struct ceph_auth_handshake
*auth
,
705 struct ceph_msg
*msg
)
710 if (!auth
->authorizer
)
712 ret
= calcu_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
716 if (sig_check
== msg
->footer
.sig
)
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
);
722 dout("ceph_x_check_message_signature %p sender did not set "
723 "CEPH_MSG_FOOTER_SIGNED\n", msg
);
727 static const struct ceph_auth_client_ops ceph_x_ops
= {
729 .is_authenticated
= ceph_x_is_authenticated
,
730 .should_authenticate
= ceph_x_should_authenticate
,
731 .build_request
= ceph_x_build_request
,
732 .handle_reply
= ceph_x_handle_reply
,
733 .create_authorizer
= ceph_x_create_authorizer
,
734 .update_authorizer
= ceph_x_update_authorizer
,
735 .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
,
740 .sign_message
= ceph_x_sign_message
,
741 .check_message_signature
= ceph_x_check_message_signature
,
745 int ceph_x_init(struct ceph_auth_client
*ac
)
747 struct ceph_x_info
*xi
;
750 dout("ceph_x_init %p\n", ac
);
752 xi
= kzalloc(sizeof(*xi
), GFP_NOFS
);
758 pr_err("no secret set (for auth_x protocol)\n");
762 ret
= ceph_crypto_key_clone(&xi
->secret
, ac
->key
);
764 pr_err("cannot clone key: %d\n", ret
);
769 xi
->ticket_handlers
= RB_ROOT
;
771 ac
->protocol
= CEPH_AUTH_CEPHX
;
773 ac
->ops
= &ceph_x_ops
;