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/libceph.h>
12 #include <linux/ceph/messenger.h>
16 #include "auth_x_protocol.h"
18 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
);
20 static int ceph_x_is_authenticated(struct ceph_auth_client
*ac
)
22 struct ceph_x_info
*xi
= ac
->private;
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
;
31 static int ceph_x_should_authenticate(struct ceph_auth_client
*ac
)
33 struct ceph_x_info
*xi
= ac
->private;
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
);
42 static int ceph_x_encrypt_buflen(int ilen
)
44 return sizeof(struct ceph_x_encrypt_header
) + ilen
+ 16 +
48 static int ceph_x_encrypt(struct ceph_crypto_key
*secret
,
49 void *ibuf
, int ilen
, void *obuf
, size_t olen
)
51 struct ceph_x_encrypt_header head
= {
53 .magic
= cpu_to_le64(CEPHX_ENC_MAGIC
)
55 size_t len
= olen
- sizeof(u32
);
58 ret
= ceph_encrypt2(secret
, obuf
+ sizeof(u32
), &len
,
59 &head
, sizeof(head
), ibuf
, ilen
);
62 ceph_encode_32(&obuf
, len
);
63 return len
+ sizeof(u32
);
66 static int ceph_x_decrypt(struct ceph_crypto_key
*secret
,
67 void **p
, void *end
, void **obuf
, size_t olen
)
69 struct ceph_x_encrypt_header head
;
70 size_t head_len
= sizeof(head
);
73 len
= ceph_decode_32(p
);
77 dout("ceph_x_decrypt len %d\n", len
);
79 *obuf
= kmalloc(len
, GFP_NOFS
);
85 ret
= ceph_decrypt2(secret
, &head
, &head_len
, *obuf
, &olen
, *p
, len
);
88 if (head
.struct_v
!= 1 || le64_to_cpu(head
.magic
) != CEPHX_ENC_MAGIC
)
95 * get existing (or insert new) ticket handler
97 static struct ceph_x_ticket_handler
*
98 get_ticket_handler(struct ceph_auth_client
*ac
, int service
)
100 struct ceph_x_ticket_handler
*th
;
101 struct ceph_x_info
*xi
= ac
->private;
102 struct rb_node
*parent
= NULL
, **p
= &xi
->ticket_handlers
.rb_node
;
106 th
= rb_entry(parent
, struct ceph_x_ticket_handler
, node
);
107 if (service
< th
->service
)
109 else if (service
> th
->service
)
116 th
= kzalloc(sizeof(*th
), GFP_NOFS
);
118 return ERR_PTR(-ENOMEM
);
119 th
->service
= service
;
120 rb_link_node(&th
->node
, parent
, p
);
121 rb_insert_color(&th
->node
, &xi
->ticket_handlers
);
125 static void remove_ticket_handler(struct ceph_auth_client
*ac
,
126 struct ceph_x_ticket_handler
*th
)
128 struct ceph_x_info
*xi
= ac
->private;
130 dout("remove_ticket_handler %p %d\n", th
, th
->service
);
131 rb_erase(&th
->node
, &xi
->ticket_handlers
);
132 ceph_crypto_key_destroy(&th
->session_key
);
134 ceph_buffer_put(th
->ticket_blob
);
138 static int process_one_ticket(struct ceph_auth_client
*ac
,
139 struct ceph_crypto_key
*secret
,
142 struct ceph_x_info
*xi
= ac
->private;
144 u8 tkt_struct_v
, blob_struct_v
;
145 struct ceph_x_ticket_handler
*th
;
150 struct timespec validity
;
151 struct ceph_crypto_key old_key
;
152 void *ticket_buf
= NULL
;
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_timespec(&validity
, dp
);
196 dp
+= sizeof(struct ceph_timespec
);
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
->secret_id
= new_secret_id
;
236 th
->expires
= new_expires
;
237 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 void ceph_x_authorizer_cleanup(struct ceph_x_authorizer
*au
)
284 ceph_crypto_key_destroy(&au
->session_key
);
286 ceph_buffer_put(au
->buf
);
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
)
296 struct ceph_x_authorize_a
*msg_a
;
297 struct ceph_x_authorize_b msg_b
;
300 int ticket_blob_len
=
301 (th
->ticket_blob
? th
->ticket_blob
->vec
.iov_len
: 0);
303 dout("build_authorizer for %s %p\n",
304 ceph_entity_type_name(th
->service
), au
);
306 ceph_crypto_key_destroy(&au
->session_key
);
307 ret
= ceph_crypto_key_clone(&au
->session_key
, &th
->session_key
);
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
) {
315 ceph_buffer_put(au
->buf
);
319 au
->buf
= ceph_buffer_new(maxlen
, GFP_NOFS
);
325 au
->service
= th
->service
;
326 au
->secret_id
= th
->secret_id
;
328 msg_a
= au
->buf
->vec
.iov_base
;
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
);
339 dout(" th %p secret_id %lld %lld\n", th
, th
->secret_id
,
340 le64_to_cpu(msg_a
->ticket_blob
.secret_id
));
343 p
+= ticket_blob_len
;
344 end
= au
->buf
->vec
.iov_base
+ au
->buf
->vec
.iov_len
;
346 get_random_bytes(&au
->nonce
, sizeof(au
->nonce
));
348 msg_b
.nonce
= cpu_to_le64(au
->nonce
);
349 ret
= ceph_x_encrypt(&au
->session_key
, &msg_b
, sizeof(msg_b
),
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
);
357 BUG_ON(au
->buf
->vec
.iov_len
> maxlen
);
361 ceph_x_authorizer_cleanup(au
);
365 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler
*th
,
368 ceph_decode_need(p
, end
, 1 + sizeof(u64
), bad
);
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
;
375 ceph_encode_32_safe(p
, end
, len
, bad
);
376 ceph_encode_copy_safe(p
, end
, buf
, len
, bad
);
378 ceph_encode_32_safe(p
, end
, 0, bad
);
386 static bool need_key(struct ceph_x_ticket_handler
*th
)
391 return get_seconds() >= th
->renew_after
;
394 static bool have_key(struct ceph_x_ticket_handler
*th
)
397 if (get_seconds() >= th
->expires
)
398 th
->have_key
= false;
404 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
)
406 int want
= ac
->want_keys
;
407 struct ceph_x_info
*xi
= ac
->private;
410 *pneed
= ac
->want_keys
& ~(xi
->have_keys
);
412 for (service
= 1; service
<= want
; service
<<= 1) {
413 struct ceph_x_ticket_handler
*th
;
415 if (!(ac
->want_keys
& service
))
418 if (*pneed
& service
)
421 th
= get_ticket_handler(ac
, service
);
430 xi
->have_keys
&= ~service
;
434 static int ceph_x_build_request(struct ceph_auth_client
*ac
,
435 void *buf
, void *end
)
437 struct ceph_x_info
*xi
= ac
->private;
439 struct ceph_x_request_header
*head
= buf
;
441 struct ceph_x_ticket_handler
*th
=
442 get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
447 ceph_x_validate_tickets(ac
, &need
);
449 dout("build_request want %x have %x need %x\n",
450 ac
->want_keys
, xi
->have_keys
, need
);
452 if (need
& CEPH_ENTITY_TYPE_AUTH
) {
453 struct ceph_x_authenticate
*auth
= (void *)(head
+ 1);
455 struct ceph_x_challenge_blob tmp
;
462 dout(" get_auth_session_key\n");
463 head
->op
= cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY
);
465 /* encrypt and hash */
466 get_random_bytes(&auth
->client_challenge
, sizeof(u64
));
467 tmp
.client_challenge
= auth
->client_challenge
;
468 tmp
.server_challenge
= cpu_to_le64(xi
->server_challenge
);
469 ret
= ceph_x_encrypt(&xi
->secret
, &tmp
, sizeof(tmp
),
470 tmp_enc
, sizeof(tmp_enc
));
476 for (u
= (u64
*)tmp_enc
; u
+ 1 <= (u64
*)(tmp_enc
+ ret
); u
++)
477 auth
->key
^= *(__le64
*)u
;
478 dout(" server_challenge %llx client_challenge %llx key %llx\n",
479 xi
->server_challenge
, le64_to_cpu(auth
->client_challenge
),
480 le64_to_cpu(auth
->key
));
482 /* now encode the old ticket if exists */
483 ret
= ceph_x_encode_ticket(th
, &p
, end
);
492 struct ceph_x_service_ticket_request
*req
;
496 head
->op
= cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY
);
498 ret
= ceph_x_build_authorizer(ac
, th
, &xi
->auth_authorizer
);
501 ceph_encode_copy(&p
, xi
->auth_authorizer
.buf
->vec
.iov_base
,
502 xi
->auth_authorizer
.buf
->vec
.iov_len
);
505 req
->keys
= cpu_to_le32(need
);
513 static int ceph_x_handle_reply(struct ceph_auth_client
*ac
, int result
,
514 void *buf
, void *end
)
516 struct ceph_x_info
*xi
= ac
->private;
517 struct ceph_x_reply_header
*head
= buf
;
518 struct ceph_x_ticket_handler
*th
;
524 return result
; /* XXX hmm? */
528 struct ceph_x_server_challenge
*sc
= buf
;
530 if (len
!= sizeof(*sc
))
532 xi
->server_challenge
= le64_to_cpu(sc
->server_challenge
);
533 dout("handle_reply got server challenge %llx\n",
534 xi
->server_challenge
);
535 xi
->starting
= false;
536 xi
->have_keys
&= ~CEPH_ENTITY_TYPE_AUTH
;
540 op
= le16_to_cpu(head
->op
);
541 result
= le32_to_cpu(head
->result
);
542 dout("handle_reply op %d result %d\n", op
, result
);
544 case CEPHX_GET_AUTH_SESSION_KEY
:
545 /* verify auth key */
546 ret
= ceph_x_proc_ticket_reply(ac
, &xi
->secret
,
547 buf
+ sizeof(*head
), end
);
550 case CEPHX_GET_PRINCIPAL_SESSION_KEY
:
551 th
= get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
554 ret
= ceph_x_proc_ticket_reply(ac
, &th
->session_key
,
555 buf
+ sizeof(*head
), end
);
563 if (ac
->want_keys
== xi
->have_keys
)
568 static int ceph_x_create_authorizer(
569 struct ceph_auth_client
*ac
, int peer_type
,
570 struct ceph_auth_handshake
*auth
)
572 struct ceph_x_authorizer
*au
;
573 struct ceph_x_ticket_handler
*th
;
576 th
= get_ticket_handler(ac
, peer_type
);
580 au
= kzalloc(sizeof(*au
), GFP_NOFS
);
584 ret
= ceph_x_build_authorizer(ac
, th
, au
);
590 auth
->authorizer
= (struct ceph_authorizer
*) au
;
591 auth
->authorizer_buf
= au
->buf
->vec
.iov_base
;
592 auth
->authorizer_buf_len
= au
->buf
->vec
.iov_len
;
593 auth
->authorizer_reply_buf
= au
->reply_buf
;
594 auth
->authorizer_reply_buf_len
= sizeof (au
->reply_buf
);
595 auth
->sign_message
= ac
->ops
->sign_message
;
596 auth
->check_message_signature
= ac
->ops
->check_message_signature
;
601 static int ceph_x_update_authorizer(
602 struct ceph_auth_client
*ac
, int peer_type
,
603 struct ceph_auth_handshake
*auth
)
605 struct ceph_x_authorizer
*au
;
606 struct ceph_x_ticket_handler
*th
;
608 th
= get_ticket_handler(ac
, peer_type
);
612 au
= (struct ceph_x_authorizer
*)auth
->authorizer
;
613 if (au
->secret_id
< th
->secret_id
) {
614 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
615 au
->service
, au
->secret_id
, th
->secret_id
);
616 return ceph_x_build_authorizer(ac
, th
, au
);
621 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client
*ac
,
622 struct ceph_authorizer
*a
, size_t len
)
624 struct ceph_x_authorizer
*au
= (void *)a
;
626 struct ceph_x_authorize_reply reply
;
627 void *preply
= &reply
;
628 void *p
= au
->reply_buf
;
629 void *end
= p
+ sizeof(au
->reply_buf
);
631 ret
= ceph_x_decrypt(&au
->session_key
, &p
, end
, &preply
, sizeof(reply
));
634 if (ret
!= sizeof(reply
))
637 if (au
->nonce
+ 1 != le64_to_cpu(reply
.nonce_plus_one
))
641 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
642 au
->nonce
, le64_to_cpu(reply
.nonce_plus_one
), ret
);
646 static void ceph_x_destroy_authorizer(struct ceph_auth_client
*ac
,
647 struct ceph_authorizer
*a
)
649 struct ceph_x_authorizer
*au
= (void *)a
;
651 ceph_x_authorizer_cleanup(au
);
656 static void ceph_x_reset(struct ceph_auth_client
*ac
)
658 struct ceph_x_info
*xi
= ac
->private;
662 xi
->server_challenge
= 0;
665 static void ceph_x_destroy(struct ceph_auth_client
*ac
)
667 struct ceph_x_info
*xi
= ac
->private;
670 dout("ceph_x_destroy %p\n", ac
);
671 ceph_crypto_key_destroy(&xi
->secret
);
673 while ((p
= rb_first(&xi
->ticket_handlers
)) != NULL
) {
674 struct ceph_x_ticket_handler
*th
=
675 rb_entry(p
, struct ceph_x_ticket_handler
, node
);
676 remove_ticket_handler(ac
, th
);
679 ceph_x_authorizer_cleanup(&xi
->auth_authorizer
);
685 static void invalidate_ticket(struct ceph_auth_client
*ac
, int peer_type
)
687 struct ceph_x_ticket_handler
*th
;
689 th
= get_ticket_handler(ac
, peer_type
);
691 th
->have_key
= false;
694 static void ceph_x_invalidate_authorizer(struct ceph_auth_client
*ac
,
698 * We are to invalidate a service ticket in the hopes of
699 * getting a new, hopefully more valid, one. But, we won't get
700 * it unless our AUTH ticket is good, so invalidate AUTH ticket
701 * as well, just in case.
703 invalidate_ticket(ac
, peer_type
);
704 invalidate_ticket(ac
, CEPH_ENTITY_TYPE_AUTH
);
707 static int calcu_signature(struct ceph_x_authorizer
*au
,
708 struct ceph_msg
*msg
, __le64
*sig
)
713 cpu_to_le32(16), msg
->hdr
.crc
, msg
->footer
.front_crc
,
714 msg
->footer
.middle_crc
, msg
->footer
.data_crc
,
716 ret
= ceph_x_encrypt(&au
->session_key
, &tmp
, sizeof(tmp
),
717 tmp_enc
, sizeof(tmp_enc
));
720 *sig
= *(__le64
*)(tmp_enc
+ 4);
724 static int ceph_x_sign_message(struct ceph_auth_handshake
*auth
,
725 struct ceph_msg
*msg
)
729 if (ceph_test_opt(from_msgr(msg
->con
->msgr
), NOMSGSIGN
))
732 ret
= calcu_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
733 msg
, &msg
->footer
.sig
);
736 msg
->footer
.flags
|= CEPH_MSG_FOOTER_SIGNED
;
740 static int ceph_x_check_message_signature(struct ceph_auth_handshake
*auth
,
741 struct ceph_msg
*msg
)
746 if (ceph_test_opt(from_msgr(msg
->con
->msgr
), NOMSGSIGN
))
749 ret
= calcu_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
753 if (sig_check
== msg
->footer
.sig
)
755 if (msg
->footer
.flags
& CEPH_MSG_FOOTER_SIGNED
)
756 dout("ceph_x_check_message_signature %p has signature %llx "
757 "expect %llx\n", msg
, msg
->footer
.sig
, sig_check
);
759 dout("ceph_x_check_message_signature %p sender did not set "
760 "CEPH_MSG_FOOTER_SIGNED\n", msg
);
764 static const struct ceph_auth_client_ops ceph_x_ops
= {
766 .is_authenticated
= ceph_x_is_authenticated
,
767 .should_authenticate
= ceph_x_should_authenticate
,
768 .build_request
= ceph_x_build_request
,
769 .handle_reply
= ceph_x_handle_reply
,
770 .create_authorizer
= ceph_x_create_authorizer
,
771 .update_authorizer
= ceph_x_update_authorizer
,
772 .verify_authorizer_reply
= ceph_x_verify_authorizer_reply
,
773 .destroy_authorizer
= ceph_x_destroy_authorizer
,
774 .invalidate_authorizer
= ceph_x_invalidate_authorizer
,
775 .reset
= ceph_x_reset
,
776 .destroy
= ceph_x_destroy
,
777 .sign_message
= ceph_x_sign_message
,
778 .check_message_signature
= ceph_x_check_message_signature
,
782 int ceph_x_init(struct ceph_auth_client
*ac
)
784 struct ceph_x_info
*xi
;
787 dout("ceph_x_init %p\n", ac
);
789 xi
= kzalloc(sizeof(*xi
), GFP_NOFS
);
795 pr_err("no secret set (for auth_x protocol)\n");
799 ret
= ceph_crypto_key_clone(&xi
->secret
, ac
->key
);
801 pr_err("cannot clone key: %d\n", ret
);
806 xi
->ticket_handlers
= RB_ROOT
;
808 ac
->protocol
= CEPH_AUTH_CEPHX
;
810 ac
->ops
= &ceph_x_ops
;