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 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/udp.h>
16 #include <linux/crypto.h>
17 #include <linux/scatterlist.h>
18 #include <linux/ctype.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
23 #define RXKAD_VERSION 2
24 #define MAXKRB5TICKETLEN 1024
25 #define RXKAD_TKT_TYPE_KERBEROS_V5 256
26 #define ANAME_SZ 40 /* size of authentication name */
27 #define INST_SZ 40 /* size of principal's instance */
28 #define REALM_SZ 40 /* size of principal's auth domain */
29 #define SNAME_SZ 40 /* size of service name */
32 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
33 MODULE_PARM_DESC(rxrpc_debug, "rxkad debugging mask");
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 */
44 MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos)");
45 MODULE_AUTHOR("Red Hat, Inc.");
46 MODULE_LICENSE("GPL");
49 * this holds a pinned cipher so that keventd doesn't get called by the cipher
50 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
53 static struct crypto_blkcipher *rxkad_ci;
54 static DEFINE_MUTEX(rxkad_ci_mutex);
57 * initialise connection security
59 static int rxkad_init_connection_security(struct rxrpc_connection *conn)
61 struct rxrpc_key_payload *payload;
62 struct crypto_blkcipher *ci;
65 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key));
67 payload = conn->key->payload.data;
68 conn->security_ix = payload->k.security_index;
70 ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
77 if (crypto_blkcipher_setkey(ci, payload->k.session_key,
78 sizeof(payload->k.session_key)) < 0)
81 switch (conn->security_level) {
82 case RXRPC_SECURITY_PLAIN:
84 case RXRPC_SECURITY_AUTH:
86 conn->security_size = sizeof(struct rxkad_level1_hdr);
87 conn->header_size += sizeof(struct rxkad_level1_hdr);
89 case RXRPC_SECURITY_ENCRYPT:
91 conn->security_size = sizeof(struct rxkad_level2_hdr);
92 conn->header_size += sizeof(struct rxkad_level2_hdr);
102 _leave(" = %d", ret);
107 * prime the encryption state with the invariant parts of a connection's
110 static void rxkad_prime_packet_security(struct rxrpc_connection *conn)
112 struct rxrpc_key_payload *payload;
113 struct blkcipher_desc desc;
114 struct scatterlist sg[2];
115 struct rxrpc_crypt iv;
118 } tmpbuf __attribute__((aligned(16))); /* must all be in same page */
125 payload = conn->key->payload.data;
126 memcpy(&iv, payload->k.session_key, sizeof(iv));
128 desc.tfm = conn->cipher;
132 tmpbuf.x[0] = conn->epoch;
133 tmpbuf.x[1] = conn->cid;
135 tmpbuf.x[3] = htonl(conn->security_ix);
137 memset(sg, 0, sizeof(sg));
138 sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
139 sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
140 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
142 memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
143 ASSERTCMP(conn->csum_iv.n[0], ==, tmpbuf.x[2]);
149 * partially encrypt a packet (level 1 security)
151 static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
156 struct rxrpc_skb_priv *sp;
157 struct blkcipher_desc desc;
158 struct rxrpc_crypt iv;
159 struct scatterlist sg[2];
161 struct rxkad_level1_hdr hdr;
162 __be32 first; /* first four bytes of data and padding */
163 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
170 check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
171 data_size |= (u32) check << 16;
173 tmpbuf.hdr.data_size = htonl(data_size);
174 memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first));
176 /* start the encryption afresh */
177 memset(&iv, 0, sizeof(iv));
178 desc.tfm = call->conn->cipher;
182 memset(sg, 0, sizeof(sg));
183 sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
184 sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
185 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
187 memcpy(sechdr, &tmpbuf, sizeof(tmpbuf));
194 * wholly encrypt a packet (level 2 security)
196 static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
201 const struct rxrpc_key_payload *payload;
202 struct rxkad_level2_hdr rxkhdr
203 __attribute__((aligned(8))); /* must be all on one page */
204 struct rxrpc_skb_priv *sp;
205 struct blkcipher_desc desc;
206 struct rxrpc_crypt iv;
207 struct scatterlist sg[16];
208 struct sk_buff *trailer;
217 check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
219 rxkhdr.data_size = htonl(data_size | (u32) check << 16);
222 /* encrypt from the session key */
223 payload = call->conn->key->payload.data;
224 memcpy(&iv, payload->k.session_key, sizeof(iv));
225 desc.tfm = call->conn->cipher;
229 memset(sg, 0, sizeof(sg[0]) * 2);
230 sg_set_buf(&sg[0], sechdr, sizeof(rxkhdr));
231 sg_set_buf(&sg[1], &rxkhdr, sizeof(rxkhdr));
232 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr));
234 /* we want to encrypt the skbuff in-place */
235 nsg = skb_cow_data(skb, 0, &trailer);
236 if (nsg < 0 || nsg > 16)
239 len = data_size + call->conn->size_align - 1;
240 len &= ~(call->conn->size_align - 1);
242 skb_to_sgvec(skb, sg, 0, len);
243 crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
250 * checksum an RxRPC packet header
252 static int rxkad_secure_packet(const struct rxrpc_call *call,
257 struct rxrpc_skb_priv *sp;
258 struct blkcipher_desc desc;
259 struct rxrpc_crypt iv;
260 struct scatterlist sg[2];
263 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
269 _enter("{%d{%x}},{#%u},%zu,",
270 call->debug_id, key_serial(call->conn->key), ntohl(sp->hdr.seq),
273 if (!call->conn->cipher)
276 ret = key_validate(call->conn->key);
280 /* continue encrypting from where we left off */
281 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
282 desc.tfm = call->conn->cipher;
286 /* calculate the security checksum */
287 x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
288 x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff);
289 tmpbuf.x[0] = sp->hdr.callNumber;
292 memset(&sg, 0, sizeof(sg));
293 sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
294 sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
295 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
297 x = ntohl(tmpbuf.x[1]);
298 x = (x >> 16) & 0xffff;
300 x = 1; /* zero checksums are not permitted */
301 sp->hdr.cksum = htons(x);
303 switch (call->conn->security_level) {
304 case RXRPC_SECURITY_PLAIN:
307 case RXRPC_SECURITY_AUTH:
308 ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
310 case RXRPC_SECURITY_ENCRYPT:
311 ret = rxkad_secure_packet_encrypt(call, skb, data_size,
319 _leave(" = %d [set %hx]", ret, x);
324 * decrypt partial encryption on a packet (level 1 security)
326 static int rxkad_verify_packet_auth(const struct rxrpc_call *call,
330 struct rxkad_level1_hdr sechdr;
331 struct rxrpc_skb_priv *sp;
332 struct blkcipher_desc desc;
333 struct rxrpc_crypt iv;
334 struct scatterlist sg[2];
335 struct sk_buff *trailer;
343 /* we want to decrypt the skbuff in-place */
344 if (skb_cow_data(skb, 0, &trailer) < 0)
347 skb_to_sgvec(skb, sg, 0, 8);
349 /* start the decryption afresh */
350 memset(&iv, 0, sizeof(iv));
351 desc.tfm = call->conn->cipher;
355 crypto_blkcipher_decrypt_iv(&desc, sg, sg, 8);
357 /* remove the decrypted packet length */
358 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
360 if (!skb_pull(skb, sizeof(sechdr)))
363 buf = ntohl(sechdr.data_size);
364 data_size = buf & 0xffff;
367 check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
370 *_abort_code = RXKADSEALEDINCON;
374 /* shorten the packet to remove the padding */
375 if (data_size > skb->len)
377 else if (data_size < skb->len)
378 skb->len = data_size;
380 _leave(" = 0 [dlen=%x]", data_size);
384 *_abort_code = RXKADDATALEN;
386 _leave(" = -EPROTO");
390 _leave(" = -ENOMEM");
395 * wholly decrypt a packet (level 2 security)
397 static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call,
401 const struct rxrpc_key_payload *payload;
402 struct rxkad_level2_hdr sechdr;
403 struct rxrpc_skb_priv *sp;
404 struct blkcipher_desc desc;
405 struct rxrpc_crypt iv;
406 struct scatterlist _sg[4], *sg;
407 struct sk_buff *trailer;
412 _enter(",{%d}", skb->len);
416 /* we want to decrypt the skbuff in-place */
417 nsg = skb_cow_data(skb, 0, &trailer);
422 if (unlikely(nsg > 4)) {
423 sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
428 skb_to_sgvec(skb, sg, 0, skb->len);
430 /* decrypt from the session key */
431 payload = call->conn->key->payload.data;
432 memcpy(&iv, payload->k.session_key, sizeof(iv));
433 desc.tfm = call->conn->cipher;
437 crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len);
441 /* remove the decrypted packet length */
442 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
444 if (!skb_pull(skb, sizeof(sechdr)))
447 buf = ntohl(sechdr.data_size);
448 data_size = buf & 0xffff;
451 check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
454 *_abort_code = RXKADSEALEDINCON;
458 /* shorten the packet to remove the padding */
459 if (data_size > skb->len)
461 else if (data_size < skb->len)
462 skb->len = data_size;
464 _leave(" = 0 [dlen=%x]", data_size);
468 *_abort_code = RXKADDATALEN;
470 _leave(" = -EPROTO");
474 _leave(" = -ENOMEM");
479 * verify the security on a received packet
481 static int rxkad_verify_packet(const struct rxrpc_call *call,
485 struct blkcipher_desc desc;
486 struct rxrpc_skb_priv *sp;
487 struct rxrpc_crypt iv;
488 struct scatterlist sg[2];
491 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
498 _enter("{%d{%x}},{#%u}",
499 call->debug_id, key_serial(call->conn->key),
502 if (!call->conn->cipher)
505 if (sp->hdr.securityIndex != 2) {
506 *_abort_code = RXKADINCONSISTENCY;
507 _leave(" = -EPROTO [not rxkad]");
511 /* continue encrypting from where we left off */
512 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
513 desc.tfm = call->conn->cipher;
517 /* validate the security checksum */
518 x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
519 x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff);
520 tmpbuf.x[0] = call->call_id;
523 memset(&sg, 0, sizeof(sg));
524 sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
525 sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
526 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
528 x = ntohl(tmpbuf.x[1]);
529 x = (x >> 16) & 0xffff;
531 x = 1; /* zero checksums are not permitted */
534 if (sp->hdr.cksum != cksum) {
535 *_abort_code = RXKADSEALEDINCON;
536 _leave(" = -EPROTO [csum failed]");
540 switch (call->conn->security_level) {
541 case RXRPC_SECURITY_PLAIN:
544 case RXRPC_SECURITY_AUTH:
545 ret = rxkad_verify_packet_auth(call, skb, _abort_code);
547 case RXRPC_SECURITY_ENCRYPT:
548 ret = rxkad_verify_packet_encrypt(call, skb, _abort_code);
555 _leave(" = %d", ret);
562 static int rxkad_issue_challenge(struct rxrpc_connection *conn)
564 struct rxkad_challenge challenge;
565 struct rxrpc_header hdr;
571 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
573 ret = key_validate(conn->key);
577 get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
579 challenge.version = htonl(2);
580 challenge.nonce = htonl(conn->security_nonce);
581 challenge.min_level = htonl(0);
582 challenge.__padding = 0;
584 msg.msg_name = &conn->trans->peer->srx.transport.sin;
585 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
586 msg.msg_control = NULL;
587 msg.msg_controllen = 0;
590 hdr.epoch = conn->epoch;
594 hdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
595 hdr.flags = conn->out_clientflag;
597 hdr.securityIndex = conn->security_ix;
599 hdr.serviceId = conn->service_id;
601 iov[0].iov_base = &hdr;
602 iov[0].iov_len = sizeof(hdr);
603 iov[1].iov_base = &challenge;
604 iov[1].iov_len = sizeof(challenge);
606 len = iov[0].iov_len + iov[1].iov_len;
608 hdr.serial = htonl(atomic_inc_return(&conn->serial));
609 _proto("Tx CHALLENGE %%%u", ntohl(hdr.serial));
611 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
613 _debug("sendmsg failed: %d", ret);
622 * send a Kerberos security response
624 static int rxkad_send_response(struct rxrpc_connection *conn,
625 struct rxrpc_header *hdr,
626 struct rxkad_response *resp,
627 const struct rxkad_key *s2)
636 msg.msg_name = &conn->trans->peer->srx.transport.sin;
637 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
638 msg.msg_control = NULL;
639 msg.msg_controllen = 0;
642 hdr->epoch = conn->epoch;
644 hdr->type = RXRPC_PACKET_TYPE_RESPONSE;
645 hdr->flags = conn->out_clientflag;
649 iov[0].iov_base = hdr;
650 iov[0].iov_len = sizeof(*hdr);
651 iov[1].iov_base = resp;
652 iov[1].iov_len = sizeof(*resp);
653 iov[2].iov_base = (void *) s2->ticket;
654 iov[2].iov_len = s2->ticket_len;
656 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
658 hdr->serial = htonl(atomic_inc_return(&conn->serial));
659 _proto("Tx RESPONSE %%%u", ntohl(hdr->serial));
661 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 3, len);
663 _debug("sendmsg failed: %d", ret);
672 * calculate the response checksum
674 static void rxkad_calc_response_checksum(struct rxkad_response *response)
678 u8 *p = (u8 *) response;
680 for (loop = sizeof(*response); loop > 0; loop--)
681 csum = csum * 0x10204081 + *p++;
683 response->encrypted.checksum = htonl(csum);
687 * load a scatterlist with a potentially split-page buffer
689 static void rxkad_sg_set_buf2(struct scatterlist sg[2],
690 void *buf, size_t buflen)
693 memset(sg, 0, sizeof(sg));
695 sg_set_buf(&sg[0], buf, buflen);
696 if (sg[0].offset + buflen > PAGE_SIZE) {
697 /* the buffer was split over two pages */
698 sg[0].length = PAGE_SIZE - sg[0].offset;
699 sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
702 ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
706 * encrypt the response packet
708 static void rxkad_encrypt_response(struct rxrpc_connection *conn,
709 struct rxkad_response *resp,
710 const struct rxkad_key *s2)
712 struct blkcipher_desc desc;
713 struct rxrpc_crypt iv;
714 struct scatterlist ssg[2], dsg[2];
716 /* continue encrypting from where we left off */
717 memcpy(&iv, s2->session_key, sizeof(iv));
718 desc.tfm = conn->cipher;
722 rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
723 memcpy(dsg, ssg, sizeof(dsg));
724 crypto_blkcipher_encrypt_iv(&desc, dsg, ssg, sizeof(resp->encrypted));
728 * respond to a challenge packet
730 static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
734 const struct rxrpc_key_payload *payload;
735 struct rxkad_challenge challenge;
736 struct rxkad_response resp
737 __attribute__((aligned(8))); /* must be aligned for crypto */
738 struct rxrpc_skb_priv *sp;
739 u32 version, nonce, min_level, abort_code;
742 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
745 _leave(" = -EPROTO [no key]");
749 ret = key_validate(conn->key);
751 *_abort_code = RXKADEXPIRED;
755 abort_code = RXKADPACKETSHORT;
757 if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0)
760 version = ntohl(challenge.version);
761 nonce = ntohl(challenge.nonce);
762 min_level = ntohl(challenge.min_level);
764 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
765 ntohl(sp->hdr.serial), version, nonce, min_level);
767 abort_code = RXKADINCONSISTENCY;
768 if (version != RXKAD_VERSION)
771 abort_code = RXKADLEVELFAIL;
772 if (conn->security_level < min_level)
775 payload = conn->key->payload.data;
777 /* build the response packet */
778 memset(&resp, 0, sizeof(resp));
780 resp.version = RXKAD_VERSION;
781 resp.encrypted.epoch = conn->epoch;
782 resp.encrypted.cid = conn->cid;
783 resp.encrypted.securityIndex = htonl(conn->security_ix);
784 resp.encrypted.call_id[0] =
785 (conn->channels[0] ? conn->channels[0]->call_id : 0);
786 resp.encrypted.call_id[1] =
787 (conn->channels[1] ? conn->channels[1]->call_id : 0);
788 resp.encrypted.call_id[2] =
789 (conn->channels[2] ? conn->channels[2]->call_id : 0);
790 resp.encrypted.call_id[3] =
791 (conn->channels[3] ? conn->channels[3]->call_id : 0);
792 resp.encrypted.inc_nonce = htonl(nonce + 1);
793 resp.encrypted.level = htonl(conn->security_level);
794 resp.kvno = htonl(payload->k.kvno);
795 resp.ticket_len = htonl(payload->k.ticket_len);
797 /* calculate the response checksum and then do the encryption */
798 rxkad_calc_response_checksum(&resp);
799 rxkad_encrypt_response(conn, &resp, &payload->k);
800 return rxkad_send_response(conn, &sp->hdr, &resp, &payload->k);
803 *_abort_code = abort_code;
804 _leave(" = -EPROTO [%d]", abort_code);
809 * decrypt the kerberos IV ticket in the response
811 static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
812 void *ticket, size_t ticket_len,
813 struct rxrpc_crypt *_session_key,
817 struct blkcipher_desc desc;
818 struct rxrpc_crypt iv, key;
819 struct scatterlist ssg[1], dsg[1];
825 u8 *p, *q, *name, *end;
827 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
831 ret = key_validate(conn->server_key);
835 *_abort_code = RXKADEXPIRED;
838 *_abort_code = RXKADNOAUTH;
843 ASSERT(conn->server_key->payload.data != NULL);
844 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
846 memcpy(&iv, &conn->server_key->type_data, sizeof(iv));
848 desc.tfm = conn->server_key->payload.data;
852 sg_init_one(&ssg[0], ticket, ticket_len);
853 memcpy(dsg, ssg, sizeof(dsg));
854 crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, ticket_len);
857 end = p + ticket_len;
862 q = memchr(p, 0, end - p); \
863 if (!q || q - p > (size)) \
872 /* extract the ticket flags */
873 _debug("KIV FLAGS: %x", *p);
874 little_endian = *p & 1;
877 /* extract the authentication name */
879 _debug("KIV ANAME: %s", name);
881 /* extract the principal's instance */
883 _debug("KIV INST : %s", name);
885 /* extract the principal's authentication domain */
887 _debug("KIV REALM: %s", name);
889 if (end - p < 4 + 8 + 4 + 2)
892 /* get the IPv4 address of the entity that requested the ticket */
893 memcpy(&addr, p, sizeof(addr));
895 _debug("KIV ADDR : "NIPQUAD_FMT, NIPQUAD(addr));
897 /* get the session key from the ticket */
898 memcpy(&key, p, sizeof(key));
900 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
901 memcpy(_session_key, &key, sizeof(key));
903 /* get the ticket's lifetime */
904 life = *p++ * 5 * 60;
905 _debug("KIV LIFE : %u", life);
907 /* get the issue time of the ticket */
910 memcpy(&stamp, p, 4);
911 issue = le32_to_cpu(stamp);
914 memcpy(&stamp, p, 4);
915 issue = be32_to_cpu(stamp);
919 _debug("KIV ISSUE: %lx [%lx]", issue, now);
921 /* check the ticket is in date */
923 *_abort_code = RXKADNOAUTH;
928 if (issue < now - life) {
929 *_abort_code = RXKADEXPIRED;
934 *_expiry = issue + life;
936 /* get the service name */
938 _debug("KIV SNAME: %s", name);
940 /* get the service instance name */
942 _debug("KIV SINST: %s", name);
946 _leave(" = %d", ret);
950 *_abort_code = RXKADBADTICKET;
956 * decrypt the response packet
958 static void rxkad_decrypt_response(struct rxrpc_connection *conn,
959 struct rxkad_response *resp,
960 const struct rxrpc_crypt *session_key)
962 struct blkcipher_desc desc;
963 struct scatterlist ssg[2], dsg[2];
964 struct rxrpc_crypt iv;
967 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
969 ASSERT(rxkad_ci != NULL);
971 mutex_lock(&rxkad_ci_mutex);
972 if (crypto_blkcipher_setkey(rxkad_ci, session_key->x,
973 sizeof(*session_key)) < 0)
976 memcpy(&iv, session_key, sizeof(iv));
981 rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
982 memcpy(dsg, ssg, sizeof(dsg));
983 crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, sizeof(resp->encrypted));
984 mutex_unlock(&rxkad_ci_mutex);
992 static int rxkad_verify_response(struct rxrpc_connection *conn,
996 struct rxkad_response response
997 __attribute__((aligned(8))); /* must be aligned for crypto */
998 struct rxrpc_skb_priv *sp;
999 struct rxrpc_crypt session_key;
1002 u32 abort_code, version, kvno, ticket_len, csum, level;
1005 _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
1007 abort_code = RXKADPACKETSHORT;
1008 if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0)
1009 goto protocol_error;
1010 if (!pskb_pull(skb, sizeof(response)))
1013 version = ntohl(response.version);
1014 ticket_len = ntohl(response.ticket_len);
1015 kvno = ntohl(response.kvno);
1016 sp = rxrpc_skb(skb);
1017 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1018 ntohl(sp->hdr.serial), version, kvno, ticket_len);
1020 abort_code = RXKADINCONSISTENCY;
1021 if (version != RXKAD_VERSION)
1023 abort_code = RXKADTICKETLEN;
1024 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1025 goto protocol_error;
1027 abort_code = RXKADUNKNOWNKEY;
1028 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1029 goto protocol_error;
1031 /* extract the kerberos ticket and decrypt and decode it */
1032 ticket = kmalloc(ticket_len, GFP_NOFS);
1036 abort_code = RXKADPACKETSHORT;
1037 if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0)
1038 goto protocol_error_free;
1040 ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
1041 &expiry, &abort_code);
1043 *_abort_code = abort_code;
1048 /* use the session key from inside the ticket to decrypt the
1050 rxkad_decrypt_response(conn, &response, &session_key);
1052 abort_code = RXKADSEALEDINCON;
1053 if (response.encrypted.epoch != conn->epoch)
1054 goto protocol_error_free;
1055 if (response.encrypted.cid != conn->cid)
1056 goto protocol_error_free;
1057 if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
1058 goto protocol_error_free;
1059 csum = response.encrypted.checksum;
1060 response.encrypted.checksum = 0;
1061 rxkad_calc_response_checksum(&response);
1062 if (response.encrypted.checksum != csum)
1063 goto protocol_error_free;
1065 if (ntohl(response.encrypted.call_id[0]) > INT_MAX ||
1066 ntohl(response.encrypted.call_id[1]) > INT_MAX ||
1067 ntohl(response.encrypted.call_id[2]) > INT_MAX ||
1068 ntohl(response.encrypted.call_id[3]) > INT_MAX)
1069 goto protocol_error_free;
1071 abort_code = RXKADOUTOFSEQUENCE;
1072 if (response.encrypted.inc_nonce != htonl(conn->security_nonce + 1))
1073 goto protocol_error_free;
1075 abort_code = RXKADLEVELFAIL;
1076 level = ntohl(response.encrypted.level);
1077 if (level > RXRPC_SECURITY_ENCRYPT)
1078 goto protocol_error_free;
1079 conn->security_level = level;
1081 /* create a key to hold the security data and expiration time - after
1082 * this the connection security can be handled in exactly the same way
1083 * as for a client connection */
1084 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1094 protocol_error_free:
1097 *_abort_code = abort_code;
1098 _leave(" = -EPROTO [%d]", abort_code);
1103 * clear the connection security
1105 static void rxkad_clear(struct rxrpc_connection *conn)
1110 crypto_free_blkcipher(conn->cipher);
1114 * RxRPC Kerberos-based security
1116 static struct rxrpc_security rxkad = {
1117 .owner = THIS_MODULE,
1119 .security_index = RXKAD_VERSION,
1120 .init_connection_security = rxkad_init_connection_security,
1121 .prime_packet_security = rxkad_prime_packet_security,
1122 .secure_packet = rxkad_secure_packet,
1123 .verify_packet = rxkad_verify_packet,
1124 .issue_challenge = rxkad_issue_challenge,
1125 .respond_to_challenge = rxkad_respond_to_challenge,
1126 .verify_response = rxkad_verify_response,
1127 .clear = rxkad_clear,
1130 static __init int rxkad_init(void)
1134 /* pin the cipher we need so that the crypto layer doesn't invoke
1135 * keventd to go get it */
1136 rxkad_ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
1137 if (IS_ERR(rxkad_ci))
1138 return PTR_ERR(rxkad_ci);
1140 return rxrpc_register_security(&rxkad);
1143 module_init(rxkad_init);
1145 static __exit void rxkad_exit(void)
1149 rxrpc_unregister_security(&rxkad);
1150 crypto_free_blkcipher(rxkad_ci);
1153 module_exit(rxkad_exit);