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->params.key));
63 token = conn->params.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->params.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 int 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;
111 struct rxrpc_crypt iv;
113 size_t tmpsize = 4 * sizeof(__be32);
117 if (!conn->params.key)
120 tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
124 token = conn->params.key->payload.data[0];
125 memcpy(&iv, token->kad->session_key, sizeof(iv));
127 tmpbuf[0] = htonl(conn->proto.epoch);
128 tmpbuf[1] = htonl(conn->proto.cid);
130 tmpbuf[3] = htonl(conn->security_ix);
132 sg_init_one(&sg, tmpbuf, tmpsize);
133 skcipher_request_set_tfm(req, conn->cipher);
134 skcipher_request_set_callback(req, 0, NULL, NULL);
135 skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
136 crypto_skcipher_encrypt(req);
137 skcipher_request_zero(req);
139 memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
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 rxkad_level1_hdr hdr;
156 struct rxrpc_crypt iv;
157 struct scatterlist sg;
164 check = sp->hdr.seq ^ sp->hdr.callNumber;
165 data_size |= (u32)check << 16;
167 hdr.data_size = htonl(data_size);
168 memcpy(sechdr, &hdr, sizeof(hdr));
170 /* start the encryption afresh */
171 memset(&iv, 0, sizeof(iv));
173 sg_init_one(&sg, sechdr, 8);
174 skcipher_request_set_tfm(req, call->conn->cipher);
175 skcipher_request_set_callback(req, 0, NULL, NULL);
176 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
177 crypto_skcipher_encrypt(req);
178 skcipher_request_zero(req);
185 * wholly encrypt a packet (level 2 security)
187 static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
192 const struct rxrpc_key_token *token;
193 struct rxkad_level2_hdr rxkhdr;
194 struct rxrpc_skb_priv *sp;
195 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
196 struct rxrpc_crypt iv;
197 struct scatterlist sg[16];
198 struct sk_buff *trailer;
208 check = sp->hdr.seq ^ sp->hdr.callNumber;
210 rxkhdr.data_size = htonl(data_size | (u32)check << 16);
212 memcpy(sechdr, &rxkhdr, sizeof(rxkhdr));
214 /* encrypt from the session key */
215 token = call->conn->params.key->payload.data[0];
216 memcpy(&iv, token->kad->session_key, sizeof(iv));
218 sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
219 skcipher_request_set_tfm(req, call->conn->cipher);
220 skcipher_request_set_callback(req, 0, NULL, NULL);
221 skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
222 crypto_skcipher_encrypt(req);
224 /* we want to encrypt the skbuff in-place */
225 nsg = skb_cow_data(skb, 0, &trailer);
227 if (nsg < 0 || nsg > 16)
230 len = data_size + call->conn->size_align - 1;
231 len &= ~(call->conn->size_align - 1);
233 sg_init_table(sg, nsg);
234 skb_to_sgvec(skb, sg, 0, len);
235 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
236 crypto_skcipher_encrypt(req);
242 skcipher_request_zero(req);
247 * checksum an RxRPC packet header
249 static int rxkad_secure_packet(struct rxrpc_call *call,
254 struct rxrpc_skb_priv *sp;
255 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
256 struct rxrpc_crypt iv;
257 struct scatterlist sg;
263 _enter("{%d{%x}},{#%u},%zu,",
264 call->debug_id, key_serial(call->conn->params.key),
265 sp->hdr.seq, data_size);
267 if (!call->conn->cipher)
270 ret = key_validate(call->conn->params.key);
274 /* continue encrypting from where we left off */
275 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
277 /* calculate the security checksum */
278 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
279 x |= sp->hdr.seq & 0x3fffffff;
280 call->crypto_buf[0] = htonl(sp->hdr.callNumber);
281 call->crypto_buf[1] = htonl(x);
283 sg_init_one(&sg, call->crypto_buf, 8);
284 skcipher_request_set_tfm(req, call->conn->cipher);
285 skcipher_request_set_callback(req, 0, NULL, NULL);
286 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
287 crypto_skcipher_encrypt(req);
288 skcipher_request_zero(req);
290 y = ntohl(call->crypto_buf[1]);
291 y = (y >> 16) & 0xffff;
293 y = 1; /* zero checksums are not permitted */
296 switch (call->conn->params.security_level) {
297 case RXRPC_SECURITY_PLAIN:
300 case RXRPC_SECURITY_AUTH:
301 ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
303 case RXRPC_SECURITY_ENCRYPT:
304 ret = rxkad_secure_packet_encrypt(call, skb, data_size,
312 _leave(" = %d [set %hx]", ret, y);
317 * decrypt partial encryption on a packet (level 1 security)
319 static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
320 unsigned int offset, unsigned int len,
323 struct rxkad_level1_hdr sechdr;
324 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
325 struct rxrpc_crypt iv;
326 struct scatterlist sg[16];
327 struct sk_buff *trailer;
335 rxrpc_abort_call("V1H", call, seq, RXKADSEALEDINCON, EPROTO);
339 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
340 * directly into the target buffer.
342 nsg = skb_cow_data(skb, 0, &trailer);
343 if (nsg < 0 || nsg > 16)
346 sg_init_table(sg, nsg);
347 skb_to_sgvec(skb, sg, offset, 8);
349 /* start the decryption afresh */
350 memset(&iv, 0, sizeof(iv));
352 skcipher_request_set_tfm(req, call->conn->cipher);
353 skcipher_request_set_callback(req, 0, NULL, NULL);
354 skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
355 crypto_skcipher_decrypt(req);
356 skcipher_request_zero(req);
358 /* Extract the decrypted packet length */
359 if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
360 rxrpc_abort_call("XV1", call, seq, RXKADDATALEN, EPROTO);
363 offset += sizeof(sechdr);
364 len -= sizeof(sechdr);
366 buf = ntohl(sechdr.data_size);
367 data_size = buf & 0xffff;
370 check ^= seq ^ call->call_id;
373 rxrpc_abort_call("V1C", call, seq, RXKADSEALEDINCON, EPROTO);
377 if (data_size > len) {
378 rxrpc_abort_call("V1L", call, seq, RXKADDATALEN, EPROTO);
382 _leave(" = 0 [dlen=%x]", data_size);
386 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
387 _leave(" = -EPROTO");
391 _leave(" = -ENOMEM");
396 * wholly decrypt a packet (level 2 security)
398 static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
399 unsigned int offset, unsigned int len,
402 const struct rxrpc_key_token *token;
403 struct rxkad_level2_hdr sechdr;
404 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
405 struct rxrpc_crypt iv;
406 struct scatterlist _sg[4], *sg;
407 struct sk_buff *trailer;
412 _enter(",{%d}", skb->len);
415 rxrpc_abort_call("V2H", call, seq, RXKADSEALEDINCON, EPROTO);
419 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
420 * directly into the target buffer.
422 nsg = skb_cow_data(skb, 0, &trailer);
427 if (unlikely(nsg > 4)) {
428 sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
433 sg_init_table(sg, nsg);
434 skb_to_sgvec(skb, sg, offset, len);
436 /* decrypt from the session key */
437 token = call->conn->params.key->payload.data[0];
438 memcpy(&iv, token->kad->session_key, sizeof(iv));
440 skcipher_request_set_tfm(req, call->conn->cipher);
441 skcipher_request_set_callback(req, 0, NULL, NULL);
442 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
443 crypto_skcipher_decrypt(req);
444 skcipher_request_zero(req);
448 /* Extract the decrypted packet length */
449 if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
450 rxrpc_abort_call("XV2", call, seq, RXKADDATALEN, EPROTO);
453 offset += sizeof(sechdr);
454 len -= sizeof(sechdr);
456 buf = ntohl(sechdr.data_size);
457 data_size = buf & 0xffff;
460 check ^= seq ^ call->call_id;
463 rxrpc_abort_call("V2C", call, seq, RXKADSEALEDINCON, EPROTO);
467 if (data_size > len) {
468 rxrpc_abort_call("V2L", call, seq, RXKADDATALEN, EPROTO);
472 _leave(" = 0 [dlen=%x]", data_size);
476 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
477 _leave(" = -EPROTO");
481 _leave(" = -ENOMEM");
486 * Verify the security on a received packet or subpacket (if part of a
489 static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
490 unsigned int offset, unsigned int len,
491 rxrpc_seq_t seq, u16 expected_cksum)
493 SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
494 struct rxrpc_crypt iv;
495 struct scatterlist sg;
499 _enter("{%d{%x}},{#%u}",
500 call->debug_id, key_serial(call->conn->params.key), seq);
502 if (!call->conn->cipher)
505 /* continue encrypting from where we left off */
506 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
508 /* validate the security checksum */
509 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
510 x |= seq & 0x3fffffff;
511 call->crypto_buf[0] = htonl(call->call_id);
512 call->crypto_buf[1] = htonl(x);
514 sg_init_one(&sg, call->crypto_buf, 8);
515 skcipher_request_set_tfm(req, call->conn->cipher);
516 skcipher_request_set_callback(req, 0, NULL, NULL);
517 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
518 crypto_skcipher_encrypt(req);
519 skcipher_request_zero(req);
521 y = ntohl(call->crypto_buf[1]);
522 cksum = (y >> 16) & 0xffff;
524 cksum = 1; /* zero checksums are not permitted */
526 if (cksum != expected_cksum) {
527 rxrpc_abort_call("VCK", call, seq, RXKADSEALEDINCON, EPROTO);
528 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
529 _leave(" = -EPROTO [csum failed]");
533 switch (call->conn->params.security_level) {
534 case RXRPC_SECURITY_PLAIN:
536 case RXRPC_SECURITY_AUTH:
537 return rxkad_verify_packet_1(call, skb, offset, len, seq);
538 case RXRPC_SECURITY_ENCRYPT:
539 return rxkad_verify_packet_2(call, skb, offset, len, seq);
546 * Locate the data contained in a packet that was partially encrypted.
548 static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
549 unsigned int *_offset, unsigned int *_len)
551 struct rxkad_level1_hdr sechdr;
553 if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
555 *_offset += sizeof(sechdr);
556 *_len = ntohl(sechdr.data_size) & 0xffff;
560 * Locate the data contained in a packet that was completely encrypted.
562 static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
563 unsigned int *_offset, unsigned int *_len)
565 struct rxkad_level2_hdr sechdr;
567 if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
569 *_offset += sizeof(sechdr);
570 *_len = ntohl(sechdr.data_size) & 0xffff;
574 * Locate the data contained in an already decrypted packet.
576 static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
577 unsigned int *_offset, unsigned int *_len)
579 switch (call->conn->params.security_level) {
580 case RXRPC_SECURITY_AUTH:
581 rxkad_locate_data_1(call, skb, _offset, _len);
583 case RXRPC_SECURITY_ENCRYPT:
584 rxkad_locate_data_2(call, skb, _offset, _len);
594 static int rxkad_issue_challenge(struct rxrpc_connection *conn)
596 struct rxkad_challenge challenge;
597 struct rxrpc_wire_header whdr;
604 _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
606 ret = key_validate(conn->params.key);
610 get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
612 challenge.version = htonl(2);
613 challenge.nonce = htonl(conn->security_nonce);
614 challenge.min_level = htonl(0);
615 challenge.__padding = 0;
617 msg.msg_name = &conn->params.peer->srx.transport.sin;
618 msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin);
619 msg.msg_control = NULL;
620 msg.msg_controllen = 0;
623 whdr.epoch = htonl(conn->proto.epoch);
624 whdr.cid = htonl(conn->proto.cid);
627 whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
628 whdr.flags = conn->out_clientflag;
630 whdr.securityIndex = conn->security_ix;
632 whdr.serviceId = htons(conn->params.service_id);
634 iov[0].iov_base = &whdr;
635 iov[0].iov_len = sizeof(whdr);
636 iov[1].iov_base = &challenge;
637 iov[1].iov_len = sizeof(challenge);
639 len = iov[0].iov_len + iov[1].iov_len;
641 serial = atomic_inc_return(&conn->serial);
642 whdr.serial = htonl(serial);
643 _proto("Tx CHALLENGE %%%u", serial);
645 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
647 _debug("sendmsg failed: %d", ret);
656 * send a Kerberos security response
658 static int rxkad_send_response(struct rxrpc_connection *conn,
659 struct rxrpc_host_header *hdr,
660 struct rxkad_response *resp,
661 const struct rxkad_key *s2)
663 struct rxrpc_wire_header whdr;
672 msg.msg_name = &conn->params.peer->srx.transport.sin;
673 msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin);
674 msg.msg_control = NULL;
675 msg.msg_controllen = 0;
678 memset(&whdr, 0, sizeof(whdr));
679 whdr.epoch = htonl(hdr->epoch);
680 whdr.cid = htonl(hdr->cid);
681 whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
682 whdr.flags = conn->out_clientflag;
683 whdr.securityIndex = hdr->securityIndex;
684 whdr.serviceId = htons(hdr->serviceId);
686 iov[0].iov_base = &whdr;
687 iov[0].iov_len = sizeof(whdr);
688 iov[1].iov_base = resp;
689 iov[1].iov_len = sizeof(*resp);
690 iov[2].iov_base = (void *)s2->ticket;
691 iov[2].iov_len = s2->ticket_len;
693 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
695 serial = atomic_inc_return(&conn->serial);
696 whdr.serial = htonl(serial);
697 _proto("Tx RESPONSE %%%u", serial);
699 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
701 _debug("sendmsg failed: %d", ret);
710 * calculate the response checksum
712 static void rxkad_calc_response_checksum(struct rxkad_response *response)
716 u8 *p = (u8 *) response;
718 for (loop = sizeof(*response); loop > 0; loop--)
719 csum = csum * 0x10204081 + *p++;
721 response->encrypted.checksum = htonl(csum);
725 * encrypt the response packet
727 static void rxkad_encrypt_response(struct rxrpc_connection *conn,
728 struct rxkad_response *resp,
729 const struct rxkad_key *s2)
731 SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
732 struct rxrpc_crypt iv;
733 struct scatterlist sg[1];
735 /* continue encrypting from where we left off */
736 memcpy(&iv, s2->session_key, sizeof(iv));
738 sg_init_table(sg, 1);
739 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
740 skcipher_request_set_tfm(req, conn->cipher);
741 skcipher_request_set_callback(req, 0, NULL, NULL);
742 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
743 crypto_skcipher_encrypt(req);
744 skcipher_request_zero(req);
748 * respond to a challenge packet
750 static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
754 const struct rxrpc_key_token *token;
755 struct rxkad_challenge challenge;
756 struct rxkad_response resp
757 __attribute__((aligned(8))); /* must be aligned for crypto */
758 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
759 u32 version, nonce, min_level, abort_code;
762 _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
764 if (!conn->params.key) {
765 _leave(" = -EPROTO [no key]");
769 ret = key_validate(conn->params.key);
771 *_abort_code = RXKADEXPIRED;
775 abort_code = RXKADPACKETSHORT;
776 if (skb_copy_bits(skb, sp->offset, &challenge, sizeof(challenge)) < 0)
779 version = ntohl(challenge.version);
780 nonce = ntohl(challenge.nonce);
781 min_level = ntohl(challenge.min_level);
783 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
784 sp->hdr.serial, version, nonce, min_level);
786 abort_code = RXKADINCONSISTENCY;
787 if (version != RXKAD_VERSION)
790 abort_code = RXKADLEVELFAIL;
791 if (conn->params.security_level < min_level)
794 token = conn->params.key->payload.data[0];
796 /* build the response packet */
797 memset(&resp, 0, sizeof(resp));
799 resp.version = htonl(RXKAD_VERSION);
800 resp.encrypted.epoch = htonl(conn->proto.epoch);
801 resp.encrypted.cid = htonl(conn->proto.cid);
802 resp.encrypted.securityIndex = htonl(conn->security_ix);
803 resp.encrypted.inc_nonce = htonl(nonce + 1);
804 resp.encrypted.level = htonl(conn->params.security_level);
805 resp.kvno = htonl(token->kad->kvno);
806 resp.ticket_len = htonl(token->kad->ticket_len);
808 resp.encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
809 resp.encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
810 resp.encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
811 resp.encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
813 /* calculate the response checksum and then do the encryption */
814 rxkad_calc_response_checksum(&resp);
815 rxkad_encrypt_response(conn, &resp, token->kad);
816 return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
819 *_abort_code = abort_code;
820 _leave(" = -EPROTO [%d]", abort_code);
825 * decrypt the kerberos IV ticket in the response
827 static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
828 void *ticket, size_t ticket_len,
829 struct rxrpc_crypt *_session_key,
833 struct skcipher_request *req;
834 struct rxrpc_crypt iv, key;
835 struct scatterlist sg[1];
841 u8 *p, *q, *name, *end;
843 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
847 ret = key_validate(conn->server_key);
851 *_abort_code = RXKADEXPIRED;
854 *_abort_code = RXKADNOAUTH;
859 ASSERT(conn->server_key->payload.data[0] != NULL);
860 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
862 memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
864 req = skcipher_request_alloc(conn->server_key->payload.data[0],
867 *_abort_code = RXKADNOAUTH;
872 sg_init_one(&sg[0], ticket, ticket_len);
873 skcipher_request_set_callback(req, 0, NULL, NULL);
874 skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
875 crypto_skcipher_decrypt(req);
876 skcipher_request_free(req);
879 end = p + ticket_len;
884 q = memchr(p, 0, end - p); \
885 if (!q || q - p > (size)) \
894 /* extract the ticket flags */
895 _debug("KIV FLAGS: %x", *p);
896 little_endian = *p & 1;
899 /* extract the authentication name */
901 _debug("KIV ANAME: %s", name);
903 /* extract the principal's instance */
905 _debug("KIV INST : %s", name);
907 /* extract the principal's authentication domain */
909 _debug("KIV REALM: %s", name);
911 if (end - p < 4 + 8 + 4 + 2)
914 /* get the IPv4 address of the entity that requested the ticket */
915 memcpy(&addr, p, sizeof(addr));
917 _debug("KIV ADDR : %pI4", &addr);
919 /* get the session key from the ticket */
920 memcpy(&key, p, sizeof(key));
922 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
923 memcpy(_session_key, &key, sizeof(key));
925 /* get the ticket's lifetime */
926 life = *p++ * 5 * 60;
927 _debug("KIV LIFE : %u", life);
929 /* get the issue time of the ticket */
932 memcpy(&stamp, p, 4);
933 issue = le32_to_cpu(stamp);
936 memcpy(&stamp, p, 4);
937 issue = be32_to_cpu(stamp);
941 _debug("KIV ISSUE: %lx [%lx]", issue, now);
943 /* check the ticket is in date */
945 *_abort_code = RXKADNOAUTH;
950 if (issue < now - life) {
951 *_abort_code = RXKADEXPIRED;
956 *_expiry = issue + life;
958 /* get the service name */
960 _debug("KIV SNAME: %s", name);
962 /* get the service instance name */
964 _debug("KIV SINST: %s", name);
968 _leave(" = %d", ret);
972 *_abort_code = RXKADBADTICKET;
978 * decrypt the response packet
980 static void rxkad_decrypt_response(struct rxrpc_connection *conn,
981 struct rxkad_response *resp,
982 const struct rxrpc_crypt *session_key)
984 SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci);
985 struct scatterlist sg[1];
986 struct rxrpc_crypt iv;
989 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
991 ASSERT(rxkad_ci != NULL);
993 mutex_lock(&rxkad_ci_mutex);
994 if (crypto_skcipher_setkey(rxkad_ci, session_key->x,
995 sizeof(*session_key)) < 0)
998 memcpy(&iv, session_key, sizeof(iv));
1000 sg_init_table(sg, 1);
1001 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
1002 skcipher_request_set_tfm(req, rxkad_ci);
1003 skcipher_request_set_callback(req, 0, NULL, NULL);
1004 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1005 crypto_skcipher_decrypt(req);
1006 skcipher_request_zero(req);
1008 mutex_unlock(&rxkad_ci_mutex);
1016 static int rxkad_verify_response(struct rxrpc_connection *conn,
1017 struct sk_buff *skb,
1020 struct rxkad_response response
1021 __attribute__((aligned(8))); /* must be aligned for crypto */
1022 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1023 struct rxrpc_crypt session_key;
1026 u32 abort_code, version, kvno, ticket_len, level;
1030 _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
1032 abort_code = RXKADPACKETSHORT;
1033 if (skb_copy_bits(skb, sp->offset, &response, sizeof(response)) < 0)
1034 goto protocol_error;
1035 if (!pskb_pull(skb, sizeof(response)))
1038 version = ntohl(response.version);
1039 ticket_len = ntohl(response.ticket_len);
1040 kvno = ntohl(response.kvno);
1041 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1042 sp->hdr.serial, version, kvno, ticket_len);
1044 abort_code = RXKADINCONSISTENCY;
1045 if (version != RXKAD_VERSION)
1046 goto protocol_error;
1048 abort_code = RXKADTICKETLEN;
1049 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1050 goto protocol_error;
1052 abort_code = RXKADUNKNOWNKEY;
1053 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1054 goto protocol_error;
1056 /* extract the kerberos ticket and decrypt and decode it */
1057 ticket = kmalloc(ticket_len, GFP_NOFS);
1061 abort_code = RXKADPACKETSHORT;
1062 if (skb_copy_bits(skb, sp->offset, ticket, ticket_len) < 0)
1063 goto protocol_error_free;
1065 ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
1066 &expiry, &abort_code);
1068 *_abort_code = abort_code;
1073 /* use the session key from inside the ticket to decrypt the
1075 rxkad_decrypt_response(conn, &response, &session_key);
1077 abort_code = RXKADSEALEDINCON;
1078 if (ntohl(response.encrypted.epoch) != conn->proto.epoch)
1079 goto protocol_error_free;
1080 if (ntohl(response.encrypted.cid) != conn->proto.cid)
1081 goto protocol_error_free;
1082 if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
1083 goto protocol_error_free;
1084 csum = response.encrypted.checksum;
1085 response.encrypted.checksum = 0;
1086 rxkad_calc_response_checksum(&response);
1087 if (response.encrypted.checksum != csum)
1088 goto protocol_error_free;
1090 spin_lock(&conn->channel_lock);
1091 for (i = 0; i < RXRPC_MAXCALLS; i++) {
1092 struct rxrpc_call *call;
1093 u32 call_id = ntohl(response.encrypted.call_id[i]);
1095 if (call_id > INT_MAX)
1096 goto protocol_error_unlock;
1098 if (call_id < conn->channels[i].call_counter)
1099 goto protocol_error_unlock;
1100 if (call_id > conn->channels[i].call_counter) {
1101 call = rcu_dereference_protected(
1102 conn->channels[i].call,
1103 lockdep_is_held(&conn->channel_lock));
1104 if (call && call->state < RXRPC_CALL_COMPLETE)
1105 goto protocol_error_unlock;
1106 conn->channels[i].call_counter = call_id;
1109 spin_unlock(&conn->channel_lock);
1111 abort_code = RXKADOUTOFSEQUENCE;
1112 if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1)
1113 goto protocol_error_free;
1115 abort_code = RXKADLEVELFAIL;
1116 level = ntohl(response.encrypted.level);
1117 if (level > RXRPC_SECURITY_ENCRYPT)
1118 goto protocol_error_free;
1119 conn->params.security_level = level;
1121 /* create a key to hold the security data and expiration time - after
1122 * this the connection security can be handled in exactly the same way
1123 * as for a client connection */
1124 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1134 protocol_error_unlock:
1135 spin_unlock(&conn->channel_lock);
1136 protocol_error_free:
1139 *_abort_code = abort_code;
1140 _leave(" = -EPROTO [%d]", abort_code);
1145 * clear the connection security
1147 static void rxkad_clear(struct rxrpc_connection *conn)
1152 crypto_free_skcipher(conn->cipher);
1156 * Initialise the rxkad security service.
1158 static int rxkad_init(void)
1160 /* pin the cipher we need so that the crypto layer doesn't invoke
1161 * keventd to go get it */
1162 rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
1163 return PTR_ERR_OR_ZERO(rxkad_ci);
1167 * Clean up the rxkad security service.
1169 static void rxkad_exit(void)
1172 crypto_free_skcipher(rxkad_ci);
1176 * RxRPC Kerberos-based security
1178 const struct rxrpc_security rxkad = {
1180 .security_index = RXRPC_SECURITY_RXKAD,
1183 .init_connection_security = rxkad_init_connection_security,
1184 .prime_packet_security = rxkad_prime_packet_security,
1185 .secure_packet = rxkad_secure_packet,
1186 .verify_packet = rxkad_verify_packet,
1187 .locate_data = rxkad_locate_data,
1188 .issue_challenge = rxkad_issue_challenge,
1189 .respond_to_challenge = rxkad_respond_to_challenge,
1190 .verify_response = rxkad_verify_response,
1191 .clear = rxkad_clear,