12 #include <linux/module.h>
13 #include <linux/net.h>
15 #include <linux/udp.h>
18 #include <linux/ctype.h>
19 #include <linux/slab.h>
23 #define rxrpc_debug rxkad_debug
26 #define RXKAD_VERSION 2
27 #define MAXKRB5TICKETLEN 1024
28 #define RXKAD_TKT_TYPE_KERBEROS_V5 256
70 token = conn->
key->payload.data;
80 if (crypto_blkcipher_setkey(ci, token->
kad->session_key,
81 sizeof(token->
kad->session_key)) < 0)
128 token = conn->
key->payload.data;
142 crypto_blkcipher_encrypt_iv(&
desc, &
sg[0], &
sg[1],
sizeof(
tmpbuf));
153 static int rxkad_secure_packet_auth(
const struct rxrpc_call *call,
173 data_size |= (
u32) check << 16;
186 crypto_blkcipher_encrypt_iv(&
desc, &
sg[0], &
sg[1],
sizeof(
tmpbuf));
197 static int rxkad_secure_packet_encrypt(
const struct rxrpc_call *call,
220 rxkhdr.data_size =
htonl(data_size | (
u32) check << 16);
224 token = call->
conn->key->payload.data;
232 crypto_blkcipher_encrypt_iv(&
desc, &
sg[0], &
sg[1],
sizeof(rxkhdr));
236 if (nsg < 0 || nsg > 16)
239 len = data_size + call->
conn->size_align - 1;
240 len &= ~(call->
conn->size_align - 1);
244 crypto_blkcipher_encrypt_iv(&
desc,
sg,
sg, len);
253 static int rxkad_secure_packet(
const struct rxrpc_call *call,
271 _enter(
"{%d{%x}},{#%u},%zu,",
275 if (!call->
conn->cipher)
296 crypto_blkcipher_encrypt_iv(&
desc, &
sg[0], &
sg[1],
sizeof(
tmpbuf));
299 y = (y >> 16) & 0xffff;
304 switch (call->
conn->security_level) {
309 ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
312 ret = rxkad_secure_packet_encrypt(call, skb, data_size,
320 _leave(
" = %d [set %hx]", ret, y);
327 static int rxkad_verify_packet_auth(
const struct rxrpc_call *call,
347 if (nsg < 0 || nsg > 16)
359 crypto_blkcipher_decrypt_iv(&
desc,
sg,
sg, 8);
367 buf =
ntohl(sechdr.data_size);
368 data_size = buf & 0xffff;
379 if (data_size > skb->
len)
384 _leave(
" = 0 [dlen=%x]", data_size);
401 static int rxkad_verify_packet_encrypt(
const struct rxrpc_call *call,
436 token = call->
conn->key->payload.data;
442 crypto_blkcipher_decrypt_iv(&
desc, sg, sg, skb->
len);
452 buf =
ntohl(sechdr.data_size);
453 data_size = buf & 0xffff;
464 if (data_size > skb->
len)
469 _leave(
" = 0 [dlen=%x]", data_size);
486 static int rxkad_verify_packet(
const struct rxrpc_call *call,
508 if (!call->
conn->cipher)
513 _leave(
" = -EPROTO [not rxkad]");
531 crypto_blkcipher_encrypt_iv(&
desc, &sg[0], &sg[1],
sizeof(
tmpbuf));
534 y = (y >> 16) & 0xffff;
539 if (sp->
hdr.cksum != cksum) {
541 _leave(
" = -EPROTO [csum failed]");
545 switch (call->
conn->security_level) {
550 ret = rxkad_verify_packet_auth(call, skb, _abort_code);
553 ret = rxkad_verify_packet_encrypt(call, skb, _abort_code);
584 challenge.version =
htonl(2);
586 challenge.min_level =
htonl(0);
587 challenge.__padding = 0;
589 msg.msg_name = &conn->
trans->peer->srx.transport.sin;
590 msg.msg_namelen =
sizeof(conn->
trans->peer->srx.transport.sin);
592 msg.msg_controllen = 0;
606 iov[0].iov_base = &
hdr;
607 iov[0].iov_len =
sizeof(
hdr);
608 iov[1].iov_base = &challenge;
609 iov[1].iov_len =
sizeof(challenge);
611 len = iov[0].iov_len + iov[1].iov_len;
618 _debug(
"sendmsg failed: %d", ret);
641 msg.msg_name = &conn->
trans->peer->srx.transport.sin;
642 msg.msg_namelen =
sizeof(conn->
trans->peer->srx.transport.sin);
644 msg.msg_controllen = 0;
654 iov[0].iov_base =
hdr;
655 iov[0].iov_len =
sizeof(*hdr);
656 iov[1].iov_base =
resp;
657 iov[1].iov_len =
sizeof(*resp);
658 iov[2].iov_base = (
void *) s2->
ticket;
661 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
668 _debug(
"sendmsg failed: %d", ret);
683 u8 *
p = (
u8 *) response;
685 for (loop =
sizeof(*response); loop > 0; loop--)
686 csum = csum * 0x10204081 + *p++;
694 static void rxkad_sg_set_buf2(
struct scatterlist sg[2],
701 sg_set_buf(&sg[0], buf, buflen);
705 sg_set_buf(&sg[1], buf + sg[0].
length, buflen - sg[0].length);
709 sg_mark_end(&sg[nsg - 1]);
732 crypto_blkcipher_encrypt_iv(&
desc, sg, sg,
sizeof(resp->
encrypted));
753 _leave(
" = -EPROTO [no key]");
765 if (
skb_copy_bits(skb, 0, &challenge,
sizeof(challenge)) < 0)
768 version =
ntohl(challenge.version);
769 nonce =
ntohl(challenge.nonce);
770 min_level =
ntohl(challenge.min_level);
772 _proto(
"Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
773 ntohl(sp->
hdr.serial), version, nonce, min_level);
783 token = conn->
key->payload.data;
786 memset(&resp, 0,
sizeof(resp));
806 rxkad_calc_response_checksum(&resp);
807 rxkad_encrypt_response(conn, &resp, token->
kad);
808 return rxkad_send_response(conn, &sp->
hdr, &resp, token->
kad);
811 *_abort_code = abort_code;
812 _leave(
" = -EPROTO [%d]", abort_code);
820 void *ticket,
size_t ticket_len,
861 crypto_blkcipher_decrypt_iv(&
desc, sg, sg, ticket_len);
864 end = p + ticket_len;
869 q = memchr(p, 0, end - p); \
870 if (!q || q - p > (size)) \
880 _debug(
"KIV FLAGS: %x", *p);
881 little_endian = *p & 1;
886 _debug(
"KIV ANAME: %s", name);
890 _debug(
"KIV INST : %s", name);
894 _debug(
"KIV REALM: %s", name);
896 if (end - p < 4 + 8 + 4 + 2)
911 life = *p++ * 5 * 60;
912 _debug(
"KIV LIFE : %u", life);
926 _debug(
"KIV ISSUE: %lx [%lx]", issue, now);
935 if (issue < now - life) {
941 *_expiry = issue + life;
945 _debug(
"KIV SNAME: %s", name);
949 _debug(
"KIV SINST: %s", name);
979 if (crypto_blkcipher_setkey(rxkad_ci, session_key->
x,
980 sizeof(*session_key)) < 0)
989 crypto_blkcipher_decrypt_iv(&
desc, sg, sg,
sizeof(resp->
encrypted));
1016 goto protocol_error;
1017 if (!pskb_pull(skb,
sizeof(response)))
1024 _proto(
"Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1025 ntohl(sp->
hdr.serial), version, kvno, ticket_len);
1029 goto protocol_error;
1033 goto protocol_error;
1037 goto protocol_error;
1046 goto protocol_error_free;
1048 ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
1049 &expiry, &abort_code);
1051 *_abort_code = abort_code;
1058 rxkad_decrypt_response(conn, &response, &session_key);
1062 goto protocol_error_free;
1064 goto protocol_error_free;
1066 goto protocol_error_free;
1069 rxkad_calc_response_checksum(&response);
1070 if (response.
encrypted.checksum != csum)
1071 goto protocol_error_free;
1077 goto protocol_error_free;
1081 goto protocol_error_free;
1086 goto protocol_error_free;
1102 protocol_error_free:
1105 *_abort_code = abort_code;
1106 _leave(
" = -EPROTO [%d]", abort_code);
1118 crypto_free_blkcipher(conn->
cipher);
1128 .init_connection_security = rxkad_init_connection_security,
1129 .prime_packet_security = rxkad_prime_packet_security,
1130 .secure_packet = rxkad_secure_packet,
1131 .verify_packet = rxkad_verify_packet,
1132 .issue_challenge = rxkad_issue_challenge,
1133 .respond_to_challenge = rxkad_respond_to_challenge,
1134 .verify_response = rxkad_verify_response,
1135 .clear = rxkad_clear,
1138 static __init int rxkad_init(
void)
1145 if (IS_ERR(rxkad_ci))
1146 return PTR_ERR(rxkad_ci);
1153 static __exit void rxkad_exit(
void)
1158 crypto_free_blkcipher(rxkad_ci);