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>
14 #include "auth_x_protocol.h"
16 #define TEMP_TICKET_BUF_LEN 256
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
);
78 ret
= ceph_decrypt2(secret
, &head
, &head_len
, obuf
, &olen
,
82 if (head
.struct_v
!= 1 || le64_to_cpu(head
.magic
) != CEPHX_ENC_MAGIC
)
89 * get existing (or insert new) ticket handler
91 static struct ceph_x_ticket_handler
*
92 get_ticket_handler(struct ceph_auth_client
*ac
, int service
)
94 struct ceph_x_ticket_handler
*th
;
95 struct ceph_x_info
*xi
= ac
->private;
96 struct rb_node
*parent
= NULL
, **p
= &xi
->ticket_handlers
.rb_node
;
100 th
= rb_entry(parent
, struct ceph_x_ticket_handler
, node
);
101 if (service
< th
->service
)
103 else if (service
> th
->service
)
110 th
= kzalloc(sizeof(*th
), GFP_NOFS
);
112 return ERR_PTR(-ENOMEM
);
113 th
->service
= service
;
114 rb_link_node(&th
->node
, parent
, p
);
115 rb_insert_color(&th
->node
, &xi
->ticket_handlers
);
119 static void remove_ticket_handler(struct ceph_auth_client
*ac
,
120 struct ceph_x_ticket_handler
*th
)
122 struct ceph_x_info
*xi
= ac
->private;
124 dout("remove_ticket_handler %p %d\n", th
, th
->service
);
125 rb_erase(&th
->node
, &xi
->ticket_handlers
);
126 ceph_crypto_key_destroy(&th
->session_key
);
128 ceph_buffer_put(th
->ticket_blob
);
132 static int ceph_x_proc_ticket_reply(struct ceph_auth_client
*ac
,
133 struct ceph_crypto_key
*secret
,
134 void *buf
, void *end
)
136 struct ceph_x_info
*xi
= ac
->private;
144 dbuf
= kmalloc(TEMP_TICKET_BUF_LEN
, GFP_NOFS
);
149 ticket_buf
= kmalloc(TEMP_TICKET_BUF_LEN
, GFP_NOFS
);
153 ceph_decode_need(&p
, end
, 1 + sizeof(u32
), bad
);
154 reply_struct_v
= ceph_decode_8(&p
);
155 if (reply_struct_v
!= 1)
157 num
= ceph_decode_32(&p
);
158 dout("%d tickets\n", num
);
161 u8 tkt_struct_v
, blob_struct_v
;
162 struct ceph_x_ticket_handler
*th
;
166 struct timespec validity
;
167 struct ceph_crypto_key old_key
;
169 struct ceph_timespec new_validity
;
170 struct ceph_crypto_key new_session_key
;
171 struct ceph_buffer
*new_ticket_blob
;
172 unsigned long new_expires
, new_renew_after
;
175 ceph_decode_need(&p
, end
, sizeof(u32
) + 1, bad
);
177 type
= ceph_decode_32(&p
);
178 dout(" ticket type %d %s\n", type
, ceph_entity_type_name(type
));
180 tkt_struct_v
= ceph_decode_8(&p
);
181 if (tkt_struct_v
!= 1)
184 th
= get_ticket_handler(ac
, type
);
191 dlen
= ceph_x_decrypt(secret
, &p
, end
, dbuf
,
192 TEMP_TICKET_BUF_LEN
);
197 dout(" decrypted %d bytes\n", dlen
);
201 tkt_struct_v
= ceph_decode_8(&dp
);
202 if (tkt_struct_v
!= 1)
205 memcpy(&old_key
, &th
->session_key
, sizeof(old_key
));
206 ret
= ceph_crypto_key_decode(&new_session_key
, &dp
, dend
);
210 ceph_decode_copy(&dp
, &new_validity
, sizeof(new_validity
));
211 ceph_decode_timespec(&validity
, &new_validity
);
212 new_expires
= get_seconds() + validity
.tv_sec
;
213 new_renew_after
= new_expires
- (validity
.tv_sec
/ 4);
214 dout(" expires=%lu renew_after=%lu\n", new_expires
,
217 /* ticket blob for service */
218 ceph_decode_8_safe(&p
, end
, is_enc
, bad
);
222 dout(" encrypted ticket\n");
223 dlen
= ceph_x_decrypt(&old_key
, &p
, end
, ticket_buf
,
224 TEMP_TICKET_BUF_LEN
);
229 dlen
= ceph_decode_32(&tp
);
232 ceph_decode_32_safe(&p
, end
, dlen
, bad
);
233 ceph_decode_need(&p
, end
, dlen
, bad
);
234 ceph_decode_copy(&p
, ticket_buf
, dlen
);
237 dout(" ticket blob is %d bytes\n", dlen
);
238 ceph_decode_need(&tp
, tpend
, 1 + sizeof(u64
), bad
);
239 blob_struct_v
= ceph_decode_8(&tp
);
240 new_secret_id
= ceph_decode_64(&tp
);
241 ret
= ceph_decode_buffer(&new_ticket_blob
, &tp
, tpend
);
245 /* all is well, update our ticket */
246 ceph_crypto_key_destroy(&th
->session_key
);
248 ceph_buffer_put(th
->ticket_blob
);
249 th
->session_key
= new_session_key
;
250 th
->ticket_blob
= new_ticket_blob
;
251 th
->validity
= new_validity
;
252 th
->secret_id
= new_secret_id
;
253 th
->expires
= new_expires
;
254 th
->renew_after
= new_renew_after
;
255 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
256 type
, ceph_entity_type_name(type
), th
->secret_id
,
257 (int)th
->ticket_blob
->vec
.iov_len
);
258 xi
->have_keys
|= th
->service
;
273 static int ceph_x_build_authorizer(struct ceph_auth_client
*ac
,
274 struct ceph_x_ticket_handler
*th
,
275 struct ceph_x_authorizer
*au
)
278 struct ceph_x_authorize_a
*msg_a
;
279 struct ceph_x_authorize_b msg_b
;
282 int ticket_blob_len
=
283 (th
->ticket_blob
? th
->ticket_blob
->vec
.iov_len
: 0);
285 dout("build_authorizer for %s %p\n",
286 ceph_entity_type_name(th
->service
), au
);
288 maxlen
= sizeof(*msg_a
) + sizeof(msg_b
) +
289 ceph_x_encrypt_buflen(ticket_blob_len
);
290 dout(" need len %d\n", maxlen
);
291 if (au
->buf
&& au
->buf
->alloc_len
< maxlen
) {
292 ceph_buffer_put(au
->buf
);
296 au
->buf
= ceph_buffer_new(maxlen
, GFP_NOFS
);
300 au
->service
= th
->service
;
302 msg_a
= au
->buf
->vec
.iov_base
;
304 msg_a
->global_id
= cpu_to_le64(ac
->global_id
);
305 msg_a
->service_id
= cpu_to_le32(th
->service
);
306 msg_a
->ticket_blob
.struct_v
= 1;
307 msg_a
->ticket_blob
.secret_id
= cpu_to_le64(th
->secret_id
);
308 msg_a
->ticket_blob
.blob_len
= cpu_to_le32(ticket_blob_len
);
309 if (ticket_blob_len
) {
310 memcpy(msg_a
->ticket_blob
.blob
, th
->ticket_blob
->vec
.iov_base
,
311 th
->ticket_blob
->vec
.iov_len
);
313 dout(" th %p secret_id %lld %lld\n", th
, th
->secret_id
,
314 le64_to_cpu(msg_a
->ticket_blob
.secret_id
));
317 p
+= ticket_blob_len
;
318 end
= au
->buf
->vec
.iov_base
+ au
->buf
->vec
.iov_len
;
320 get_random_bytes(&au
->nonce
, sizeof(au
->nonce
));
322 msg_b
.nonce
= cpu_to_le64(au
->nonce
);
323 ret
= ceph_x_encrypt(&th
->session_key
, &msg_b
, sizeof(msg_b
),
328 au
->buf
->vec
.iov_len
= p
- au
->buf
->vec
.iov_base
;
329 dout(" built authorizer nonce %llx len %d\n", au
->nonce
,
330 (int)au
->buf
->vec
.iov_len
);
331 BUG_ON(au
->buf
->vec
.iov_len
> maxlen
);
335 ceph_buffer_put(au
->buf
);
340 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler
*th
,
343 ceph_decode_need(p
, end
, 1 + sizeof(u64
), bad
);
345 ceph_encode_64(p
, th
->secret_id
);
346 if (th
->ticket_blob
) {
347 const char *buf
= th
->ticket_blob
->vec
.iov_base
;
348 u32 len
= th
->ticket_blob
->vec
.iov_len
;
350 ceph_encode_32_safe(p
, end
, len
, bad
);
351 ceph_encode_copy_safe(p
, end
, buf
, len
, bad
);
353 ceph_encode_32_safe(p
, end
, 0, bad
);
361 static void ceph_x_validate_tickets(struct ceph_auth_client
*ac
, int *pneed
)
363 int want
= ac
->want_keys
;
364 struct ceph_x_info
*xi
= ac
->private;
367 *pneed
= ac
->want_keys
& ~(xi
->have_keys
);
369 for (service
= 1; service
<= want
; service
<<= 1) {
370 struct ceph_x_ticket_handler
*th
;
372 if (!(ac
->want_keys
& service
))
375 if (*pneed
& service
)
378 th
= get_ticket_handler(ac
, service
);
385 if (get_seconds() >= th
->renew_after
)
387 if (get_seconds() >= th
->expires
)
388 xi
->have_keys
&= ~service
;
393 static int ceph_x_build_request(struct ceph_auth_client
*ac
,
394 void *buf
, void *end
)
396 struct ceph_x_info
*xi
= ac
->private;
398 struct ceph_x_request_header
*head
= buf
;
400 struct ceph_x_ticket_handler
*th
=
401 get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
406 ceph_x_validate_tickets(ac
, &need
);
408 dout("build_request want %x have %x need %x\n",
409 ac
->want_keys
, xi
->have_keys
, need
);
411 if (need
& CEPH_ENTITY_TYPE_AUTH
) {
412 struct ceph_x_authenticate
*auth
= (void *)(head
+ 1);
414 struct ceph_x_challenge_blob tmp
;
421 dout(" get_auth_session_key\n");
422 head
->op
= cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY
);
424 /* encrypt and hash */
425 get_random_bytes(&auth
->client_challenge
, sizeof(u64
));
426 tmp
.client_challenge
= auth
->client_challenge
;
427 tmp
.server_challenge
= cpu_to_le64(xi
->server_challenge
);
428 ret
= ceph_x_encrypt(&xi
->secret
, &tmp
, sizeof(tmp
),
429 tmp_enc
, sizeof(tmp_enc
));
435 for (u
= (u64
*)tmp_enc
; u
+ 1 <= (u64
*)(tmp_enc
+ ret
); u
++)
436 auth
->key
^= *(__le64
*)u
;
437 dout(" server_challenge %llx client_challenge %llx key %llx\n",
438 xi
->server_challenge
, le64_to_cpu(auth
->client_challenge
),
439 le64_to_cpu(auth
->key
));
441 /* now encode the old ticket if exists */
442 ret
= ceph_x_encode_ticket(th
, &p
, end
);
451 struct ceph_x_service_ticket_request
*req
;
455 head
->op
= cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY
);
457 ret
= ceph_x_build_authorizer(ac
, th
, &xi
->auth_authorizer
);
460 ceph_encode_copy(&p
, xi
->auth_authorizer
.buf
->vec
.iov_base
,
461 xi
->auth_authorizer
.buf
->vec
.iov_len
);
464 req
->keys
= cpu_to_le32(need
);
472 static int ceph_x_handle_reply(struct ceph_auth_client
*ac
, int result
,
473 void *buf
, void *end
)
475 struct ceph_x_info
*xi
= ac
->private;
476 struct ceph_x_reply_header
*head
= buf
;
477 struct ceph_x_ticket_handler
*th
;
483 return result
; /* XXX hmm? */
487 struct ceph_x_server_challenge
*sc
= buf
;
489 if (len
!= sizeof(*sc
))
491 xi
->server_challenge
= le64_to_cpu(sc
->server_challenge
);
492 dout("handle_reply got server challenge %llx\n",
493 xi
->server_challenge
);
494 xi
->starting
= false;
495 xi
->have_keys
&= ~CEPH_ENTITY_TYPE_AUTH
;
499 op
= le16_to_cpu(head
->op
);
500 result
= le32_to_cpu(head
->result
);
501 dout("handle_reply op %d result %d\n", op
, result
);
503 case CEPHX_GET_AUTH_SESSION_KEY
:
504 /* verify auth key */
505 ret
= ceph_x_proc_ticket_reply(ac
, &xi
->secret
,
506 buf
+ sizeof(*head
), end
);
509 case CEPHX_GET_PRINCIPAL_SESSION_KEY
:
510 th
= get_ticket_handler(ac
, CEPH_ENTITY_TYPE_AUTH
);
513 ret
= ceph_x_proc_ticket_reply(ac
, &th
->session_key
,
514 buf
+ sizeof(*head
), end
);
522 if (ac
->want_keys
== xi
->have_keys
)
527 static int ceph_x_create_authorizer(
528 struct ceph_auth_client
*ac
, int peer_type
,
529 struct ceph_authorizer
**a
,
530 void **buf
, size_t *len
,
531 void **reply_buf
, size_t *reply_len
)
533 struct ceph_x_authorizer
*au
;
534 struct ceph_x_ticket_handler
*th
;
537 th
= get_ticket_handler(ac
, peer_type
);
541 au
= kzalloc(sizeof(*au
), GFP_NOFS
);
545 ret
= ceph_x_build_authorizer(ac
, th
, au
);
551 *a
= (struct ceph_authorizer
*)au
;
552 *buf
= au
->buf
->vec
.iov_base
;
553 *len
= au
->buf
->vec
.iov_len
;
554 *reply_buf
= au
->reply_buf
;
555 *reply_len
= sizeof(au
->reply_buf
);
559 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client
*ac
,
560 struct ceph_authorizer
*a
, size_t len
)
562 struct ceph_x_authorizer
*au
= (void *)a
;
563 struct ceph_x_ticket_handler
*th
;
565 struct ceph_x_authorize_reply reply
;
566 void *p
= au
->reply_buf
;
567 void *end
= p
+ sizeof(au
->reply_buf
);
569 th
= get_ticket_handler(ac
, au
->service
);
572 ret
= ceph_x_decrypt(&th
->session_key
, &p
, end
, &reply
, sizeof(reply
));
575 if (ret
!= sizeof(reply
))
578 if (au
->nonce
+ 1 != le64_to_cpu(reply
.nonce_plus_one
))
582 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
583 au
->nonce
, le64_to_cpu(reply
.nonce_plus_one
), ret
);
587 static void ceph_x_destroy_authorizer(struct ceph_auth_client
*ac
,
588 struct ceph_authorizer
*a
)
590 struct ceph_x_authorizer
*au
= (void *)a
;
592 ceph_buffer_put(au
->buf
);
597 static void ceph_x_reset(struct ceph_auth_client
*ac
)
599 struct ceph_x_info
*xi
= ac
->private;
603 xi
->server_challenge
= 0;
606 static void ceph_x_destroy(struct ceph_auth_client
*ac
)
608 struct ceph_x_info
*xi
= ac
->private;
611 dout("ceph_x_destroy %p\n", ac
);
612 ceph_crypto_key_destroy(&xi
->secret
);
614 while ((p
= rb_first(&xi
->ticket_handlers
)) != NULL
) {
615 struct ceph_x_ticket_handler
*th
=
616 rb_entry(p
, struct ceph_x_ticket_handler
, node
);
617 remove_ticket_handler(ac
, th
);
620 if (xi
->auth_authorizer
.buf
)
621 ceph_buffer_put(xi
->auth_authorizer
.buf
);
627 static void ceph_x_invalidate_authorizer(struct ceph_auth_client
*ac
,
630 struct ceph_x_ticket_handler
*th
;
632 th
= get_ticket_handler(ac
, peer_type
);
634 remove_ticket_handler(ac
, th
);
638 static const struct ceph_auth_client_ops ceph_x_ops
= {
640 .is_authenticated
= ceph_x_is_authenticated
,
641 .should_authenticate
= ceph_x_should_authenticate
,
642 .build_request
= ceph_x_build_request
,
643 .handle_reply
= ceph_x_handle_reply
,
644 .create_authorizer
= ceph_x_create_authorizer
,
645 .verify_authorizer_reply
= ceph_x_verify_authorizer_reply
,
646 .destroy_authorizer
= ceph_x_destroy_authorizer
,
647 .invalidate_authorizer
= ceph_x_invalidate_authorizer
,
648 .reset
= ceph_x_reset
,
649 .destroy
= ceph_x_destroy
,
653 int ceph_x_init(struct ceph_auth_client
*ac
)
655 struct ceph_x_info
*xi
;
658 dout("ceph_x_init %p\n", ac
);
660 xi
= kzalloc(sizeof(*xi
), GFP_NOFS
);
666 pr_err("no secret set (for auth_x protocol)\n");
670 ret
= ceph_crypto_key_unarmor(&xi
->secret
, ac
->secret
);
675 xi
->ticket_handlers
= RB_ROOT
;
677 ac
->protocol
= CEPH_AUTH_CEPHX
;
679 ac
->ops
= &ceph_x_ops
;