1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/ceph_debug.h>
6 #include <linux/module.h>
7 #include <linux/random.h>
8 #include <linux/slab.h>
10 #include <linux/ceph/decode.h>
11 #include <linux/ceph/auth.h>
12 #include <linux/ceph/ceph_features.h>
13 #include <linux/ceph/libceph.h>
14 #include <linux/ceph/messenger.h>
18 #include "auth_x_protocol.h"
20 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
);
22 static int ceph_x_is_authenticated(struct ceph_auth_client
*ac
)
24 struct ceph_x_info
*xi
= ac
->private;
27 ceph_x_validate_tickets(ac
, &need
);
28 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
29 ac
->want_keys
, need
, xi
->have_keys
);
30 return (ac
->want_keys
& xi
->have_keys
) == ac
->want_keys
;
33 static int ceph_x_should_authenticate(struct ceph_auth_client
*ac
)
35 struct ceph_x_info
*xi
= ac
->private;
38 ceph_x_validate_tickets(ac
, &need
);
39 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
40 ac
->want_keys
, need
, xi
->have_keys
);
44 static int ceph_x_encrypt_offset(void)
46 return sizeof(u32
) + sizeof(struct ceph_x_encrypt_header
);
49 static int ceph_x_encrypt_buflen(int ilen
)
51 return ceph_x_encrypt_offset() + ilen
+ 16;
54 static int ceph_x_encrypt(struct ceph_crypto_key
*secret
, void *buf
,
55 int buf_len
, int plaintext_len
)
57 struct ceph_x_encrypt_header
*hdr
= buf
+ sizeof(u32
);
62 hdr
->magic
= cpu_to_le64(CEPHX_ENC_MAGIC
);
64 ret
= ceph_crypt(secret
, true, buf
+ sizeof(u32
), buf_len
- sizeof(u32
),
65 plaintext_len
+ sizeof(struct ceph_x_encrypt_header
),
70 ceph_encode_32(&buf
, ciphertext_len
);
71 return sizeof(u32
) + ciphertext_len
;
74 static int __ceph_x_decrypt(struct ceph_crypto_key
*secret
, void *p
,
77 struct ceph_x_encrypt_header
*hdr
= p
;
81 ret
= ceph_crypt(secret
, false, p
, ciphertext_len
, ciphertext_len
,
86 if (le64_to_cpu(hdr
->magic
) != CEPHX_ENC_MAGIC
) {
87 pr_err("%s bad magic\n", __func__
);
91 return plaintext_len
- sizeof(*hdr
);
94 static int ceph_x_decrypt(struct ceph_crypto_key
*secret
, void **p
, void *end
)
99 ceph_decode_32_safe(p
, end
, ciphertext_len
, e_inval
);
100 ceph_decode_need(p
, end
, ciphertext_len
, e_inval
);
102 ret
= __ceph_x_decrypt(secret
, *p
, ciphertext_len
);
106 *p
+= ciphertext_len
;
114 * get existing (or insert new) ticket handler
116 static struct ceph_x_ticket_handler
*
117 get_ticket_handler(struct ceph_auth_client
*ac
, int service
)
119 struct ceph_x_ticket_handler
*th
;
120 struct ceph_x_info
*xi
= ac
->private;
121 struct rb_node
*parent
= NULL
, **p
= &xi
->ticket_handlers
.rb_node
;
125 th
= rb_entry(parent
, struct ceph_x_ticket_handler
, node
);
126 if (service
< th
->service
)
128 else if (service
> th
->service
)
135 th
= kzalloc(sizeof(*th
), GFP_NOFS
);
137 return ERR_PTR(-ENOMEM
);
138 th
->service
= service
;
139 rb_link_node(&th
->node
, parent
, p
);
140 rb_insert_color(&th
->node
, &xi
->ticket_handlers
);
144 static void remove_ticket_handler(struct ceph_auth_client
*ac
,
145 struct ceph_x_ticket_handler
*th
)
147 struct ceph_x_info
*xi
= ac
->private;
149 dout("remove_ticket_handler %p %d\n", th
, th
->service
);
150 rb_erase(&th
->node
, &xi
->ticket_handlers
);
151 ceph_crypto_key_destroy(&th
->session_key
);
153 ceph_buffer_put(th
->ticket_blob
);
157 static int process_one_ticket(struct ceph_auth_client
*ac
,
158 struct ceph_crypto_key
*secret
,
161 struct ceph_x_info
*xi
= ac
->private;
163 u8 tkt_struct_v
, blob_struct_v
;
164 struct ceph_x_ticket_handler
*th
;
168 struct timespec64 validity
;
171 struct ceph_crypto_key new_session_key
= { 0 };
172 struct ceph_buffer
*new_ticket_blob
;
173 time64_t new_expires
, new_renew_after
;
177 ceph_decode_need(p
, end
, sizeof(u32
) + 1, bad
);
179 type
= ceph_decode_32(p
);
180 dout(" ticket type %d %s\n", type
, ceph_entity_type_name(type
));
182 tkt_struct_v
= ceph_decode_8(p
);
183 if (tkt_struct_v
!= 1)
186 th
= get_ticket_handler(ac
, type
);
193 dp
= *p
+ ceph_x_encrypt_offset();
194 ret
= ceph_x_decrypt(secret
, p
, end
);
197 dout(" decrypted %d bytes\n", ret
);
200 tkt_struct_v
= ceph_decode_8(&dp
);
201 if (tkt_struct_v
!= 1)
204 ret
= ceph_crypto_key_decode(&new_session_key
, &dp
, dend
);
208 ceph_decode_timespec64(&validity
, dp
);
209 dp
+= sizeof(struct ceph_timespec
);
210 new_expires
= ktime_get_real_seconds() + validity
.tv_sec
;
211 new_renew_after
= new_expires
- (validity
.tv_sec
/ 4);
212 dout(" expires=%llu renew_after=%llu\n", new_expires
,
215 /* ticket blob for service */
216 ceph_decode_8_safe(p
, end
, is_enc
, bad
);
219 tp
= *p
+ ceph_x_encrypt_offset();
220 ret
= ceph_x_decrypt(&th
->session_key
, p
, end
);
223 dout(" encrypted ticket, decrypted %d bytes\n", ret
);
231 ceph_decode_32_safe(ptp
, tpend
, dlen
, bad
);
232 dout(" ticket blob is %d bytes\n", dlen
);
233 ceph_decode_need(ptp
, tpend
, 1 + sizeof(u64
), bad
);
234 blob_struct_v
= ceph_decode_8(ptp
);
235 if (blob_struct_v
!= 1)
238 new_secret_id
= ceph_decode_64(ptp
);
239 ret
= ceph_decode_buffer(&new_ticket_blob
, ptp
, tpend
);
243 /* all is well, update our ticket */
244 ceph_crypto_key_destroy(&th
->session_key
);
246 ceph_buffer_put(th
->ticket_blob
);
247 th
->session_key
= new_session_key
;
248 th
->ticket_blob
= new_ticket_blob
;
249 th
->secret_id
= new_secret_id
;
250 th
->expires
= new_expires
;
251 th
->renew_after
= new_renew_after
;
253 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
254 type
, ceph_entity_type_name(type
), th
->secret_id
,
255 (int)th
->ticket_blob
->vec
.iov_len
);
256 xi
->have_keys
|= th
->service
;
262 ceph_crypto_key_destroy(&new_session_key
);
266 static int ceph_x_proc_ticket_reply(struct ceph_auth_client
*ac
,
267 struct ceph_crypto_key
*secret
,
268 void *buf
, void *end
)
275 ceph_decode_8_safe(&p
, end
, reply_struct_v
, bad
);
276 if (reply_struct_v
!= 1)
279 ceph_decode_32_safe(&p
, end
, num
, bad
);
280 dout("%d tickets\n", num
);
283 ret
= process_one_ticket(ac
, secret
, &p
, end
);
295 * Encode and encrypt the second part (ceph_x_authorize_b) of the
296 * authorizer. The first part (ceph_x_authorize_a) should already be
299 static int encrypt_authorizer(struct ceph_x_authorizer
*au
,
300 u64
*server_challenge
)
302 struct ceph_x_authorize_a
*msg_a
;
303 struct ceph_x_authorize_b
*msg_b
;
307 msg_a
= au
->buf
->vec
.iov_base
;
308 WARN_ON(msg_a
->ticket_blob
.secret_id
!= cpu_to_le64(au
->secret_id
));
309 p
= (void *)(msg_a
+ 1) + le32_to_cpu(msg_a
->ticket_blob
.blob_len
);
310 end
= au
->buf
->vec
.iov_base
+ au
->buf
->vec
.iov_len
;
312 msg_b
= p
+ ceph_x_encrypt_offset();
314 msg_b
->nonce
= cpu_to_le64(au
->nonce
);
315 if (server_challenge
) {
316 msg_b
->have_challenge
= 1;
317 msg_b
->server_challenge_plus_one
=
318 cpu_to_le64(*server_challenge
+ 1);
320 msg_b
->have_challenge
= 0;
321 msg_b
->server_challenge_plus_one
= 0;
324 ret
= ceph_x_encrypt(&au
->session_key
, p
, end
- p
, sizeof(*msg_b
));
329 if (server_challenge
) {
333 au
->buf
->vec
.iov_len
= p
- au
->buf
->vec
.iov_base
;
339 static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer
*au
)
341 ceph_crypto_key_destroy(&au
->session_key
);
343 ceph_buffer_put(au
->buf
);
348 static int ceph_x_build_authorizer(struct ceph_auth_client
*ac
,
349 struct ceph_x_ticket_handler
*th
,
350 struct ceph_x_authorizer
*au
)
353 struct ceph_x_authorize_a
*msg_a
;
354 struct ceph_x_authorize_b
*msg_b
;
356 int ticket_blob_len
=
357 (th
->ticket_blob
? th
->ticket_blob
->vec
.iov_len
: 0);
359 dout("build_authorizer for %s %p\n",
360 ceph_entity_type_name(th
->service
), au
);
362 ceph_crypto_key_destroy(&au
->session_key
);
363 ret
= ceph_crypto_key_clone(&au
->session_key
, &th
->session_key
);
367 maxlen
= sizeof(*msg_a
) + ticket_blob_len
+
368 ceph_x_encrypt_buflen(sizeof(*msg_b
));
369 dout(" need len %d\n", maxlen
);
370 if (au
->buf
&& au
->buf
->alloc_len
< maxlen
) {
371 ceph_buffer_put(au
->buf
);
375 au
->buf
= ceph_buffer_new(maxlen
, GFP_NOFS
);
381 au
->service
= th
->service
;
382 au
->secret_id
= th
->secret_id
;
384 msg_a
= au
->buf
->vec
.iov_base
;
386 msg_a
->global_id
= cpu_to_le64(ac
->global_id
);
387 msg_a
->service_id
= cpu_to_le32(th
->service
);
388 msg_a
->ticket_blob
.struct_v
= 1;
389 msg_a
->ticket_blob
.secret_id
= cpu_to_le64(th
->secret_id
);
390 msg_a
->ticket_blob
.blob_len
= cpu_to_le32(ticket_blob_len
);
391 if (ticket_blob_len
) {
392 memcpy(msg_a
->ticket_blob
.blob
, th
->ticket_blob
->vec
.iov_base
,
393 th
->ticket_blob
->vec
.iov_len
);
395 dout(" th %p secret_id %lld %lld\n", th
, th
->secret_id
,
396 le64_to_cpu(msg_a
->ticket_blob
.secret_id
));
398 get_random_bytes(&au
->nonce
, sizeof(au
->nonce
));
399 ret
= encrypt_authorizer(au
, NULL
);
401 pr_err("failed to encrypt authorizer: %d", ret
);
405 dout(" built authorizer nonce %llx len %d\n", au
->nonce
,
406 (int)au
->buf
->vec
.iov_len
);
410 ceph_x_authorizer_cleanup(au
);
414 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler
*th
,
417 ceph_decode_need(p
, end
, 1 + sizeof(u64
), bad
);
419 ceph_encode_64(p
, th
->secret_id
);
420 if (th
->ticket_blob
) {
421 const char *buf
= th
->ticket_blob
->vec
.iov_base
;
422 u32 len
= th
->ticket_blob
->vec
.iov_len
;
424 ceph_encode_32_safe(p
, end
, len
, bad
);
425 ceph_encode_copy_safe(p
, end
, buf
, len
, bad
);
427 ceph_encode_32_safe(p
, end
, 0, bad
);
435 static bool need_key(struct ceph_x_ticket_handler
*th
)
440 return ktime_get_real_seconds() >= th
->renew_after
;
443 static bool have_key(struct ceph_x_ticket_handler
*th
)
446 if (ktime_get_real_seconds() >= th
->expires
)
447 th
->have_key
= false;
453 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
)
455 int want
= ac
->want_keys
;
456 struct ceph_x_info
*xi
= ac
->private;
459 *pneed
= ac
->want_keys
& ~(xi
->have_keys
);
461 for (service
= 1; service
<= want
; service
<<= 1) {
462 struct ceph_x_ticket_handler
*th
;
464 if (!(ac
->want_keys
& service
))
467 if (*pneed
& service
)
470 th
= get_ticket_handler(ac
, service
);
479 xi
->have_keys
&= ~service
;
483 static int ceph_x_build_request(struct ceph_auth_client
*ac
,
484 void *buf
, void *end
)
486 struct ceph_x_info
*xi
= ac
->private;
488 struct ceph_x_request_header
*head
= buf
;
490 struct ceph_x_ticket_handler
*th
=
491 get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
496 ceph_x_validate_tickets(ac
, &need
);
498 dout("build_request want %x have %x need %x\n",
499 ac
->want_keys
, xi
->have_keys
, need
);
501 if (need
& CEPH_ENTITY_TYPE_AUTH
) {
502 struct ceph_x_authenticate
*auth
= (void *)(head
+ 1);
504 void *enc_buf
= xi
->auth_authorizer
.enc_buf
;
505 struct ceph_x_challenge_blob
*blob
= enc_buf
+
506 ceph_x_encrypt_offset();
512 dout(" get_auth_session_key\n");
513 head
->op
= cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY
);
515 /* encrypt and hash */
516 get_random_bytes(&auth
->client_challenge
, sizeof(u64
));
517 blob
->client_challenge
= auth
->client_challenge
;
518 blob
->server_challenge
= cpu_to_le64(xi
->server_challenge
);
519 ret
= ceph_x_encrypt(&xi
->secret
, enc_buf
, CEPHX_AU_ENC_BUF_LEN
,
526 for (u
= (u64
*)enc_buf
; u
+ 1 <= (u64
*)(enc_buf
+ ret
); u
++)
527 auth
->key
^= *(__le64
*)u
;
528 dout(" server_challenge %llx client_challenge %llx key %llx\n",
529 xi
->server_challenge
, le64_to_cpu(auth
->client_challenge
),
530 le64_to_cpu(auth
->key
));
532 /* now encode the old ticket if exists */
533 ret
= ceph_x_encode_ticket(th
, &p
, end
);
542 struct ceph_x_service_ticket_request
*req
;
546 head
->op
= cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY
);
548 ret
= ceph_x_build_authorizer(ac
, th
, &xi
->auth_authorizer
);
551 ceph_encode_copy(&p
, xi
->auth_authorizer
.buf
->vec
.iov_base
,
552 xi
->auth_authorizer
.buf
->vec
.iov_len
);
555 req
->keys
= cpu_to_le32(need
);
563 static int ceph_x_handle_reply(struct ceph_auth_client
*ac
, int result
,
564 void *buf
, void *end
)
566 struct ceph_x_info
*xi
= ac
->private;
567 struct ceph_x_reply_header
*head
= buf
;
568 struct ceph_x_ticket_handler
*th
;
574 return result
; /* XXX hmm? */
578 struct ceph_x_server_challenge
*sc
= buf
;
580 if (len
!= sizeof(*sc
))
582 xi
->server_challenge
= le64_to_cpu(sc
->server_challenge
);
583 dout("handle_reply got server challenge %llx\n",
584 xi
->server_challenge
);
585 xi
->starting
= false;
586 xi
->have_keys
&= ~CEPH_ENTITY_TYPE_AUTH
;
590 op
= le16_to_cpu(head
->op
);
591 result
= le32_to_cpu(head
->result
);
592 dout("handle_reply op %d result %d\n", op
, result
);
594 case CEPHX_GET_AUTH_SESSION_KEY
:
595 /* verify auth key */
596 ret
= ceph_x_proc_ticket_reply(ac
, &xi
->secret
,
597 buf
+ sizeof(*head
), end
);
600 case CEPHX_GET_PRINCIPAL_SESSION_KEY
:
601 th
= get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
604 ret
= ceph_x_proc_ticket_reply(ac
, &th
->session_key
,
605 buf
+ sizeof(*head
), end
);
613 if (ac
->want_keys
== xi
->have_keys
)
618 static void ceph_x_destroy_authorizer(struct ceph_authorizer
*a
)
620 struct ceph_x_authorizer
*au
= (void *)a
;
622 ceph_x_authorizer_cleanup(au
);
626 static int ceph_x_create_authorizer(
627 struct ceph_auth_client
*ac
, int peer_type
,
628 struct ceph_auth_handshake
*auth
)
630 struct ceph_x_authorizer
*au
;
631 struct ceph_x_ticket_handler
*th
;
634 th
= get_ticket_handler(ac
, peer_type
);
638 au
= kzalloc(sizeof(*au
), GFP_NOFS
);
642 au
->base
.destroy
= ceph_x_destroy_authorizer
;
644 ret
= ceph_x_build_authorizer(ac
, th
, au
);
650 auth
->authorizer
= (struct ceph_authorizer
*) au
;
651 auth
->authorizer_buf
= au
->buf
->vec
.iov_base
;
652 auth
->authorizer_buf_len
= au
->buf
->vec
.iov_len
;
653 auth
->authorizer_reply_buf
= au
->enc_buf
;
654 auth
->authorizer_reply_buf_len
= CEPHX_AU_ENC_BUF_LEN
;
655 auth
->sign_message
= ac
->ops
->sign_message
;
656 auth
->check_message_signature
= ac
->ops
->check_message_signature
;
661 static int ceph_x_update_authorizer(
662 struct ceph_auth_client
*ac
, int peer_type
,
663 struct ceph_auth_handshake
*auth
)
665 struct ceph_x_authorizer
*au
;
666 struct ceph_x_ticket_handler
*th
;
668 th
= get_ticket_handler(ac
, peer_type
);
672 au
= (struct ceph_x_authorizer
*)auth
->authorizer
;
673 if (au
->secret_id
< th
->secret_id
) {
674 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
675 au
->service
, au
->secret_id
, th
->secret_id
);
676 return ceph_x_build_authorizer(ac
, th
, au
);
681 static int decrypt_authorize_challenge(struct ceph_x_authorizer
*au
,
683 int challenge_buf_len
,
684 u64
*server_challenge
)
686 struct ceph_x_authorize_challenge
*ch
=
687 challenge_buf
+ sizeof(struct ceph_x_encrypt_header
);
691 ret
= __ceph_x_decrypt(&au
->session_key
, challenge_buf
,
695 if (ret
< sizeof(*ch
)) {
696 pr_err("bad size %d for ceph_x_authorize_challenge\n", ret
);
700 *server_challenge
= le64_to_cpu(ch
->server_challenge
);
704 static int ceph_x_add_authorizer_challenge(struct ceph_auth_client
*ac
,
705 struct ceph_authorizer
*a
,
707 int challenge_buf_len
)
709 struct ceph_x_authorizer
*au
= (void *)a
;
710 u64 server_challenge
;
713 ret
= decrypt_authorize_challenge(au
, challenge_buf
, challenge_buf_len
,
716 pr_err("failed to decrypt authorize challenge: %d", ret
);
720 ret
= encrypt_authorizer(au
, &server_challenge
);
722 pr_err("failed to encrypt authorizer w/ challenge: %d", ret
);
729 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client
*ac
,
730 struct ceph_authorizer
*a
)
732 struct ceph_x_authorizer
*au
= (void *)a
;
733 void *p
= au
->enc_buf
;
734 struct ceph_x_authorize_reply
*reply
= p
+ ceph_x_encrypt_offset();
737 ret
= ceph_x_decrypt(&au
->session_key
, &p
, p
+ CEPHX_AU_ENC_BUF_LEN
);
740 if (ret
< sizeof(*reply
)) {
741 pr_err("bad size %d for ceph_x_authorize_reply\n", ret
);
745 if (au
->nonce
+ 1 != le64_to_cpu(reply
->nonce_plus_one
))
749 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
750 au
->nonce
, le64_to_cpu(reply
->nonce_plus_one
), ret
);
754 static void ceph_x_reset(struct ceph_auth_client
*ac
)
756 struct ceph_x_info
*xi
= ac
->private;
760 xi
->server_challenge
= 0;
763 static void ceph_x_destroy(struct ceph_auth_client
*ac
)
765 struct ceph_x_info
*xi
= ac
->private;
768 dout("ceph_x_destroy %p\n", ac
);
769 ceph_crypto_key_destroy(&xi
->secret
);
771 while ((p
= rb_first(&xi
->ticket_handlers
)) != NULL
) {
772 struct ceph_x_ticket_handler
*th
=
773 rb_entry(p
, struct ceph_x_ticket_handler
, node
);
774 remove_ticket_handler(ac
, th
);
777 ceph_x_authorizer_cleanup(&xi
->auth_authorizer
);
783 static void invalidate_ticket(struct ceph_auth_client
*ac
, int peer_type
)
785 struct ceph_x_ticket_handler
*th
;
787 th
= get_ticket_handler(ac
, peer_type
);
789 th
->have_key
= false;
792 static void ceph_x_invalidate_authorizer(struct ceph_auth_client
*ac
,
796 * We are to invalidate a service ticket in the hopes of
797 * getting a new, hopefully more valid, one. But, we won't get
798 * it unless our AUTH ticket is good, so invalidate AUTH ticket
799 * as well, just in case.
801 invalidate_ticket(ac
, peer_type
);
802 invalidate_ticket(ac
, CEPH_ENTITY_TYPE_AUTH
);
805 static int calc_signature(struct ceph_x_authorizer
*au
, struct ceph_msg
*msg
,
808 void *enc_buf
= au
->enc_buf
;
811 if (!CEPH_HAVE_FEATURE(msg
->con
->peer_features
, CEPHX_V2
)) {
818 } __packed
*sigblock
= enc_buf
+ ceph_x_encrypt_offset();
820 sigblock
->len
= cpu_to_le32(4*sizeof(u32
));
821 sigblock
->header_crc
= msg
->hdr
.crc
;
822 sigblock
->front_crc
= msg
->footer
.front_crc
;
823 sigblock
->middle_crc
= msg
->footer
.middle_crc
;
824 sigblock
->data_crc
= msg
->footer
.data_crc
;
826 ret
= ceph_x_encrypt(&au
->session_key
, enc_buf
,
827 CEPHX_AU_ENC_BUF_LEN
, sizeof(*sigblock
));
831 *psig
= *(__le64
*)(enc_buf
+ sizeof(u32
));
841 __le32 seq_lower_word
;
842 } __packed
*sigblock
= enc_buf
;
845 } __packed
*penc
= enc_buf
;
848 sigblock
->header_crc
= msg
->hdr
.crc
;
849 sigblock
->front_crc
= msg
->footer
.front_crc
;
850 sigblock
->front_len
= msg
->hdr
.front_len
;
851 sigblock
->middle_crc
= msg
->footer
.middle_crc
;
852 sigblock
->middle_len
= msg
->hdr
.middle_len
;
853 sigblock
->data_crc
= msg
->footer
.data_crc
;
854 sigblock
->data_len
= msg
->hdr
.data_len
;
855 sigblock
->seq_lower_word
= *(__le32
*)&msg
->hdr
.seq
;
857 /* no leading len, no ceph_x_encrypt_header */
858 ret
= ceph_crypt(&au
->session_key
, true, enc_buf
,
859 CEPHX_AU_ENC_BUF_LEN
, sizeof(*sigblock
),
864 *psig
= penc
->a
^ penc
->b
^ penc
->c
^ penc
->d
;
870 static int ceph_x_sign_message(struct ceph_auth_handshake
*auth
,
871 struct ceph_msg
*msg
)
876 if (ceph_test_opt(from_msgr(msg
->con
->msgr
), NOMSGSIGN
))
879 ret
= calc_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
884 msg
->footer
.sig
= sig
;
885 msg
->footer
.flags
|= CEPH_MSG_FOOTER_SIGNED
;
889 static int ceph_x_check_message_signature(struct ceph_auth_handshake
*auth
,
890 struct ceph_msg
*msg
)
895 if (ceph_test_opt(from_msgr(msg
->con
->msgr
), NOMSGSIGN
))
898 ret
= calc_signature((struct ceph_x_authorizer
*)auth
->authorizer
,
902 if (sig_check
== msg
->footer
.sig
)
904 if (msg
->footer
.flags
& CEPH_MSG_FOOTER_SIGNED
)
905 dout("ceph_x_check_message_signature %p has signature %llx "
906 "expect %llx\n", msg
, msg
->footer
.sig
, sig_check
);
908 dout("ceph_x_check_message_signature %p sender did not set "
909 "CEPH_MSG_FOOTER_SIGNED\n", msg
);
913 static const struct ceph_auth_client_ops ceph_x_ops
= {
915 .is_authenticated
= ceph_x_is_authenticated
,
916 .should_authenticate
= ceph_x_should_authenticate
,
917 .build_request
= ceph_x_build_request
,
918 .handle_reply
= ceph_x_handle_reply
,
919 .create_authorizer
= ceph_x_create_authorizer
,
920 .update_authorizer
= ceph_x_update_authorizer
,
921 .add_authorizer_challenge
= ceph_x_add_authorizer_challenge
,
922 .verify_authorizer_reply
= ceph_x_verify_authorizer_reply
,
923 .invalidate_authorizer
= ceph_x_invalidate_authorizer
,
924 .reset
= ceph_x_reset
,
925 .destroy
= ceph_x_destroy
,
926 .sign_message
= ceph_x_sign_message
,
927 .check_message_signature
= ceph_x_check_message_signature
,
931 int ceph_x_init(struct ceph_auth_client
*ac
)
933 struct ceph_x_info
*xi
;
936 dout("ceph_x_init %p\n", ac
);
938 xi
= kzalloc(sizeof(*xi
), GFP_NOFS
);
944 pr_err("no secret set (for auth_x protocol)\n");
948 ret
= ceph_crypto_key_clone(&xi
->secret
, ac
->key
);
950 pr_err("cannot clone key: %d\n", ret
);
955 xi
->ticket_handlers
= RB_ROOT
;
957 ac
->protocol
= CEPH_AUTH_CEPHX
;
959 ac
->ops
= &ceph_x_ops
;