1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kerberos-based RxRPC security
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <crypto/skcipher.h>
11 #include <linux/module.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/udp.h>
15 #include <linux/scatterlist.h>
16 #include <linux/ctype.h>
17 #include <linux/slab.h>
18 #include <linux/key-type.h>
20 #include <net/af_rxrpc.h>
21 #include <keys/rxrpc-type.h>
22 #include "ar-internal.h"
24 #define RXKAD_VERSION 2
25 #define MAXKRB5TICKETLEN 1024
26 #define RXKAD_TKT_TYPE_KERBEROS_V5 256
27 #define ANAME_SZ 40 /* size of authentication name */
28 #define INST_SZ 40 /* size of principal's instance */
29 #define REALM_SZ 40 /* size of principal's auth domain */
30 #define SNAME_SZ 40 /* size of service name */
33 struct rxkad_level1_hdr {
34 __be32 data_size; /* true data size (excluding padding) */
37 struct rxkad_level2_hdr {
38 __be32 data_size; /* true data size (excluding padding) */
39 __be32 checksum; /* decrypted data checksum */
42 static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
43 struct crypto_sync_skcipher *ci);
46 * this holds a pinned cipher so that keventd doesn't get called by the cipher
47 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
50 static struct crypto_sync_skcipher *rxkad_ci;
51 static struct skcipher_request *rxkad_ci_req;
52 static DEFINE_MUTEX(rxkad_ci_mutex);
55 * Parse the information from a server key
57 * The data should be the 8-byte secret key.
59 static int rxkad_preparse_server_key(struct key_preparsed_payload *prep)
61 struct crypto_skcipher *ci;
63 if (prep->datalen != 8)
66 memcpy(&prep->payload.data[2], prep->data, 8);
68 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
70 _leave(" = %ld", PTR_ERR(ci));
74 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
77 prep->payload.data[0] = ci;
82 static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep)
85 if (prep->payload.data[0])
86 crypto_free_skcipher(prep->payload.data[0]);
89 static void rxkad_destroy_server_key(struct key *key)
91 if (key->payload.data[0]) {
92 crypto_free_skcipher(key->payload.data[0]);
93 key->payload.data[0] = NULL;
98 * initialise connection security
100 static int rxkad_init_connection_security(struct rxrpc_connection *conn,
101 struct rxrpc_key_token *token)
103 struct crypto_sync_skcipher *ci;
106 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key));
108 conn->security_ix = token->security_index;
110 ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
117 if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
118 sizeof(token->kad->session_key)) < 0)
121 switch (conn->security_level) {
122 case RXRPC_SECURITY_PLAIN:
123 case RXRPC_SECURITY_AUTH:
124 case RXRPC_SECURITY_ENCRYPT:
131 ret = rxkad_prime_packet_security(conn, ci);
135 conn->rxkad.cipher = ci;
139 crypto_free_sync_skcipher(ci);
141 _leave(" = %d", ret);
146 * Work out how much data we can put in a packet.
148 static int rxkad_how_much_data(struct rxrpc_call *call, size_t remain,
149 size_t *_buf_size, size_t *_data_size, size_t *_offset)
151 size_t shdr, buf_size, chunk;
153 switch (call->conn->security_level) {
155 buf_size = chunk = min_t(size_t, remain, RXRPC_JUMBO_DATALEN);
158 case RXRPC_SECURITY_AUTH:
159 shdr = sizeof(struct rxkad_level1_hdr);
161 case RXRPC_SECURITY_ENCRYPT:
162 shdr = sizeof(struct rxkad_level2_hdr);
166 buf_size = round_down(RXRPC_JUMBO_DATALEN, RXKAD_ALIGN);
168 chunk = buf_size - shdr;
170 buf_size = round_up(shdr + remain, RXKAD_ALIGN);
173 *_buf_size = buf_size;
180 * prime the encryption state with the invariant parts of a connection's
183 static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
184 struct crypto_sync_skcipher *ci)
186 struct skcipher_request *req;
187 struct rxrpc_key_token *token;
188 struct scatterlist sg;
189 struct rxrpc_crypt iv;
191 size_t tmpsize = 4 * sizeof(__be32);
198 tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
202 req = skcipher_request_alloc(&ci->base, GFP_NOFS);
208 token = conn->key->payload.data[0];
209 memcpy(&iv, token->kad->session_key, sizeof(iv));
211 tmpbuf[0] = htonl(conn->proto.epoch);
212 tmpbuf[1] = htonl(conn->proto.cid);
214 tmpbuf[3] = htonl(conn->security_ix);
216 sg_init_one(&sg, tmpbuf, tmpsize);
217 skcipher_request_set_sync_tfm(req, ci);
218 skcipher_request_set_callback(req, 0, NULL, NULL);
219 skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
220 crypto_skcipher_encrypt(req);
221 skcipher_request_free(req);
223 memcpy(&conn->rxkad.csum_iv, tmpbuf + 2, sizeof(conn->rxkad.csum_iv));
230 * Allocate and prepare the crypto request on a call. For any particular call,
231 * this is called serially for the packets, so no lock should be necessary.
233 static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
235 struct crypto_skcipher *tfm = &call->conn->rxkad.cipher->base;
237 return skcipher_request_alloc(tfm, GFP_NOFS);
241 * Clean up the crypto on a call.
243 static void rxkad_free_call_crypto(struct rxrpc_call *call)
248 * partially encrypt a packet (level 1 security)
250 static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
251 struct rxrpc_txbuf *txb,
252 struct skcipher_request *req)
254 struct rxkad_level1_hdr *hdr = (void *)txb->data;
255 struct rxrpc_crypt iv;
256 struct scatterlist sg;
262 check = txb->seq ^ ntohl(txb->wire.callNumber);
263 hdr->data_size = htonl((u32)check << 16 | txb->len);
265 txb->len += sizeof(struct rxkad_level1_hdr);
267 pad = RXKAD_ALIGN - pad;
268 pad &= RXKAD_ALIGN - 1;
270 memset(txb->data + txb->offset, 0, pad);
274 /* start the encryption afresh */
275 memset(&iv, 0, sizeof(iv));
277 sg_init_one(&sg, txb->data, 8);
278 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
279 skcipher_request_set_callback(req, 0, NULL, NULL);
280 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
281 crypto_skcipher_encrypt(req);
282 skcipher_request_zero(req);
289 * wholly encrypt a packet (level 2 security)
291 static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
292 struct rxrpc_txbuf *txb,
293 struct skcipher_request *req)
295 const struct rxrpc_key_token *token;
296 struct rxkad_level2_hdr *rxkhdr = (void *)txb->data;
297 struct rxrpc_crypt iv;
298 struct scatterlist sg;
305 check = txb->seq ^ ntohl(txb->wire.callNumber);
307 rxkhdr->data_size = htonl(txb->len | (u32)check << 16);
308 rxkhdr->checksum = 0;
310 txb->len += sizeof(struct rxkad_level2_hdr);
312 pad = RXKAD_ALIGN - pad;
313 pad &= RXKAD_ALIGN - 1;
315 memset(txb->data + txb->offset, 0, pad);
319 /* encrypt from the session key */
320 token = call->conn->key->payload.data[0];
321 memcpy(&iv, token->kad->session_key, sizeof(iv));
323 sg_init_one(&sg, txb->data, txb->len);
324 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
325 skcipher_request_set_callback(req, 0, NULL, NULL);
326 skcipher_request_set_crypt(req, &sg, &sg, txb->len, iv.x);
327 ret = crypto_skcipher_encrypt(req);
328 skcipher_request_zero(req);
333 * checksum an RxRPC packet header
335 static int rxkad_secure_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
337 struct skcipher_request *req;
338 struct rxrpc_crypt iv;
339 struct scatterlist sg;
342 } crypto __aligned(8);
346 _enter("{%d{%x}},{#%u},%u,",
347 call->debug_id, key_serial(call->conn->key),
350 if (!call->conn->rxkad.cipher)
353 ret = key_validate(call->conn->key);
357 req = rxkad_get_call_crypto(call);
361 /* continue encrypting from where we left off */
362 memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
364 /* calculate the security checksum */
365 x = (ntohl(txb->wire.cid) & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
366 x |= txb->seq & 0x3fffffff;
367 crypto.buf[0] = txb->wire.callNumber;
368 crypto.buf[1] = htonl(x);
370 sg_init_one(&sg, crypto.buf, 8);
371 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
372 skcipher_request_set_callback(req, 0, NULL, NULL);
373 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
374 crypto_skcipher_encrypt(req);
375 skcipher_request_zero(req);
377 y = ntohl(crypto.buf[1]);
378 y = (y >> 16) & 0xffff;
380 y = 1; /* zero checksums are not permitted */
381 txb->wire.cksum = htons(y);
383 switch (call->conn->security_level) {
384 case RXRPC_SECURITY_PLAIN:
387 case RXRPC_SECURITY_AUTH:
388 ret = rxkad_secure_packet_auth(call, txb, req);
390 case RXRPC_SECURITY_ENCRYPT:
391 ret = rxkad_secure_packet_encrypt(call, txb, req);
398 skcipher_request_free(req);
399 _leave(" = %d [set %x]", ret, y);
404 * decrypt partial encryption on a packet (level 1 security)
406 static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
408 struct skcipher_request *req)
410 struct rxkad_level1_hdr sechdr;
411 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
412 struct rxrpc_crypt iv;
413 struct scatterlist sg[16];
422 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
427 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
428 * directly into the target buffer.
430 sg_init_table(sg, ARRAY_SIZE(sg));
431 ret = skb_to_sgvec(skb, sg, sp->offset, 8);
432 if (unlikely(ret < 0))
435 /* start the decryption afresh */
436 memset(&iv, 0, sizeof(iv));
438 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
439 skcipher_request_set_callback(req, 0, NULL, NULL);
440 skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
441 crypto_skcipher_decrypt(req);
442 skcipher_request_zero(req);
444 /* Extract the decrypted packet length */
445 if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) {
446 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
450 sp->offset += sizeof(sechdr);
451 sp->len -= sizeof(sechdr);
453 buf = ntohl(sechdr.data_size);
454 data_size = buf & 0xffff;
457 check ^= seq ^ call->call_id;
460 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
465 if (data_size > sp->len) {
466 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
472 _leave(" = 0 [dlen=%x]", data_size);
477 rxrpc_send_abort_packet(call);
482 * wholly decrypt a packet (level 2 security)
484 static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
486 struct skcipher_request *req)
488 const struct rxrpc_key_token *token;
489 struct rxkad_level2_hdr sechdr;
490 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
491 struct rxrpc_crypt iv;
492 struct scatterlist _sg[4], *sg;
498 _enter(",{%d}", sp->len);
501 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
506 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
507 * directly into the target buffer.
510 nsg = skb_shinfo(skb)->nr_frags + 1;
514 sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
519 sg_init_table(sg, nsg);
520 ret = skb_to_sgvec(skb, sg, sp->offset, sp->len);
521 if (unlikely(ret < 0)) {
527 /* decrypt from the session key */
528 token = call->conn->key->payload.data[0];
529 memcpy(&iv, token->kad->session_key, sizeof(iv));
531 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
532 skcipher_request_set_callback(req, 0, NULL, NULL);
533 skcipher_request_set_crypt(req, sg, sg, sp->len, iv.x);
534 crypto_skcipher_decrypt(req);
535 skcipher_request_zero(req);
539 /* Extract the decrypted packet length */
540 if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) {
541 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
545 sp->offset += sizeof(sechdr);
546 sp->len -= sizeof(sechdr);
548 buf = ntohl(sechdr.data_size);
549 data_size = buf & 0xffff;
552 check ^= seq ^ call->call_id;
555 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
560 if (data_size > sp->len) {
561 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
567 _leave(" = 0 [dlen=%x]", data_size);
572 rxrpc_send_abort_packet(call);
576 _leave(" = -ENOMEM");
581 * Verify the security on a received packet and the subpackets therein.
583 static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb)
585 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
586 struct skcipher_request *req;
587 struct rxrpc_crypt iv;
588 struct scatterlist sg;
591 } crypto __aligned(8);
592 rxrpc_seq_t seq = sp->hdr.seq;
598 _enter("{%d{%x}},{#%u}",
599 call->debug_id, key_serial(call->conn->key), seq);
601 if (!call->conn->rxkad.cipher)
604 req = rxkad_get_call_crypto(call);
608 /* continue encrypting from where we left off */
609 memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
611 /* validate the security checksum */
612 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
613 x |= seq & 0x3fffffff;
614 crypto.buf[0] = htonl(call->call_id);
615 crypto.buf[1] = htonl(x);
617 sg_init_one(&sg, crypto.buf, 8);
618 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
619 skcipher_request_set_callback(req, 0, NULL, NULL);
620 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
621 crypto_skcipher_encrypt(req);
622 skcipher_request_zero(req);
624 y = ntohl(crypto.buf[1]);
625 cksum = (y >> 16) & 0xffff;
627 cksum = 1; /* zero checksums are not permitted */
629 if (cksum != sp->hdr.cksum) {
630 aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
635 switch (call->conn->security_level) {
636 case RXRPC_SECURITY_PLAIN:
639 case RXRPC_SECURITY_AUTH:
640 ret = rxkad_verify_packet_1(call, skb, seq, req);
642 case RXRPC_SECURITY_ENCRYPT:
643 ret = rxkad_verify_packet_2(call, skb, seq, req);
650 skcipher_request_free(req);
655 rxrpc_send_abort_packet(call);
662 static int rxkad_issue_challenge(struct rxrpc_connection *conn)
664 struct rxkad_challenge challenge;
665 struct rxrpc_wire_header whdr;
672 _enter("{%d}", conn->debug_id);
674 get_random_bytes(&conn->rxkad.nonce, sizeof(conn->rxkad.nonce));
676 challenge.version = htonl(2);
677 challenge.nonce = htonl(conn->rxkad.nonce);
678 challenge.min_level = htonl(0);
679 challenge.__padding = 0;
681 msg.msg_name = &conn->peer->srx.transport;
682 msg.msg_namelen = conn->peer->srx.transport_len;
683 msg.msg_control = NULL;
684 msg.msg_controllen = 0;
687 whdr.epoch = htonl(conn->proto.epoch);
688 whdr.cid = htonl(conn->proto.cid);
691 whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
692 whdr.flags = conn->out_clientflag;
694 whdr.securityIndex = conn->security_ix;
696 whdr.serviceId = htons(conn->service_id);
698 iov[0].iov_base = &whdr;
699 iov[0].iov_len = sizeof(whdr);
700 iov[1].iov_base = &challenge;
701 iov[1].iov_len = sizeof(challenge);
703 len = iov[0].iov_len + iov[1].iov_len;
705 serial = atomic_inc_return(&conn->serial);
706 whdr.serial = htonl(serial);
708 ret = kernel_sendmsg(conn->local->socket, &msg, iov, 2, len);
710 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
711 rxrpc_tx_point_rxkad_challenge);
715 conn->peer->last_tx_at = ktime_get_seconds();
716 trace_rxrpc_tx_packet(conn->debug_id, &whdr,
717 rxrpc_tx_point_rxkad_challenge);
723 * send a Kerberos security response
725 static int rxkad_send_response(struct rxrpc_connection *conn,
726 struct rxrpc_host_header *hdr,
727 struct rxkad_response *resp,
728 const struct rxkad_key *s2)
730 struct rxrpc_wire_header whdr;
739 msg.msg_name = &conn->peer->srx.transport;
740 msg.msg_namelen = conn->peer->srx.transport_len;
741 msg.msg_control = NULL;
742 msg.msg_controllen = 0;
745 memset(&whdr, 0, sizeof(whdr));
746 whdr.epoch = htonl(hdr->epoch);
747 whdr.cid = htonl(hdr->cid);
748 whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
749 whdr.flags = conn->out_clientflag;
750 whdr.securityIndex = hdr->securityIndex;
751 whdr.serviceId = htons(hdr->serviceId);
753 iov[0].iov_base = &whdr;
754 iov[0].iov_len = sizeof(whdr);
755 iov[1].iov_base = resp;
756 iov[1].iov_len = sizeof(*resp);
757 iov[2].iov_base = (void *)s2->ticket;
758 iov[2].iov_len = s2->ticket_len;
760 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
762 serial = atomic_inc_return(&conn->serial);
763 whdr.serial = htonl(serial);
765 ret = kernel_sendmsg(conn->local->socket, &msg, iov, 3, len);
767 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
768 rxrpc_tx_point_rxkad_response);
772 conn->peer->last_tx_at = ktime_get_seconds();
778 * calculate the response checksum
780 static void rxkad_calc_response_checksum(struct rxkad_response *response)
784 u8 *p = (u8 *) response;
786 for (loop = sizeof(*response); loop > 0; loop--)
787 csum = csum * 0x10204081 + *p++;
789 response->encrypted.checksum = htonl(csum);
793 * encrypt the response packet
795 static int rxkad_encrypt_response(struct rxrpc_connection *conn,
796 struct rxkad_response *resp,
797 const struct rxkad_key *s2)
799 struct skcipher_request *req;
800 struct rxrpc_crypt iv;
801 struct scatterlist sg[1];
803 req = skcipher_request_alloc(&conn->rxkad.cipher->base, GFP_NOFS);
807 /* continue encrypting from where we left off */
808 memcpy(&iv, s2->session_key, sizeof(iv));
810 sg_init_table(sg, 1);
811 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
812 skcipher_request_set_sync_tfm(req, conn->rxkad.cipher);
813 skcipher_request_set_callback(req, 0, NULL, NULL);
814 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
815 crypto_skcipher_encrypt(req);
816 skcipher_request_free(req);
821 * respond to a challenge packet
823 static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
827 const struct rxrpc_key_token *token;
828 struct rxkad_challenge challenge;
829 struct rxkad_response *resp;
830 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
832 u32 version, nonce, min_level, abort_code;
835 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
837 eproto = tracepoint_string("chall_no_key");
838 abort_code = RX_PROTOCOL_ERROR;
842 abort_code = RXKADEXPIRED;
843 ret = key_validate(conn->key);
847 eproto = tracepoint_string("chall_short");
848 abort_code = RXKADPACKETSHORT;
849 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
850 &challenge, sizeof(challenge)) < 0)
853 version = ntohl(challenge.version);
854 nonce = ntohl(challenge.nonce);
855 min_level = ntohl(challenge.min_level);
857 trace_rxrpc_rx_challenge(conn, sp->hdr.serial, version, nonce, min_level);
859 eproto = tracepoint_string("chall_ver");
860 abort_code = RXKADINCONSISTENCY;
861 if (version != RXKAD_VERSION)
864 abort_code = RXKADLEVELFAIL;
866 if (conn->security_level < min_level)
869 token = conn->key->payload.data[0];
871 /* build the response packet */
872 resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
876 resp->version = htonl(RXKAD_VERSION);
877 resp->encrypted.epoch = htonl(conn->proto.epoch);
878 resp->encrypted.cid = htonl(conn->proto.cid);
879 resp->encrypted.securityIndex = htonl(conn->security_ix);
880 resp->encrypted.inc_nonce = htonl(nonce + 1);
881 resp->encrypted.level = htonl(conn->security_level);
882 resp->kvno = htonl(token->kad->kvno);
883 resp->ticket_len = htonl(token->kad->ticket_len);
884 resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
885 resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
886 resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
887 resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
889 /* calculate the response checksum and then do the encryption */
890 rxkad_calc_response_checksum(resp);
891 ret = rxkad_encrypt_response(conn, resp, token->kad);
893 ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
898 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
901 *_abort_code = abort_code;
906 * decrypt the kerberos IV ticket in the response
908 static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
909 struct key *server_key,
911 void *ticket, size_t ticket_len,
912 struct rxrpc_crypt *_session_key,
916 struct skcipher_request *req;
917 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
918 struct rxrpc_crypt iv, key;
919 struct scatterlist sg[1];
927 u8 *p, *q, *name, *end;
929 _enter("{%d},{%x}", conn->debug_id, key_serial(server_key));
933 ASSERT(server_key->payload.data[0] != NULL);
934 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
936 memcpy(&iv, &server_key->payload.data[2], sizeof(iv));
939 req = skcipher_request_alloc(server_key->payload.data[0], GFP_NOFS);
941 goto temporary_error;
943 sg_init_one(&sg[0], ticket, ticket_len);
944 skcipher_request_set_callback(req, 0, NULL, NULL);
945 skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
946 crypto_skcipher_decrypt(req);
947 skcipher_request_free(req);
950 end = p + ticket_len;
955 eproto = tracepoint_string("rxkad_bad_"#field); \
956 q = memchr(p, 0, end - p); \
957 if (!q || q - p > (field##_SZ)) \
966 /* extract the ticket flags */
967 _debug("KIV FLAGS: %x", *p);
968 little_endian = *p & 1;
971 /* extract the authentication name */
973 _debug("KIV ANAME: %s", name);
975 /* extract the principal's instance */
977 _debug("KIV INST : %s", name);
979 /* extract the principal's authentication domain */
981 _debug("KIV REALM: %s", name);
983 eproto = tracepoint_string("rxkad_bad_len");
984 if (end - p < 4 + 8 + 4 + 2)
987 /* get the IPv4 address of the entity that requested the ticket */
988 memcpy(&addr, p, sizeof(addr));
990 _debug("KIV ADDR : %pI4", &addr);
992 /* get the session key from the ticket */
993 memcpy(&key, p, sizeof(key));
995 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
996 memcpy(_session_key, &key, sizeof(key));
998 /* get the ticket's lifetime */
999 life = *p++ * 5 * 60;
1000 _debug("KIV LIFE : %u", life);
1002 /* get the issue time of the ticket */
1003 if (little_endian) {
1005 memcpy(&stamp, p, 4);
1006 issue = rxrpc_u32_to_time64(le32_to_cpu(stamp));
1009 memcpy(&stamp, p, 4);
1010 issue = rxrpc_u32_to_time64(be32_to_cpu(stamp));
1013 now = ktime_get_real_seconds();
1014 _debug("KIV ISSUE: %llx [%llx]", issue, now);
1016 /* check the ticket is in date */
1018 abort_code = RXKADNOAUTH;
1019 ret = -EKEYREJECTED;
1023 if (issue < now - life) {
1024 abort_code = RXKADEXPIRED;
1029 *_expiry = issue + life;
1031 /* get the service name */
1033 _debug("KIV SNAME: %s", name);
1035 /* get the service instance name */
1037 _debug("KIV SINST: %s", name);
1041 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
1042 abort_code = RXKADBADTICKET;
1045 *_abort_code = abort_code;
1052 * decrypt the response packet
1054 static void rxkad_decrypt_response(struct rxrpc_connection *conn,
1055 struct rxkad_response *resp,
1056 const struct rxrpc_crypt *session_key)
1058 struct skcipher_request *req = rxkad_ci_req;
1059 struct scatterlist sg[1];
1060 struct rxrpc_crypt iv;
1062 _enter(",,%08x%08x",
1063 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
1065 mutex_lock(&rxkad_ci_mutex);
1066 if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
1067 sizeof(*session_key)) < 0)
1070 memcpy(&iv, session_key, sizeof(iv));
1072 sg_init_table(sg, 1);
1073 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
1074 skcipher_request_set_sync_tfm(req, rxkad_ci);
1075 skcipher_request_set_callback(req, 0, NULL, NULL);
1076 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1077 crypto_skcipher_decrypt(req);
1078 skcipher_request_zero(req);
1080 mutex_unlock(&rxkad_ci_mutex);
1088 static int rxkad_verify_response(struct rxrpc_connection *conn,
1089 struct sk_buff *skb,
1092 struct rxkad_response *response;
1093 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1094 struct rxrpc_crypt session_key;
1095 struct key *server_key;
1099 u32 abort_code, version, kvno, ticket_len, level;
1103 _enter("{%d}", conn->debug_id);
1105 server_key = rxrpc_look_up_server_security(conn, skb, 0, 0);
1106 if (IS_ERR(server_key)) {
1107 switch (PTR_ERR(server_key)) {
1109 abort_code = RXKADUNKNOWNKEY;
1112 abort_code = RXKADEXPIRED;
1115 abort_code = RXKADNOAUTH;
1118 trace_rxrpc_abort(0, "SVK",
1119 sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1120 abort_code, PTR_ERR(server_key));
1121 *_abort_code = abort_code;
1126 response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
1128 goto temporary_error;
1130 eproto = tracepoint_string("rxkad_rsp_short");
1131 abort_code = RXKADPACKETSHORT;
1132 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1133 response, sizeof(*response)) < 0)
1134 goto protocol_error;
1136 version = ntohl(response->version);
1137 ticket_len = ntohl(response->ticket_len);
1138 kvno = ntohl(response->kvno);
1140 trace_rxrpc_rx_response(conn, sp->hdr.serial, version, kvno, ticket_len);
1142 eproto = tracepoint_string("rxkad_rsp_ver");
1143 abort_code = RXKADINCONSISTENCY;
1144 if (version != RXKAD_VERSION)
1145 goto protocol_error;
1147 eproto = tracepoint_string("rxkad_rsp_tktlen");
1148 abort_code = RXKADTICKETLEN;
1149 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1150 goto protocol_error;
1152 eproto = tracepoint_string("rxkad_rsp_unkkey");
1153 abort_code = RXKADUNKNOWNKEY;
1154 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1155 goto protocol_error;
1157 /* extract the kerberos ticket and decrypt and decode it */
1159 ticket = kmalloc(ticket_len, GFP_NOFS);
1161 goto temporary_error_free_resp;
1163 eproto = tracepoint_string("rxkad_tkt_short");
1164 abort_code = RXKADPACKETSHORT;
1165 ret = skb_copy_bits(skb, sizeof(struct rxrpc_wire_header) + sizeof(*response),
1166 ticket, ticket_len);
1168 goto temporary_error_free_ticket;
1170 ret = rxkad_decrypt_ticket(conn, server_key, skb, ticket, ticket_len,
1171 &session_key, &expiry, _abort_code);
1173 goto temporary_error_free_ticket;
1175 /* use the session key from inside the ticket to decrypt the
1177 rxkad_decrypt_response(conn, response, &session_key);
1179 eproto = tracepoint_string("rxkad_rsp_param");
1180 abort_code = RXKADSEALEDINCON;
1181 if (ntohl(response->encrypted.epoch) != conn->proto.epoch)
1182 goto protocol_error_free;
1183 if (ntohl(response->encrypted.cid) != conn->proto.cid)
1184 goto protocol_error_free;
1185 if (ntohl(response->encrypted.securityIndex) != conn->security_ix)
1186 goto protocol_error_free;
1187 csum = response->encrypted.checksum;
1188 response->encrypted.checksum = 0;
1189 rxkad_calc_response_checksum(response);
1190 eproto = tracepoint_string("rxkad_rsp_csum");
1191 if (response->encrypted.checksum != csum)
1192 goto protocol_error_free;
1194 spin_lock(&conn->bundle->channel_lock);
1195 for (i = 0; i < RXRPC_MAXCALLS; i++) {
1196 struct rxrpc_call *call;
1197 u32 call_id = ntohl(response->encrypted.call_id[i]);
1199 eproto = tracepoint_string("rxkad_rsp_callid");
1200 if (call_id > INT_MAX)
1201 goto protocol_error_unlock;
1203 eproto = tracepoint_string("rxkad_rsp_callctr");
1204 if (call_id < conn->channels[i].call_counter)
1205 goto protocol_error_unlock;
1207 eproto = tracepoint_string("rxkad_rsp_callst");
1208 if (call_id > conn->channels[i].call_counter) {
1209 call = rcu_dereference_protected(
1210 conn->channels[i].call,
1211 lockdep_is_held(&conn->bundle->channel_lock));
1212 if (call && call->state < RXRPC_CALL_COMPLETE)
1213 goto protocol_error_unlock;
1214 conn->channels[i].call_counter = call_id;
1217 spin_unlock(&conn->bundle->channel_lock);
1219 eproto = tracepoint_string("rxkad_rsp_seq");
1220 abort_code = RXKADOUTOFSEQUENCE;
1221 if (ntohl(response->encrypted.inc_nonce) != conn->rxkad.nonce + 1)
1222 goto protocol_error_free;
1224 eproto = tracepoint_string("rxkad_rsp_level");
1225 abort_code = RXKADLEVELFAIL;
1226 level = ntohl(response->encrypted.level);
1227 if (level > RXRPC_SECURITY_ENCRYPT)
1228 goto protocol_error_free;
1229 conn->security_level = level;
1231 /* create a key to hold the security data and expiration time - after
1232 * this the connection security can be handled in exactly the same way
1233 * as for a client connection */
1234 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1236 goto temporary_error_free_ticket;
1243 protocol_error_unlock:
1244 spin_unlock(&conn->bundle->channel_lock);
1245 protocol_error_free:
1249 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
1250 key_put(server_key);
1251 *_abort_code = abort_code;
1254 temporary_error_free_ticket:
1256 temporary_error_free_resp:
1259 /* Ignore the response packet if we got a temporary error such as
1260 * ENOMEM. We just want to send the challenge again. Note that we
1261 * also come out this way if the ticket decryption fails.
1263 key_put(server_key);
1268 * clear the connection security
1270 static void rxkad_clear(struct rxrpc_connection *conn)
1274 if (conn->rxkad.cipher)
1275 crypto_free_sync_skcipher(conn->rxkad.cipher);
1279 * Initialise the rxkad security service.
1281 static int rxkad_init(void)
1283 struct crypto_sync_skcipher *tfm;
1284 struct skcipher_request *req;
1286 /* pin the cipher we need so that the crypto layer doesn't invoke
1287 * keventd to go get it */
1288 tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
1290 return PTR_ERR(tfm);
1292 req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
1301 crypto_free_sync_skcipher(tfm);
1306 * Clean up the rxkad security service.
1308 static void rxkad_exit(void)
1310 crypto_free_sync_skcipher(rxkad_ci);
1311 skcipher_request_free(rxkad_ci_req);
1315 * RxRPC Kerberos-based security
1317 const struct rxrpc_security rxkad = {
1319 .security_index = RXRPC_SECURITY_RXKAD,
1320 .no_key_abort = RXKADUNKNOWNKEY,
1323 .preparse_server_key = rxkad_preparse_server_key,
1324 .free_preparse_server_key = rxkad_free_preparse_server_key,
1325 .destroy_server_key = rxkad_destroy_server_key,
1326 .init_connection_security = rxkad_init_connection_security,
1327 .how_much_data = rxkad_how_much_data,
1328 .secure_packet = rxkad_secure_packet,
1329 .verify_packet = rxkad_verify_packet,
1330 .free_call_crypto = rxkad_free_call_crypto,
1331 .issue_challenge = rxkad_issue_challenge,
1332 .respond_to_challenge = rxkad_respond_to_challenge,
1333 .verify_response = rxkad_verify_response,
1334 .clear = rxkad_clear,