1 /* Kerberos-based RxRPC security
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <crypto/skcipher.h>
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/udp.h>
19 #include <linux/scatterlist.h>
20 #include <linux/ctype.h>
21 #include <linux/slab.h>
23 #include <net/af_rxrpc.h>
24 #include <keys/rxrpc-type.h>
25 #include "ar-internal.h"
27 #define RXKAD_VERSION 2
28 #define MAXKRB5TICKETLEN 1024
29 #define RXKAD_TKT_TYPE_KERBEROS_V5 256
30 #define ANAME_SZ 40 /* size of authentication name */
31 #define INST_SZ 40 /* size of principal's instance */
32 #define REALM_SZ 40 /* size of principal's auth domain */
33 #define SNAME_SZ 40 /* size of service name */
35 struct rxkad_level1_hdr
{
36 __be32 data_size
; /* true data size (excluding padding) */
39 struct rxkad_level2_hdr
{
40 __be32 data_size
; /* true data size (excluding padding) */
41 __be32 checksum
; /* decrypted data checksum */
45 * this holds a pinned cipher so that keventd doesn't get called by the cipher
46 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
49 static struct crypto_skcipher
*rxkad_ci
;
50 static DEFINE_MUTEX(rxkad_ci_mutex
);
53 * initialise connection security
55 static int rxkad_init_connection_security(struct rxrpc_connection
*conn
)
57 struct crypto_skcipher
*ci
;
58 struct rxrpc_key_token
*token
;
61 _enter("{%d},{%x}", conn
->debug_id
, key_serial(conn
->key
));
63 token
= conn
->key
->payload
.data
[0];
64 conn
->security_ix
= token
->security_index
;
66 ci
= crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC
);
73 if (crypto_skcipher_setkey(ci
, token
->kad
->session_key
,
74 sizeof(token
->kad
->session_key
)) < 0)
77 switch (conn
->security_level
) {
78 case RXRPC_SECURITY_PLAIN
:
80 case RXRPC_SECURITY_AUTH
:
82 conn
->security_size
= sizeof(struct rxkad_level1_hdr
);
83 conn
->header_size
+= sizeof(struct rxkad_level1_hdr
);
85 case RXRPC_SECURITY_ENCRYPT
:
87 conn
->security_size
= sizeof(struct rxkad_level2_hdr
);
88 conn
->header_size
+= sizeof(struct rxkad_level2_hdr
);
103 * prime the encryption state with the invariant parts of a connection's
106 static void rxkad_prime_packet_security(struct rxrpc_connection
*conn
)
108 struct rxrpc_key_token
*token
;
109 SKCIPHER_REQUEST_ON_STACK(req
, conn
->cipher
);
110 struct scatterlist sg
[2];
111 struct rxrpc_crypt iv
;
114 } tmpbuf
__attribute__((aligned(16))); /* must all be in same page */
121 token
= conn
->key
->payload
.data
[0];
122 memcpy(&iv
, token
->kad
->session_key
, sizeof(iv
));
124 tmpbuf
.x
[0] = htonl(conn
->epoch
);
125 tmpbuf
.x
[1] = htonl(conn
->cid
);
127 tmpbuf
.x
[3] = htonl(conn
->security_ix
);
129 sg_init_one(&sg
[0], &tmpbuf
, sizeof(tmpbuf
));
130 sg_init_one(&sg
[1], &tmpbuf
, sizeof(tmpbuf
));
132 skcipher_request_set_tfm(req
, conn
->cipher
);
133 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
134 skcipher_request_set_crypt(req
, &sg
[1], &sg
[0], sizeof(tmpbuf
), iv
.x
);
136 crypto_skcipher_encrypt(req
);
137 skcipher_request_zero(req
);
139 memcpy(&conn
->csum_iv
, &tmpbuf
.x
[2], sizeof(conn
->csum_iv
));
140 ASSERTCMP((u32 __force
)conn
->csum_iv
.n
[0], ==, (u32 __force
)tmpbuf
.x
[2]);
146 * partially encrypt a packet (level 1 security)
148 static int rxkad_secure_packet_auth(const struct rxrpc_call
*call
,
153 struct rxrpc_skb_priv
*sp
;
154 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
155 struct rxrpc_crypt iv
;
156 struct scatterlist sg
[2];
158 struct rxkad_level1_hdr hdr
;
159 __be32 first
; /* first four bytes of data and padding */
160 } tmpbuf
__attribute__((aligned(8))); /* must all be in same page */
167 check
= sp
->hdr
.seq
^ sp
->hdr
.callNumber
;
168 data_size
|= (u32
)check
<< 16;
170 tmpbuf
.hdr
.data_size
= htonl(data_size
);
171 memcpy(&tmpbuf
.first
, sechdr
+ 4, sizeof(tmpbuf
.first
));
173 /* start the encryption afresh */
174 memset(&iv
, 0, sizeof(iv
));
176 sg_init_one(&sg
[0], &tmpbuf
, sizeof(tmpbuf
));
177 sg_init_one(&sg
[1], &tmpbuf
, sizeof(tmpbuf
));
179 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
180 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
181 skcipher_request_set_crypt(req
, &sg
[1], &sg
[0], sizeof(tmpbuf
), iv
.x
);
183 crypto_skcipher_encrypt(req
);
184 skcipher_request_zero(req
);
186 memcpy(sechdr
, &tmpbuf
, sizeof(tmpbuf
));
193 * wholly encrypt a packet (level 2 security)
195 static int rxkad_secure_packet_encrypt(const struct rxrpc_call
*call
,
200 const struct rxrpc_key_token
*token
;
201 struct rxkad_level2_hdr rxkhdr
202 __attribute__((aligned(8))); /* must be all on one page */
203 struct rxrpc_skb_priv
*sp
;
204 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
205 struct rxrpc_crypt iv
;
206 struct scatterlist sg
[16];
207 struct sk_buff
*trailer
;
217 check
= sp
->hdr
.seq
^ sp
->hdr
.callNumber
;
219 rxkhdr
.data_size
= htonl(data_size
| (u32
)check
<< 16);
222 /* encrypt from the session key */
223 token
= call
->conn
->key
->payload
.data
[0];
224 memcpy(&iv
, token
->kad
->session_key
, sizeof(iv
));
226 sg_init_one(&sg
[0], sechdr
, sizeof(rxkhdr
));
227 sg_init_one(&sg
[1], &rxkhdr
, sizeof(rxkhdr
));
229 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
230 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
231 skcipher_request_set_crypt(req
, &sg
[1], &sg
[0], sizeof(rxkhdr
), iv
.x
);
233 crypto_skcipher_encrypt(req
);
235 /* we want to encrypt the skbuff in-place */
236 nsg
= skb_cow_data(skb
, 0, &trailer
);
238 if (nsg
< 0 || nsg
> 16)
241 len
= data_size
+ call
->conn
->size_align
- 1;
242 len
&= ~(call
->conn
->size_align
- 1);
244 sg_init_table(sg
, nsg
);
245 skb_to_sgvec(skb
, sg
, 0, len
);
247 skcipher_request_set_crypt(req
, sg
, sg
, len
, iv
.x
);
249 crypto_skcipher_encrypt(req
);
255 skcipher_request_zero(req
);
260 * checksum an RxRPC packet header
262 static int rxkad_secure_packet(const struct rxrpc_call
*call
,
267 struct rxrpc_skb_priv
*sp
;
268 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
269 struct rxrpc_crypt iv
;
270 struct scatterlist sg
[2];
273 } tmpbuf
__attribute__((aligned(8))); /* must all be in same page */
279 _enter("{%d{%x}},{#%u},%zu,",
280 call
->debug_id
, key_serial(call
->conn
->key
), sp
->hdr
.seq
,
283 if (!call
->conn
->cipher
)
286 ret
= key_validate(call
->conn
->key
);
290 /* continue encrypting from where we left off */
291 memcpy(&iv
, call
->conn
->csum_iv
.x
, sizeof(iv
));
293 /* calculate the security checksum */
294 x
= call
->channel
<< (32 - RXRPC_CIDSHIFT
);
295 x
|= sp
->hdr
.seq
& 0x3fffffff;
296 tmpbuf
.x
[0] = htonl(sp
->hdr
.callNumber
);
297 tmpbuf
.x
[1] = htonl(x
);
299 sg_init_one(&sg
[0], &tmpbuf
, sizeof(tmpbuf
));
300 sg_init_one(&sg
[1], &tmpbuf
, sizeof(tmpbuf
));
302 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
303 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
304 skcipher_request_set_crypt(req
, &sg
[1], &sg
[0], sizeof(tmpbuf
), iv
.x
);
306 crypto_skcipher_encrypt(req
);
307 skcipher_request_zero(req
);
309 y
= ntohl(tmpbuf
.x
[1]);
310 y
= (y
>> 16) & 0xffff;
312 y
= 1; /* zero checksums are not permitted */
315 switch (call
->conn
->security_level
) {
316 case RXRPC_SECURITY_PLAIN
:
319 case RXRPC_SECURITY_AUTH
:
320 ret
= rxkad_secure_packet_auth(call
, skb
, data_size
, sechdr
);
322 case RXRPC_SECURITY_ENCRYPT
:
323 ret
= rxkad_secure_packet_encrypt(call
, skb
, data_size
,
331 _leave(" = %d [set %hx]", ret
, y
);
336 * decrypt partial encryption on a packet (level 1 security)
338 static int rxkad_verify_packet_auth(const struct rxrpc_call
*call
,
342 struct rxkad_level1_hdr sechdr
;
343 struct rxrpc_skb_priv
*sp
;
344 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
345 struct rxrpc_crypt iv
;
346 struct scatterlist sg
[16];
347 struct sk_buff
*trailer
;
356 /* we want to decrypt the skbuff in-place */
357 nsg
= skb_cow_data(skb
, 0, &trailer
);
358 if (nsg
< 0 || nsg
> 16)
361 sg_init_table(sg
, nsg
);
362 skb_to_sgvec(skb
, sg
, 0, 8);
364 /* start the decryption afresh */
365 memset(&iv
, 0, sizeof(iv
));
367 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
368 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
369 skcipher_request_set_crypt(req
, sg
, sg
, 8, iv
.x
);
371 crypto_skcipher_decrypt(req
);
372 skcipher_request_zero(req
);
374 /* remove the decrypted packet length */
375 if (skb_copy_bits(skb
, 0, &sechdr
, sizeof(sechdr
)) < 0)
377 if (!skb_pull(skb
, sizeof(sechdr
)))
380 buf
= ntohl(sechdr
.data_size
);
381 data_size
= buf
& 0xffff;
384 check
^= sp
->hdr
.seq
^ sp
->hdr
.callNumber
;
387 *_abort_code
= RXKADSEALEDINCON
;
391 /* shorten the packet to remove the padding */
392 if (data_size
> skb
->len
)
394 else if (data_size
< skb
->len
)
395 skb
->len
= data_size
;
397 _leave(" = 0 [dlen=%x]", data_size
);
401 *_abort_code
= RXKADDATALEN
;
403 _leave(" = -EPROTO");
407 _leave(" = -ENOMEM");
412 * wholly decrypt a packet (level 2 security)
414 static int rxkad_verify_packet_encrypt(const struct rxrpc_call
*call
,
418 const struct rxrpc_key_token
*token
;
419 struct rxkad_level2_hdr sechdr
;
420 struct rxrpc_skb_priv
*sp
;
421 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
422 struct rxrpc_crypt iv
;
423 struct scatterlist _sg
[4], *sg
;
424 struct sk_buff
*trailer
;
429 _enter(",{%d}", skb
->len
);
433 /* we want to decrypt the skbuff in-place */
434 nsg
= skb_cow_data(skb
, 0, &trailer
);
439 if (unlikely(nsg
> 4)) {
440 sg
= kmalloc(sizeof(*sg
) * nsg
, GFP_NOIO
);
445 sg_init_table(sg
, nsg
);
446 skb_to_sgvec(skb
, sg
, 0, skb
->len
);
448 /* decrypt from the session key */
449 token
= call
->conn
->key
->payload
.data
[0];
450 memcpy(&iv
, token
->kad
->session_key
, sizeof(iv
));
452 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
453 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
454 skcipher_request_set_crypt(req
, sg
, sg
, skb
->len
, iv
.x
);
456 crypto_skcipher_decrypt(req
);
457 skcipher_request_zero(req
);
461 /* remove the decrypted packet length */
462 if (skb_copy_bits(skb
, 0, &sechdr
, sizeof(sechdr
)) < 0)
464 if (!skb_pull(skb
, sizeof(sechdr
)))
467 buf
= ntohl(sechdr
.data_size
);
468 data_size
= buf
& 0xffff;
471 check
^= sp
->hdr
.seq
^ sp
->hdr
.callNumber
;
474 *_abort_code
= RXKADSEALEDINCON
;
478 /* shorten the packet to remove the padding */
479 if (data_size
> skb
->len
)
481 else if (data_size
< skb
->len
)
482 skb
->len
= data_size
;
484 _leave(" = 0 [dlen=%x]", data_size
);
488 *_abort_code
= RXKADDATALEN
;
490 _leave(" = -EPROTO");
494 _leave(" = -ENOMEM");
499 * verify the security on a received packet
501 static int rxkad_verify_packet(const struct rxrpc_call
*call
,
505 SKCIPHER_REQUEST_ON_STACK(req
, call
->conn
->cipher
);
506 struct rxrpc_skb_priv
*sp
;
507 struct rxrpc_crypt iv
;
508 struct scatterlist sg
[2];
511 } tmpbuf
__attribute__((aligned(8))); /* must all be in same page */
518 _enter("{%d{%x}},{#%u}",
519 call
->debug_id
, key_serial(call
->conn
->key
), sp
->hdr
.seq
);
521 if (!call
->conn
->cipher
)
524 if (sp
->hdr
.securityIndex
!= RXRPC_SECURITY_RXKAD
) {
525 *_abort_code
= RXKADINCONSISTENCY
;
526 _leave(" = -EPROTO [not rxkad]");
530 /* continue encrypting from where we left off */
531 memcpy(&iv
, call
->conn
->csum_iv
.x
, sizeof(iv
));
533 /* validate the security checksum */
534 x
= call
->channel
<< (32 - RXRPC_CIDSHIFT
);
535 x
|= sp
->hdr
.seq
& 0x3fffffff;
536 tmpbuf
.x
[0] = htonl(call
->call_id
);
537 tmpbuf
.x
[1] = htonl(x
);
539 sg_init_one(&sg
[0], &tmpbuf
, sizeof(tmpbuf
));
540 sg_init_one(&sg
[1], &tmpbuf
, sizeof(tmpbuf
));
542 skcipher_request_set_tfm(req
, call
->conn
->cipher
);
543 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
544 skcipher_request_set_crypt(req
, &sg
[1], &sg
[0], sizeof(tmpbuf
), iv
.x
);
546 crypto_skcipher_encrypt(req
);
547 skcipher_request_zero(req
);
549 y
= ntohl(tmpbuf
.x
[1]);
550 cksum
= (y
>> 16) & 0xffff;
552 cksum
= 1; /* zero checksums are not permitted */
554 if (sp
->hdr
.cksum
!= cksum
) {
555 *_abort_code
= RXKADSEALEDINCON
;
556 _leave(" = -EPROTO [csum failed]");
560 switch (call
->conn
->security_level
) {
561 case RXRPC_SECURITY_PLAIN
:
564 case RXRPC_SECURITY_AUTH
:
565 ret
= rxkad_verify_packet_auth(call
, skb
, _abort_code
);
567 case RXRPC_SECURITY_ENCRYPT
:
568 ret
= rxkad_verify_packet_encrypt(call
, skb
, _abort_code
);
575 _leave(" = %d", ret
);
582 static int rxkad_issue_challenge(struct rxrpc_connection
*conn
)
584 struct rxkad_challenge challenge
;
585 struct rxrpc_wire_header whdr
;
592 _enter("{%d,%x}", conn
->debug_id
, key_serial(conn
->key
));
594 ret
= key_validate(conn
->key
);
598 get_random_bytes(&conn
->security_nonce
, sizeof(conn
->security_nonce
));
600 challenge
.version
= htonl(2);
601 challenge
.nonce
= htonl(conn
->security_nonce
);
602 challenge
.min_level
= htonl(0);
603 challenge
.__padding
= 0;
605 msg
.msg_name
= &conn
->trans
->peer
->srx
.transport
.sin
;
606 msg
.msg_namelen
= sizeof(conn
->trans
->peer
->srx
.transport
.sin
);
607 msg
.msg_control
= NULL
;
608 msg
.msg_controllen
= 0;
611 whdr
.epoch
= htonl(conn
->epoch
);
612 whdr
.cid
= htonl(conn
->cid
);
615 whdr
.type
= RXRPC_PACKET_TYPE_CHALLENGE
;
616 whdr
.flags
= conn
->out_clientflag
;
618 whdr
.securityIndex
= conn
->security_ix
;
620 whdr
.serviceId
= htons(conn
->service_id
);
622 iov
[0].iov_base
= &whdr
;
623 iov
[0].iov_len
= sizeof(whdr
);
624 iov
[1].iov_base
= &challenge
;
625 iov
[1].iov_len
= sizeof(challenge
);
627 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
629 serial
= atomic_inc_return(&conn
->serial
);
630 whdr
.serial
= htonl(serial
);
631 _proto("Tx CHALLENGE %%%u", serial
);
633 ret
= kernel_sendmsg(conn
->trans
->local
->socket
, &msg
, iov
, 2, len
);
635 _debug("sendmsg failed: %d", ret
);
644 * send a Kerberos security response
646 static int rxkad_send_response(struct rxrpc_connection
*conn
,
647 struct rxrpc_host_header
*hdr
,
648 struct rxkad_response
*resp
,
649 const struct rxkad_key
*s2
)
651 struct rxrpc_wire_header whdr
;
660 msg
.msg_name
= &conn
->trans
->peer
->srx
.transport
.sin
;
661 msg
.msg_namelen
= sizeof(conn
->trans
->peer
->srx
.transport
.sin
);
662 msg
.msg_control
= NULL
;
663 msg
.msg_controllen
= 0;
666 memset(&whdr
, 0, sizeof(whdr
));
667 whdr
.epoch
= htonl(hdr
->epoch
);
668 whdr
.cid
= htonl(hdr
->cid
);
669 whdr
.type
= RXRPC_PACKET_TYPE_RESPONSE
;
670 whdr
.flags
= conn
->out_clientflag
;
671 whdr
.securityIndex
= hdr
->securityIndex
;
672 whdr
.serviceId
= htons(hdr
->serviceId
);
674 iov
[0].iov_base
= &whdr
;
675 iov
[0].iov_len
= sizeof(whdr
);
676 iov
[1].iov_base
= resp
;
677 iov
[1].iov_len
= sizeof(*resp
);
678 iov
[2].iov_base
= (void *)s2
->ticket
;
679 iov
[2].iov_len
= s2
->ticket_len
;
681 len
= iov
[0].iov_len
+ iov
[1].iov_len
+ iov
[2].iov_len
;
683 serial
= atomic_inc_return(&conn
->serial
);
684 whdr
.serial
= htonl(serial
);
685 _proto("Tx RESPONSE %%%u", serial
);
687 ret
= kernel_sendmsg(conn
->trans
->local
->socket
, &msg
, iov
, 3, len
);
689 _debug("sendmsg failed: %d", ret
);
698 * calculate the response checksum
700 static void rxkad_calc_response_checksum(struct rxkad_response
*response
)
704 u8
*p
= (u8
*) response
;
706 for (loop
= sizeof(*response
); loop
> 0; loop
--)
707 csum
= csum
* 0x10204081 + *p
++;
709 response
->encrypted
.checksum
= htonl(csum
);
713 * load a scatterlist with a potentially split-page buffer
715 static void rxkad_sg_set_buf2(struct scatterlist sg
[2],
716 void *buf
, size_t buflen
)
720 sg_init_table(sg
, 2);
722 sg_set_buf(&sg
[0], buf
, buflen
);
723 if (sg
[0].offset
+ buflen
> PAGE_SIZE
) {
724 /* the buffer was split over two pages */
725 sg
[0].length
= PAGE_SIZE
- sg
[0].offset
;
726 sg_set_buf(&sg
[1], buf
+ sg
[0].length
, buflen
- sg
[0].length
);
730 sg_mark_end(&sg
[nsg
- 1]);
732 ASSERTCMP(sg
[0].length
+ sg
[1].length
, ==, buflen
);
736 * encrypt the response packet
738 static void rxkad_encrypt_response(struct rxrpc_connection
*conn
,
739 struct rxkad_response
*resp
,
740 const struct rxkad_key
*s2
)
742 SKCIPHER_REQUEST_ON_STACK(req
, conn
->cipher
);
743 struct rxrpc_crypt iv
;
744 struct scatterlist sg
[2];
746 /* continue encrypting from where we left off */
747 memcpy(&iv
, s2
->session_key
, sizeof(iv
));
749 rxkad_sg_set_buf2(sg
, &resp
->encrypted
, sizeof(resp
->encrypted
));
751 skcipher_request_set_tfm(req
, conn
->cipher
);
752 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
753 skcipher_request_set_crypt(req
, sg
, sg
, sizeof(resp
->encrypted
), iv
.x
);
755 crypto_skcipher_encrypt(req
);
756 skcipher_request_zero(req
);
760 * respond to a challenge packet
762 static int rxkad_respond_to_challenge(struct rxrpc_connection
*conn
,
766 const struct rxrpc_key_token
*token
;
767 struct rxkad_challenge challenge
;
768 struct rxkad_response resp
769 __attribute__((aligned(8))); /* must be aligned for crypto */
770 struct rxrpc_skb_priv
*sp
;
771 u32 version
, nonce
, min_level
, abort_code
;
774 _enter("{%d,%x}", conn
->debug_id
, key_serial(conn
->key
));
777 _leave(" = -EPROTO [no key]");
781 ret
= key_validate(conn
->key
);
783 *_abort_code
= RXKADEXPIRED
;
787 abort_code
= RXKADPACKETSHORT
;
789 if (skb_copy_bits(skb
, 0, &challenge
, sizeof(challenge
)) < 0)
792 version
= ntohl(challenge
.version
);
793 nonce
= ntohl(challenge
.nonce
);
794 min_level
= ntohl(challenge
.min_level
);
796 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
797 sp
->hdr
.serial
, version
, nonce
, min_level
);
799 abort_code
= RXKADINCONSISTENCY
;
800 if (version
!= RXKAD_VERSION
)
803 abort_code
= RXKADLEVELFAIL
;
804 if (conn
->security_level
< min_level
)
807 token
= conn
->key
->payload
.data
[0];
809 /* build the response packet */
810 memset(&resp
, 0, sizeof(resp
));
812 resp
.version
= htonl(RXKAD_VERSION
);
813 resp
.encrypted
.epoch
= htonl(conn
->epoch
);
814 resp
.encrypted
.cid
= htonl(conn
->cid
);
815 resp
.encrypted
.securityIndex
= htonl(conn
->security_ix
);
816 resp
.encrypted
.inc_nonce
= htonl(nonce
+ 1);
817 resp
.encrypted
.level
= htonl(conn
->security_level
);
818 resp
.kvno
= htonl(token
->kad
->kvno
);
819 resp
.ticket_len
= htonl(token
->kad
->ticket_len
);
821 resp
.encrypted
.call_id
[0] =
822 htonl(conn
->channels
[0] ? conn
->channels
[0]->call_id
: 0);
823 resp
.encrypted
.call_id
[1] =
824 htonl(conn
->channels
[1] ? conn
->channels
[1]->call_id
: 0);
825 resp
.encrypted
.call_id
[2] =
826 htonl(conn
->channels
[2] ? conn
->channels
[2]->call_id
: 0);
827 resp
.encrypted
.call_id
[3] =
828 htonl(conn
->channels
[3] ? conn
->channels
[3]->call_id
: 0);
830 /* calculate the response checksum and then do the encryption */
831 rxkad_calc_response_checksum(&resp
);
832 rxkad_encrypt_response(conn
, &resp
, token
->kad
);
833 return rxkad_send_response(conn
, &sp
->hdr
, &resp
, token
->kad
);
836 *_abort_code
= abort_code
;
837 _leave(" = -EPROTO [%d]", abort_code
);
842 * decrypt the kerberos IV ticket in the response
844 static int rxkad_decrypt_ticket(struct rxrpc_connection
*conn
,
845 void *ticket
, size_t ticket_len
,
846 struct rxrpc_crypt
*_session_key
,
850 struct skcipher_request
*req
;
851 struct rxrpc_crypt iv
, key
;
852 struct scatterlist sg
[1];
858 u8
*p
, *q
, *name
, *end
;
860 _enter("{%d},{%x}", conn
->debug_id
, key_serial(conn
->server_key
));
864 ret
= key_validate(conn
->server_key
);
868 *_abort_code
= RXKADEXPIRED
;
871 *_abort_code
= RXKADNOAUTH
;
876 ASSERT(conn
->server_key
->payload
.data
[0] != NULL
);
877 ASSERTCMP((unsigned long) ticket
& 7UL, ==, 0);
879 memcpy(&iv
, &conn
->server_key
->payload
.data
[2], sizeof(iv
));
881 req
= skcipher_request_alloc(conn
->server_key
->payload
.data
[0],
884 *_abort_code
= RXKADNOAUTH
;
889 sg_init_one(&sg
[0], ticket
, ticket_len
);
891 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
892 skcipher_request_set_crypt(req
, sg
, sg
, ticket_len
, iv
.x
);
894 crypto_skcipher_decrypt(req
);
895 skcipher_request_free(req
);
898 end
= p
+ ticket_len
;
903 q = memchr(p, 0, end - p); \
904 if (!q || q - p > (size)) \
913 /* extract the ticket flags */
914 _debug("KIV FLAGS: %x", *p
);
915 little_endian
= *p
& 1;
918 /* extract the authentication name */
920 _debug("KIV ANAME: %s", name
);
922 /* extract the principal's instance */
924 _debug("KIV INST : %s", name
);
926 /* extract the principal's authentication domain */
928 _debug("KIV REALM: %s", name
);
930 if (end
- p
< 4 + 8 + 4 + 2)
933 /* get the IPv4 address of the entity that requested the ticket */
934 memcpy(&addr
, p
, sizeof(addr
));
936 _debug("KIV ADDR : %pI4", &addr
);
938 /* get the session key from the ticket */
939 memcpy(&key
, p
, sizeof(key
));
941 _debug("KIV KEY : %08x %08x", ntohl(key
.n
[0]), ntohl(key
.n
[1]));
942 memcpy(_session_key
, &key
, sizeof(key
));
944 /* get the ticket's lifetime */
945 life
= *p
++ * 5 * 60;
946 _debug("KIV LIFE : %u", life
);
948 /* get the issue time of the ticket */
951 memcpy(&stamp
, p
, 4);
952 issue
= le32_to_cpu(stamp
);
955 memcpy(&stamp
, p
, 4);
956 issue
= be32_to_cpu(stamp
);
960 _debug("KIV ISSUE: %lx [%lx]", issue
, now
);
962 /* check the ticket is in date */
964 *_abort_code
= RXKADNOAUTH
;
969 if (issue
< now
- life
) {
970 *_abort_code
= RXKADEXPIRED
;
975 *_expiry
= issue
+ life
;
977 /* get the service name */
979 _debug("KIV SNAME: %s", name
);
981 /* get the service instance name */
983 _debug("KIV SINST: %s", name
);
987 _leave(" = %d", ret
);
991 *_abort_code
= RXKADBADTICKET
;
997 * decrypt the response packet
999 static void rxkad_decrypt_response(struct rxrpc_connection
*conn
,
1000 struct rxkad_response
*resp
,
1001 const struct rxrpc_crypt
*session_key
)
1003 SKCIPHER_REQUEST_ON_STACK(req
, rxkad_ci
);
1004 struct scatterlist sg
[2];
1005 struct rxrpc_crypt iv
;
1007 _enter(",,%08x%08x",
1008 ntohl(session_key
->n
[0]), ntohl(session_key
->n
[1]));
1010 ASSERT(rxkad_ci
!= NULL
);
1012 mutex_lock(&rxkad_ci_mutex
);
1013 if (crypto_skcipher_setkey(rxkad_ci
, session_key
->x
,
1014 sizeof(*session_key
)) < 0)
1017 memcpy(&iv
, session_key
, sizeof(iv
));
1019 rxkad_sg_set_buf2(sg
, &resp
->encrypted
, sizeof(resp
->encrypted
));
1021 skcipher_request_set_tfm(req
, rxkad_ci
);
1022 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
1023 skcipher_request_set_crypt(req
, sg
, sg
, sizeof(resp
->encrypted
), iv
.x
);
1025 crypto_skcipher_decrypt(req
);
1026 skcipher_request_zero(req
);
1028 mutex_unlock(&rxkad_ci_mutex
);
1036 static int rxkad_verify_response(struct rxrpc_connection
*conn
,
1037 struct sk_buff
*skb
,
1040 struct rxkad_response response
1041 __attribute__((aligned(8))); /* must be aligned for crypto */
1042 struct rxrpc_skb_priv
*sp
;
1043 struct rxrpc_crypt session_key
;
1046 u32 abort_code
, version
, kvno
, ticket_len
, level
;
1050 _enter("{%d,%x}", conn
->debug_id
, key_serial(conn
->server_key
));
1052 abort_code
= RXKADPACKETSHORT
;
1053 if (skb_copy_bits(skb
, 0, &response
, sizeof(response
)) < 0)
1054 goto protocol_error
;
1055 if (!pskb_pull(skb
, sizeof(response
)))
1058 version
= ntohl(response
.version
);
1059 ticket_len
= ntohl(response
.ticket_len
);
1060 kvno
= ntohl(response
.kvno
);
1061 sp
= rxrpc_skb(skb
);
1062 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1063 sp
->hdr
.serial
, version
, kvno
, ticket_len
);
1065 abort_code
= RXKADINCONSISTENCY
;
1066 if (version
!= RXKAD_VERSION
)
1067 goto protocol_error
;
1069 abort_code
= RXKADTICKETLEN
;
1070 if (ticket_len
< 4 || ticket_len
> MAXKRB5TICKETLEN
)
1071 goto protocol_error
;
1073 abort_code
= RXKADUNKNOWNKEY
;
1074 if (kvno
>= RXKAD_TKT_TYPE_KERBEROS_V5
)
1075 goto protocol_error
;
1077 /* extract the kerberos ticket and decrypt and decode it */
1078 ticket
= kmalloc(ticket_len
, GFP_NOFS
);
1082 abort_code
= RXKADPACKETSHORT
;
1083 if (skb_copy_bits(skb
, 0, ticket
, ticket_len
) < 0)
1084 goto protocol_error_free
;
1086 ret
= rxkad_decrypt_ticket(conn
, ticket
, ticket_len
, &session_key
,
1087 &expiry
, &abort_code
);
1089 *_abort_code
= abort_code
;
1094 /* use the session key from inside the ticket to decrypt the
1096 rxkad_decrypt_response(conn
, &response
, &session_key
);
1098 abort_code
= RXKADSEALEDINCON
;
1099 if (ntohl(response
.encrypted
.epoch
) != conn
->epoch
)
1100 goto protocol_error_free
;
1101 if (ntohl(response
.encrypted
.cid
) != conn
->cid
)
1102 goto protocol_error_free
;
1103 if (ntohl(response
.encrypted
.securityIndex
) != conn
->security_ix
)
1104 goto protocol_error_free
;
1105 csum
= response
.encrypted
.checksum
;
1106 response
.encrypted
.checksum
= 0;
1107 rxkad_calc_response_checksum(&response
);
1108 if (response
.encrypted
.checksum
!= csum
)
1109 goto protocol_error_free
;
1111 if (ntohl(response
.encrypted
.call_id
[0]) > INT_MAX
||
1112 ntohl(response
.encrypted
.call_id
[1]) > INT_MAX
||
1113 ntohl(response
.encrypted
.call_id
[2]) > INT_MAX
||
1114 ntohl(response
.encrypted
.call_id
[3]) > INT_MAX
)
1115 goto protocol_error_free
;
1117 abort_code
= RXKADOUTOFSEQUENCE
;
1118 if (ntohl(response
.encrypted
.inc_nonce
) != conn
->security_nonce
+ 1)
1119 goto protocol_error_free
;
1121 abort_code
= RXKADLEVELFAIL
;
1122 level
= ntohl(response
.encrypted
.level
);
1123 if (level
> RXRPC_SECURITY_ENCRYPT
)
1124 goto protocol_error_free
;
1125 conn
->security_level
= level
;
1127 /* create a key to hold the security data and expiration time - after
1128 * this the connection security can be handled in exactly the same way
1129 * as for a client connection */
1130 ret
= rxrpc_get_server_data_key(conn
, &session_key
, expiry
, kvno
);
1140 protocol_error_free
:
1143 *_abort_code
= abort_code
;
1144 _leave(" = -EPROTO [%d]", abort_code
);
1149 * clear the connection security
1151 static void rxkad_clear(struct rxrpc_connection
*conn
)
1156 crypto_free_skcipher(conn
->cipher
);
1160 * Initialise the rxkad security service.
1162 static int rxkad_init(void)
1164 /* pin the cipher we need so that the crypto layer doesn't invoke
1165 * keventd to go get it */
1166 rxkad_ci
= crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC
);
1167 return PTR_ERR_OR_ZERO(rxkad_ci
);
1171 * Clean up the rxkad security service.
1173 static void rxkad_exit(void)
1176 crypto_free_skcipher(rxkad_ci
);
1180 * RxRPC Kerberos-based security
1182 const struct rxrpc_security rxkad
= {
1184 .security_index
= RXRPC_SECURITY_RXKAD
,
1187 .init_connection_security
= rxkad_init_connection_security
,
1188 .prime_packet_security
= rxkad_prime_packet_security
,
1189 .secure_packet
= rxkad_secure_packet
,
1190 .verify_packet
= rxkad_verify_packet
,
1191 .issue_challenge
= rxkad_issue_challenge
,
1192 .respond_to_challenge
= rxkad_respond_to_challenge
,
1193 .verify_response
= rxkad_verify_response
,
1194 .clear
= rxkad_clear
,