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>
19 #include <linux/slab.h>
21 #include <net/af_rxrpc.h>
22 #include <keys/rxrpc-type.h>
23 #define rxrpc_debug rxkad_debug
24 #include "ar-internal.h"
26 #define RXKAD_VERSION 2
27 #define MAXKRB5TICKETLEN 1024
28 #define RXKAD_TKT_TYPE_KERBEROS_V5 256
29 #define ANAME_SZ 40 /* size of authentication name */
30 #define INST_SZ 40 /* size of principal's instance */
31 #define REALM_SZ 40 /* size of principal's auth domain */
32 #define SNAME_SZ 40 /* size of service name */
34 unsigned int rxrpc_debug;
35 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
36 MODULE_PARM_DESC(debug, "rxkad debugging mask");
38 struct rxkad_level1_hdr {
39 __be32 data_size; /* true data size (excluding padding) */
42 struct rxkad_level2_hdr {
43 __be32 data_size; /* true data size (excluding padding) */
44 __be32 checksum; /* decrypted data checksum */
47 MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos 4)");
48 MODULE_AUTHOR("Red Hat, Inc.");
49 MODULE_LICENSE("GPL");
52 * this holds a pinned cipher so that keventd doesn't get called by the cipher
53 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
56 static struct crypto_blkcipher *rxkad_ci;
57 static DEFINE_MUTEX(rxkad_ci_mutex);
60 * initialise connection security
62 static int rxkad_init_connection_security(struct rxrpc_connection *conn)
64 struct crypto_blkcipher *ci;
65 struct rxrpc_key_token *token;
68 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key));
70 token = conn->key->payload.data[0];
71 conn->security_ix = token->security_index;
73 ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
80 if (crypto_blkcipher_setkey(ci, token->kad->session_key,
81 sizeof(token->kad->session_key)) < 0)
84 switch (conn->security_level) {
85 case RXRPC_SECURITY_PLAIN:
87 case RXRPC_SECURITY_AUTH:
89 conn->security_size = sizeof(struct rxkad_level1_hdr);
90 conn->header_size += sizeof(struct rxkad_level1_hdr);
92 case RXRPC_SECURITY_ENCRYPT:
94 conn->security_size = sizeof(struct rxkad_level2_hdr);
95 conn->header_size += sizeof(struct rxkad_level2_hdr);
105 _leave(" = %d", ret);
110 * prime the encryption state with the invariant parts of a connection's
113 static void rxkad_prime_packet_security(struct rxrpc_connection *conn)
115 struct rxrpc_key_token *token;
116 struct blkcipher_desc desc;
117 struct scatterlist sg[2];
118 struct rxrpc_crypt iv;
121 } tmpbuf __attribute__((aligned(16))); /* must all be in same page */
128 token = conn->key->payload.data[0];
129 memcpy(&iv, token->kad->session_key, sizeof(iv));
131 desc.tfm = conn->cipher;
135 tmpbuf.x[0] = htonl(conn->epoch);
136 tmpbuf.x[1] = htonl(conn->cid);
138 tmpbuf.x[3] = htonl(conn->security_ix);
140 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
141 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
142 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
144 memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
145 ASSERTCMP((u32 __force)conn->csum_iv.n[0], ==, (u32 __force)tmpbuf.x[2]);
151 * partially encrypt a packet (level 1 security)
153 static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
158 struct rxrpc_skb_priv *sp;
159 struct blkcipher_desc desc;
160 struct rxrpc_crypt iv;
161 struct scatterlist sg[2];
163 struct rxkad_level1_hdr hdr;
164 __be32 first; /* first four bytes of data and padding */
165 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
172 check = sp->hdr.seq ^ sp->hdr.callNumber;
173 data_size |= (u32)check << 16;
175 tmpbuf.hdr.data_size = htonl(data_size);
176 memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first));
178 /* start the encryption afresh */
179 memset(&iv, 0, sizeof(iv));
180 desc.tfm = call->conn->cipher;
184 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
185 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
186 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
188 memcpy(sechdr, &tmpbuf, sizeof(tmpbuf));
195 * wholly encrypt a packet (level 2 security)
197 static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
202 const struct rxrpc_key_token *token;
203 struct rxkad_level2_hdr rxkhdr
204 __attribute__((aligned(8))); /* must be all on one page */
205 struct rxrpc_skb_priv *sp;
206 struct blkcipher_desc desc;
207 struct rxrpc_crypt iv;
208 struct scatterlist sg[16];
209 struct sk_buff *trailer;
218 check = sp->hdr.seq ^ sp->hdr.callNumber;
220 rxkhdr.data_size = htonl(data_size | (u32)check << 16);
223 /* encrypt from the session key */
224 token = call->conn->key->payload.data[0];
225 memcpy(&iv, token->kad->session_key, sizeof(iv));
226 desc.tfm = call->conn->cipher;
230 sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
231 sg_init_one(&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 sg_init_table(sg, nsg);
243 skb_to_sgvec(skb, sg, 0, len);
244 crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
251 * checksum an RxRPC packet header
253 static int rxkad_secure_packet(const struct rxrpc_call *call,
258 struct rxrpc_skb_priv *sp;
259 struct blkcipher_desc desc;
260 struct rxrpc_crypt iv;
261 struct scatterlist sg[2];
264 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
270 _enter("{%d{%x}},{#%u},%zu,",
271 call->debug_id, key_serial(call->conn->key), sp->hdr.seq,
274 if (!call->conn->cipher)
277 ret = key_validate(call->conn->key);
281 /* continue encrypting from where we left off */
282 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
283 desc.tfm = call->conn->cipher;
287 /* calculate the security checksum */
288 x = call->channel << (32 - RXRPC_CIDSHIFT);
289 x |= sp->hdr.seq & 0x3fffffff;
290 tmpbuf.x[0] = htonl(sp->hdr.callNumber);
291 tmpbuf.x[1] = htonl(x);
293 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
294 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
295 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
297 y = ntohl(tmpbuf.x[1]);
298 y = (y >> 16) & 0xffff;
300 y = 1; /* zero checksums are not permitted */
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, y);
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[16];
335 struct sk_buff *trailer;
344 /* we want to decrypt the skbuff in-place */
345 nsg = skb_cow_data(skb, 0, &trailer);
346 if (nsg < 0 || nsg > 16)
349 sg_init_table(sg, nsg);
350 skb_to_sgvec(skb, sg, 0, 8);
352 /* start the decryption afresh */
353 memset(&iv, 0, sizeof(iv));
354 desc.tfm = call->conn->cipher;
358 crypto_blkcipher_decrypt_iv(&desc, sg, sg, 8);
360 /* remove the decrypted packet length */
361 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
363 if (!skb_pull(skb, sizeof(sechdr)))
366 buf = ntohl(sechdr.data_size);
367 data_size = buf & 0xffff;
370 check ^= sp->hdr.seq ^ sp->hdr.callNumber;
373 *_abort_code = RXKADSEALEDINCON;
377 /* shorten the packet to remove the padding */
378 if (data_size > skb->len)
380 else if (data_size < skb->len)
381 skb->len = data_size;
383 _leave(" = 0 [dlen=%x]", data_size);
387 *_abort_code = RXKADDATALEN;
389 _leave(" = -EPROTO");
393 _leave(" = -ENOMEM");
398 * wholly decrypt a packet (level 2 security)
400 static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call,
404 const struct rxrpc_key_token *token;
405 struct rxkad_level2_hdr sechdr;
406 struct rxrpc_skb_priv *sp;
407 struct blkcipher_desc desc;
408 struct rxrpc_crypt iv;
409 struct scatterlist _sg[4], *sg;
410 struct sk_buff *trailer;
415 _enter(",{%d}", skb->len);
419 /* we want to decrypt the skbuff in-place */
420 nsg = skb_cow_data(skb, 0, &trailer);
425 if (unlikely(nsg > 4)) {
426 sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
431 sg_init_table(sg, nsg);
432 skb_to_sgvec(skb, sg, 0, skb->len);
434 /* decrypt from the session key */
435 token = call->conn->key->payload.data[0];
436 memcpy(&iv, token->kad->session_key, sizeof(iv));
437 desc.tfm = call->conn->cipher;
441 crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len);
445 /* remove the decrypted packet length */
446 if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
448 if (!skb_pull(skb, sizeof(sechdr)))
451 buf = ntohl(sechdr.data_size);
452 data_size = buf & 0xffff;
455 check ^= sp->hdr.seq ^ sp->hdr.callNumber;
458 *_abort_code = RXKADSEALEDINCON;
462 /* shorten the packet to remove the padding */
463 if (data_size > skb->len)
465 else if (data_size < skb->len)
466 skb->len = data_size;
468 _leave(" = 0 [dlen=%x]", data_size);
472 *_abort_code = RXKADDATALEN;
474 _leave(" = -EPROTO");
478 _leave(" = -ENOMEM");
483 * verify the security on a received packet
485 static int rxkad_verify_packet(const struct rxrpc_call *call,
489 struct blkcipher_desc desc;
490 struct rxrpc_skb_priv *sp;
491 struct rxrpc_crypt iv;
492 struct scatterlist sg[2];
495 } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
502 _enter("{%d{%x}},{#%u}",
503 call->debug_id, key_serial(call->conn->key), sp->hdr.seq);
505 if (!call->conn->cipher)
508 if (sp->hdr.securityIndex != RXRPC_SECURITY_RXKAD) {
509 *_abort_code = RXKADINCONSISTENCY;
510 _leave(" = -EPROTO [not rxkad]");
514 /* continue encrypting from where we left off */
515 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
516 desc.tfm = call->conn->cipher;
520 /* validate the security checksum */
521 x = call->channel << (32 - RXRPC_CIDSHIFT);
522 x |= sp->hdr.seq & 0x3fffffff;
523 tmpbuf.x[0] = htonl(call->call_id);
524 tmpbuf.x[1] = htonl(x);
526 sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
527 sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
528 crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
530 y = ntohl(tmpbuf.x[1]);
531 cksum = (y >> 16) & 0xffff;
533 cksum = 1; /* zero checksums are not permitted */
535 if (sp->hdr.cksum != cksum) {
536 *_abort_code = RXKADSEALEDINCON;
537 _leave(" = -EPROTO [csum failed]");
541 switch (call->conn->security_level) {
542 case RXRPC_SECURITY_PLAIN:
545 case RXRPC_SECURITY_AUTH:
546 ret = rxkad_verify_packet_auth(call, skb, _abort_code);
548 case RXRPC_SECURITY_ENCRYPT:
549 ret = rxkad_verify_packet_encrypt(call, skb, _abort_code);
556 _leave(" = %d", ret);
563 static int rxkad_issue_challenge(struct rxrpc_connection *conn)
565 struct rxkad_challenge challenge;
566 struct rxrpc_wire_header whdr;
573 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
575 ret = key_validate(conn->key);
579 get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
581 challenge.version = htonl(2);
582 challenge.nonce = htonl(conn->security_nonce);
583 challenge.min_level = htonl(0);
584 challenge.__padding = 0;
586 msg.msg_name = &conn->trans->peer->srx.transport.sin;
587 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
588 msg.msg_control = NULL;
589 msg.msg_controllen = 0;
592 whdr.epoch = htonl(conn->epoch);
593 whdr.cid = htonl(conn->cid);
596 whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
597 whdr.flags = conn->out_clientflag;
599 whdr.securityIndex = conn->security_ix;
601 whdr.serviceId = htons(conn->service_id);
603 iov[0].iov_base = &whdr;
604 iov[0].iov_len = sizeof(whdr);
605 iov[1].iov_base = &challenge;
606 iov[1].iov_len = sizeof(challenge);
608 len = iov[0].iov_len + iov[1].iov_len;
610 serial = atomic_inc_return(&conn->serial);
611 whdr.serial = htonl(serial);
612 _proto("Tx CHALLENGE %%%u", serial);
614 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
616 _debug("sendmsg failed: %d", ret);
625 * send a Kerberos security response
627 static int rxkad_send_response(struct rxrpc_connection *conn,
628 struct rxrpc_host_header *hdr,
629 struct rxkad_response *resp,
630 const struct rxkad_key *s2)
632 struct rxrpc_wire_header whdr;
641 msg.msg_name = &conn->trans->peer->srx.transport.sin;
642 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
643 msg.msg_control = NULL;
644 msg.msg_controllen = 0;
647 memset(&whdr, 0, sizeof(whdr));
648 whdr.epoch = htonl(hdr->epoch);
649 whdr.cid = htonl(hdr->cid);
650 whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
651 whdr.flags = conn->out_clientflag;
652 whdr.securityIndex = hdr->securityIndex;
653 whdr.serviceId = htons(hdr->serviceId);
655 iov[0].iov_base = &whdr;
656 iov[0].iov_len = sizeof(whdr);
657 iov[1].iov_base = resp;
658 iov[1].iov_len = sizeof(*resp);
659 iov[2].iov_base = (void *)s2->ticket;
660 iov[2].iov_len = s2->ticket_len;
662 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
664 serial = atomic_inc_return(&conn->serial);
665 whdr.serial = htonl(serial);
666 _proto("Tx RESPONSE %%%u", serial);
668 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 3, len);
670 _debug("sendmsg failed: %d", ret);
679 * calculate the response checksum
681 static void rxkad_calc_response_checksum(struct rxkad_response *response)
685 u8 *p = (u8 *) response;
687 for (loop = sizeof(*response); loop > 0; loop--)
688 csum = csum * 0x10204081 + *p++;
690 response->encrypted.checksum = htonl(csum);
694 * load a scatterlist with a potentially split-page buffer
696 static void rxkad_sg_set_buf2(struct scatterlist sg[2],
697 void *buf, size_t buflen)
701 sg_init_table(sg, 2);
703 sg_set_buf(&sg[0], buf, buflen);
704 if (sg[0].offset + buflen > PAGE_SIZE) {
705 /* the buffer was split over two pages */
706 sg[0].length = PAGE_SIZE - sg[0].offset;
707 sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
711 sg_mark_end(&sg[nsg - 1]);
713 ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
717 * encrypt the response packet
719 static void rxkad_encrypt_response(struct rxrpc_connection *conn,
720 struct rxkad_response *resp,
721 const struct rxkad_key *s2)
723 struct blkcipher_desc desc;
724 struct rxrpc_crypt iv;
725 struct scatterlist sg[2];
727 /* continue encrypting from where we left off */
728 memcpy(&iv, s2->session_key, sizeof(iv));
729 desc.tfm = conn->cipher;
733 rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
734 crypto_blkcipher_encrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
738 * respond to a challenge packet
740 static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
744 const struct rxrpc_key_token *token;
745 struct rxkad_challenge challenge;
746 struct rxkad_response resp
747 __attribute__((aligned(8))); /* must be aligned for crypto */
748 struct rxrpc_skb_priv *sp;
749 u32 version, nonce, min_level, abort_code;
752 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
755 _leave(" = -EPROTO [no key]");
759 ret = key_validate(conn->key);
761 *_abort_code = RXKADEXPIRED;
765 abort_code = RXKADPACKETSHORT;
767 if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0)
770 version = ntohl(challenge.version);
771 nonce = ntohl(challenge.nonce);
772 min_level = ntohl(challenge.min_level);
774 _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
775 sp->hdr.serial, version, nonce, min_level);
777 abort_code = RXKADINCONSISTENCY;
778 if (version != RXKAD_VERSION)
781 abort_code = RXKADLEVELFAIL;
782 if (conn->security_level < min_level)
785 token = conn->key->payload.data[0];
787 /* build the response packet */
788 memset(&resp, 0, sizeof(resp));
790 resp.version = htonl(RXKAD_VERSION);
791 resp.encrypted.epoch = htonl(conn->epoch);
792 resp.encrypted.cid = htonl(conn->cid);
793 resp.encrypted.securityIndex = htonl(conn->security_ix);
794 resp.encrypted.inc_nonce = htonl(nonce + 1);
795 resp.encrypted.level = htonl(conn->security_level);
796 resp.kvno = htonl(token->kad->kvno);
797 resp.ticket_len = htonl(token->kad->ticket_len);
799 resp.encrypted.call_id[0] =
800 htonl(conn->channels[0] ? conn->channels[0]->call_id : 0);
801 resp.encrypted.call_id[1] =
802 htonl(conn->channels[1] ? conn->channels[1]->call_id : 0);
803 resp.encrypted.call_id[2] =
804 htonl(conn->channels[2] ? conn->channels[2]->call_id : 0);
805 resp.encrypted.call_id[3] =
806 htonl(conn->channels[3] ? conn->channels[3]->call_id : 0);
808 /* calculate the response checksum and then do the encryption */
809 rxkad_calc_response_checksum(&resp);
810 rxkad_encrypt_response(conn, &resp, token->kad);
811 return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
814 *_abort_code = abort_code;
815 _leave(" = -EPROTO [%d]", abort_code);
820 * decrypt the kerberos IV ticket in the response
822 static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
823 void *ticket, size_t ticket_len,
824 struct rxrpc_crypt *_session_key,
828 struct blkcipher_desc desc;
829 struct rxrpc_crypt iv, key;
830 struct scatterlist sg[1];
836 u8 *p, *q, *name, *end;
838 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
842 ret = key_validate(conn->server_key);
846 *_abort_code = RXKADEXPIRED;
849 *_abort_code = RXKADNOAUTH;
854 ASSERT(conn->server_key->payload.data[0] != NULL);
855 ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
857 memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
859 desc.tfm = conn->server_key->payload.data[0];
863 sg_init_one(&sg[0], ticket, ticket_len);
864 crypto_blkcipher_decrypt_iv(&desc, sg, sg, ticket_len);
867 end = p + ticket_len;
872 q = memchr(p, 0, end - p); \
873 if (!q || q - p > (size)) \
882 /* extract the ticket flags */
883 _debug("KIV FLAGS: %x", *p);
884 little_endian = *p & 1;
887 /* extract the authentication name */
889 _debug("KIV ANAME: %s", name);
891 /* extract the principal's instance */
893 _debug("KIV INST : %s", name);
895 /* extract the principal's authentication domain */
897 _debug("KIV REALM: %s", name);
899 if (end - p < 4 + 8 + 4 + 2)
902 /* get the IPv4 address of the entity that requested the ticket */
903 memcpy(&addr, p, sizeof(addr));
905 _debug("KIV ADDR : %pI4", &addr);
907 /* get the session key from the ticket */
908 memcpy(&key, p, sizeof(key));
910 _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
911 memcpy(_session_key, &key, sizeof(key));
913 /* get the ticket's lifetime */
914 life = *p++ * 5 * 60;
915 _debug("KIV LIFE : %u", life);
917 /* get the issue time of the ticket */
920 memcpy(&stamp, p, 4);
921 issue = le32_to_cpu(stamp);
924 memcpy(&stamp, p, 4);
925 issue = be32_to_cpu(stamp);
929 _debug("KIV ISSUE: %lx [%lx]", issue, now);
931 /* check the ticket is in date */
933 *_abort_code = RXKADNOAUTH;
938 if (issue < now - life) {
939 *_abort_code = RXKADEXPIRED;
944 *_expiry = issue + life;
946 /* get the service name */
948 _debug("KIV SNAME: %s", name);
950 /* get the service instance name */
952 _debug("KIV SINST: %s", name);
956 _leave(" = %d", ret);
960 *_abort_code = RXKADBADTICKET;
966 * decrypt the response packet
968 static void rxkad_decrypt_response(struct rxrpc_connection *conn,
969 struct rxkad_response *resp,
970 const struct rxrpc_crypt *session_key)
972 struct blkcipher_desc desc;
973 struct scatterlist sg[2];
974 struct rxrpc_crypt iv;
977 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
979 ASSERT(rxkad_ci != NULL);
981 mutex_lock(&rxkad_ci_mutex);
982 if (crypto_blkcipher_setkey(rxkad_ci, session_key->x,
983 sizeof(*session_key)) < 0)
986 memcpy(&iv, session_key, sizeof(iv));
991 rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
992 crypto_blkcipher_decrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
993 mutex_unlock(&rxkad_ci_mutex);
1001 static int rxkad_verify_response(struct rxrpc_connection *conn,
1002 struct sk_buff *skb,
1005 struct rxkad_response response
1006 __attribute__((aligned(8))); /* must be aligned for crypto */
1007 struct rxrpc_skb_priv *sp;
1008 struct rxrpc_crypt session_key;
1011 u32 abort_code, version, kvno, ticket_len, level;
1015 _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
1017 abort_code = RXKADPACKETSHORT;
1018 if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0)
1019 goto protocol_error;
1020 if (!pskb_pull(skb, sizeof(response)))
1023 version = ntohl(response.version);
1024 ticket_len = ntohl(response.ticket_len);
1025 kvno = ntohl(response.kvno);
1026 sp = rxrpc_skb(skb);
1027 _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1028 sp->hdr.serial, version, kvno, ticket_len);
1030 abort_code = RXKADINCONSISTENCY;
1031 if (version != RXKAD_VERSION)
1032 goto protocol_error;
1034 abort_code = RXKADTICKETLEN;
1035 if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1036 goto protocol_error;
1038 abort_code = RXKADUNKNOWNKEY;
1039 if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1040 goto protocol_error;
1042 /* extract the kerberos ticket and decrypt and decode it */
1043 ticket = kmalloc(ticket_len, GFP_NOFS);
1047 abort_code = RXKADPACKETSHORT;
1048 if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0)
1049 goto protocol_error_free;
1051 ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
1052 &expiry, &abort_code);
1054 *_abort_code = abort_code;
1059 /* use the session key from inside the ticket to decrypt the
1061 rxkad_decrypt_response(conn, &response, &session_key);
1063 abort_code = RXKADSEALEDINCON;
1064 if (ntohl(response.encrypted.epoch) != conn->epoch)
1065 goto protocol_error_free;
1066 if (ntohl(response.encrypted.cid) != conn->cid)
1067 goto protocol_error_free;
1068 if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
1069 goto protocol_error_free;
1070 csum = response.encrypted.checksum;
1071 response.encrypted.checksum = 0;
1072 rxkad_calc_response_checksum(&response);
1073 if (response.encrypted.checksum != csum)
1074 goto protocol_error_free;
1076 if (ntohl(response.encrypted.call_id[0]) > INT_MAX ||
1077 ntohl(response.encrypted.call_id[1]) > INT_MAX ||
1078 ntohl(response.encrypted.call_id[2]) > INT_MAX ||
1079 ntohl(response.encrypted.call_id[3]) > INT_MAX)
1080 goto protocol_error_free;
1082 abort_code = RXKADOUTOFSEQUENCE;
1083 if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1)
1084 goto protocol_error_free;
1086 abort_code = RXKADLEVELFAIL;
1087 level = ntohl(response.encrypted.level);
1088 if (level > RXRPC_SECURITY_ENCRYPT)
1089 goto protocol_error_free;
1090 conn->security_level = level;
1092 /* create a key to hold the security data and expiration time - after
1093 * this the connection security can be handled in exactly the same way
1094 * as for a client connection */
1095 ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1105 protocol_error_free:
1108 *_abort_code = abort_code;
1109 _leave(" = -EPROTO [%d]", abort_code);
1114 * clear the connection security
1116 static void rxkad_clear(struct rxrpc_connection *conn)
1121 crypto_free_blkcipher(conn->cipher);
1125 * RxRPC Kerberos-based security
1127 static struct rxrpc_security rxkad = {
1128 .owner = THIS_MODULE,
1130 .security_index = RXRPC_SECURITY_RXKAD,
1131 .init_connection_security = rxkad_init_connection_security,
1132 .prime_packet_security = rxkad_prime_packet_security,
1133 .secure_packet = rxkad_secure_packet,
1134 .verify_packet = rxkad_verify_packet,
1135 .issue_challenge = rxkad_issue_challenge,
1136 .respond_to_challenge = rxkad_respond_to_challenge,
1137 .verify_response = rxkad_verify_response,
1138 .clear = rxkad_clear,
1141 static __init int rxkad_init(void)
1145 /* pin the cipher we need so that the crypto layer doesn't invoke
1146 * keventd to go get it */
1147 rxkad_ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
1148 if (IS_ERR(rxkad_ci))
1149 return PTR_ERR(rxkad_ci);
1151 return rxrpc_register_security(&rxkad);
1154 module_init(rxkad_init);
1156 static __exit void rxkad_exit(void)
1160 rxrpc_unregister_security(&rxkad);
1161 crypto_free_blkcipher(rxkad_ci);
1164 module_exit(rxkad_exit);