📄 rxkad.c
字号:
/* Kerberos-based RxRPC security * * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <linux/module.h>#include <linux/net.h>#include <linux/skbuff.h>#include <linux/udp.h>#include <linux/crypto.h>#include <linux/scatterlist.h>#include <linux/ctype.h>#include <net/sock.h>#include <net/af_rxrpc.h>#define rxrpc_debug rxkad_debug#include "ar-internal.h"#define RXKAD_VERSION 2#define MAXKRB5TICKETLEN 1024#define RXKAD_TKT_TYPE_KERBEROS_V5 256#define ANAME_SZ 40 /* size of authentication name */#define INST_SZ 40 /* size of principal's instance */#define REALM_SZ 40 /* size of principal's auth domain */#define SNAME_SZ 40 /* size of service name */unsigned rxrpc_debug;module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);MODULE_PARM_DESC(rxrpc_debug, "rxkad debugging mask");struct rxkad_level1_hdr { __be32 data_size; /* true data size (excluding padding) */};struct rxkad_level2_hdr { __be32 data_size; /* true data size (excluding padding) */ __be32 checksum; /* decrypted data checksum */};MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos)");MODULE_AUTHOR("Red Hat, Inc.");MODULE_LICENSE("GPL");/* * this holds a pinned cipher so that keventd doesn't get called by the cipher * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE * packets */static struct crypto_blkcipher *rxkad_ci;static DEFINE_MUTEX(rxkad_ci_mutex);/* * initialise connection security */static int rxkad_init_connection_security(struct rxrpc_connection *conn){ struct rxrpc_key_payload *payload; struct crypto_blkcipher *ci; int ret; _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key)); payload = conn->key->payload.data; conn->security_ix = payload->k.security_index; ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); if (IS_ERR(ci)) { _debug("no cipher"); ret = PTR_ERR(ci); goto error; } if (crypto_blkcipher_setkey(ci, payload->k.session_key, sizeof(payload->k.session_key)) < 0) BUG(); switch (conn->security_level) { case RXRPC_SECURITY_PLAIN: break; case RXRPC_SECURITY_AUTH: conn->size_align = 8; conn->security_size = sizeof(struct rxkad_level1_hdr); conn->header_size += sizeof(struct rxkad_level1_hdr); break; case RXRPC_SECURITY_ENCRYPT: conn->size_align = 8; conn->security_size = sizeof(struct rxkad_level2_hdr); conn->header_size += sizeof(struct rxkad_level2_hdr); break; default: ret = -EKEYREJECTED; goto error; } conn->cipher = ci; ret = 0;error: _leave(" = %d", ret); return ret;}/* * prime the encryption state with the invariant parts of a connection's * description */static void rxkad_prime_packet_security(struct rxrpc_connection *conn){ struct rxrpc_key_payload *payload; struct blkcipher_desc desc; struct scatterlist sg[2]; struct rxrpc_crypt iv; struct { __be32 x[4]; } tmpbuf __attribute__((aligned(16))); /* must all be in same page */ _enter(""); if (!conn->key) return; payload = conn->key->payload.data; memcpy(&iv, payload->k.session_key, sizeof(iv)); desc.tfm = conn->cipher; desc.info = iv.x; desc.flags = 0; tmpbuf.x[0] = conn->epoch; tmpbuf.x[1] = conn->cid; tmpbuf.x[2] = 0; tmpbuf.x[3] = htonl(conn->security_ix); sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv)); ASSERTCMP(conn->csum_iv.n[0], ==, tmpbuf.x[2]); _leave("");}/* * partially encrypt a packet (level 1 security) */static int rxkad_secure_packet_auth(const struct rxrpc_call *call, struct sk_buff *skb, u32 data_size, void *sechdr){ struct rxrpc_skb_priv *sp; struct blkcipher_desc desc; struct rxrpc_crypt iv; struct scatterlist sg[2]; struct { struct rxkad_level1_hdr hdr; __be32 first; /* first four bytes of data and padding */ } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ u16 check; sp = rxrpc_skb(skb); _enter(""); check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber); data_size |= (u32) check << 16; tmpbuf.hdr.data_size = htonl(data_size); memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first)); /* start the encryption afresh */ memset(&iv, 0, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); memcpy(sechdr, &tmpbuf, sizeof(tmpbuf)); _leave(" = 0"); return 0;}/* * wholly encrypt a packet (level 2 security) */static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, struct sk_buff *skb, u32 data_size, void *sechdr){ const struct rxrpc_key_payload *payload; struct rxkad_level2_hdr rxkhdr __attribute__((aligned(8))); /* must be all on one page */ struct rxrpc_skb_priv *sp; struct blkcipher_desc desc; struct rxrpc_crypt iv; struct scatterlist sg[16]; struct sk_buff *trailer; unsigned len; u16 check; int nsg; sp = rxrpc_skb(skb); _enter(""); check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber); rxkhdr.data_size = htonl(data_size | (u32) check << 16); rxkhdr.checksum = 0; /* encrypt from the session key */ payload = call->conn->key->payload.data; memcpy(&iv, payload->k.session_key, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; sg_init_one(&sg[0], sechdr, sizeof(rxkhdr)); sg_init_one(&sg[1], &rxkhdr, sizeof(rxkhdr)); crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr)); /* we want to encrypt the skbuff in-place */ nsg = skb_cow_data(skb, 0, &trailer); if (nsg < 0 || nsg > 16) return -ENOMEM; len = data_size + call->conn->size_align - 1; len &= ~(call->conn->size_align - 1); sg_init_table(sg, nsg); skb_to_sgvec(skb, sg, 0, len); crypto_blkcipher_encrypt_iv(&desc, sg, sg, len); _leave(" = 0"); return 0;}/* * checksum an RxRPC packet header */static int rxkad_secure_packet(const struct rxrpc_call *call, struct sk_buff *skb, size_t data_size, void *sechdr){ struct rxrpc_skb_priv *sp; struct blkcipher_desc desc; struct rxrpc_crypt iv; struct scatterlist sg[2]; struct { __be32 x[2]; } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ __be32 x; int ret; sp = rxrpc_skb(skb); _enter("{%d{%x}},{#%u},%zu,", call->debug_id, key_serial(call->conn->key), ntohl(sp->hdr.seq), data_size); if (!call->conn->cipher) return 0; ret = key_validate(call->conn->key); if (ret < 0) return ret; /* continue encrypting from where we left off */ memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; /* calculate the security checksum */ x = htonl(call->channel << (32 - RXRPC_CIDSHIFT)); x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff); tmpbuf.x[0] = sp->hdr.callNumber; tmpbuf.x[1] = x; sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); x = ntohl(tmpbuf.x[1]); x = (x >> 16) & 0xffff; if (x == 0) x = 1; /* zero checksums are not permitted */ sp->hdr.cksum = htons(x); switch (call->conn->security_level) { case RXRPC_SECURITY_PLAIN: ret = 0; break; case RXRPC_SECURITY_AUTH: ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr); break; case RXRPC_SECURITY_ENCRYPT: ret = rxkad_secure_packet_encrypt(call, skb, data_size, sechdr); break; default: ret = -EPERM; break; } _leave(" = %d [set %hx]", ret, x); return ret;}/* * decrypt partial encryption on a packet (level 1 security) */static int rxkad_verify_packet_auth(const struct rxrpc_call *call, struct sk_buff *skb, u32 *_abort_code){ struct rxkad_level1_hdr sechdr; struct rxrpc_skb_priv *sp; struct blkcipher_desc desc; struct rxrpc_crypt iv; struct scatterlist sg[16]; struct sk_buff *trailer; u32 data_size, buf; u16 check; int nsg; _enter(""); sp = rxrpc_skb(skb); /* we want to decrypt the skbuff in-place */ nsg = skb_cow_data(skb, 0, &trailer); if (nsg < 0 || nsg > 16) goto nomem; sg_init_table(sg, nsg); skb_to_sgvec(skb, sg, 0, 8); /* start the decryption afresh */ memset(&iv, 0, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; crypto_blkcipher_decrypt_iv(&desc, sg, sg, 8); /* remove the decrypted packet length */ if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) goto datalen_error; if (!skb_pull(skb, sizeof(sechdr))) BUG(); buf = ntohl(sechdr.data_size); data_size = buf & 0xffff; check = buf >> 16; check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber); check &= 0xffff; if (check != 0) { *_abort_code = RXKADSEALEDINCON; goto protocol_error; } /* shorten the packet to remove the padding */ if (data_size > skb->len) goto datalen_error; else if (data_size < skb->len) skb->len = data_size; _leave(" = 0 [dlen=%x]", data_size); return 0;datalen_error: *_abort_code = RXKADDATALEN;protocol_error: _leave(" = -EPROTO"); return -EPROTO;nomem: _leave(" = -ENOMEM"); return -ENOMEM;}/* * wholly decrypt a packet (level 2 security) */static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call, struct sk_buff *skb, u32 *_abort_code){ const struct rxrpc_key_payload *payload; struct rxkad_level2_hdr sechdr; struct rxrpc_skb_priv *sp; struct blkcipher_desc desc; struct rxrpc_crypt iv; struct scatterlist _sg[4], *sg; struct sk_buff *trailer; u32 data_size, buf; u16 check; int nsg; _enter(",{%d}", skb->len); sp = rxrpc_skb(skb); /* we want to decrypt the skbuff in-place */ nsg = skb_cow_data(skb, 0, &trailer); if (nsg < 0) goto nomem; sg = _sg; if (unlikely(nsg > 4)) { sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO); if (!sg) goto nomem; } sg_init_table(sg, nsg); skb_to_sgvec(skb, sg, 0, skb->len); /* decrypt from the session key */ payload = call->conn->key->payload.data; memcpy(&iv, payload->k.session_key, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len); if (sg != _sg) kfree(sg); /* remove the decrypted packet length */ if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) goto datalen_error; if (!skb_pull(skb, sizeof(sechdr))) BUG(); buf = ntohl(sechdr.data_size); data_size = buf & 0xffff; check = buf >> 16; check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber); check &= 0xffff; if (check != 0) { *_abort_code = RXKADSEALEDINCON; goto protocol_error; } /* shorten the packet to remove the padding */ if (data_size > skb->len) goto datalen_error; else if (data_size < skb->len) skb->len = data_size; _leave(" = 0 [dlen=%x]", data_size); return 0;datalen_error: *_abort_code = RXKADDATALEN;protocol_error: _leave(" = -EPROTO"); return -EPROTO;nomem: _leave(" = -ENOMEM"); return -ENOMEM;}/* * verify the security on a received packet */static int rxkad_verify_packet(const struct rxrpc_call *call, struct sk_buff *skb, u32 *_abort_code){ struct blkcipher_desc desc; struct rxrpc_skb_priv *sp; struct rxrpc_crypt iv; struct scatterlist sg[2]; struct { __be32 x[2]; } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ __be32 x; __be16 cksum; int ret; sp = rxrpc_skb(skb); _enter("{%d{%x}},{#%u}", call->debug_id, key_serial(call->conn->key), ntohl(sp->hdr.seq)); if (!call->conn->cipher) return 0; if (sp->hdr.securityIndex != 2) { *_abort_code = RXKADINCONSISTENCY; _leave(" = -EPROTO [not rxkad]"); return -EPROTO; } /* continue encrypting from where we left off */ memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); desc.tfm = call->conn->cipher; desc.info = iv.x; desc.flags = 0; /* validate the security checksum */ x = htonl(call->channel << (32 - RXRPC_CIDSHIFT)); x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff); tmpbuf.x[0] = call->call_id; tmpbuf.x[1] = x; sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); x = ntohl(tmpbuf.x[1]); x = (x >> 16) & 0xffff; if (x == 0) x = 1; /* zero checksums are not permitted */ cksum = htons(x); if (sp->hdr.cksum != cksum) { *_abort_code = RXKADSEALEDINCON; _leave(" = -EPROTO [csum failed]"); return -EPROTO; } switch (call->conn->security_level) { case RXRPC_SECURITY_PLAIN: ret = 0; break; case RXRPC_SECURITY_AUTH: ret = rxkad_verify_packet_auth(call, skb, _abort_code); break; case RXRPC_SECURITY_ENCRYPT: ret = rxkad_verify_packet_encrypt(call, skb, _abort_code); break; default: ret = -ENOANO; break; } _leave(" = %d", ret); return ret;}/* * issue a challenge */static int rxkad_issue_challenge(struct rxrpc_connection *conn){ struct rxkad_challenge challenge; struct rxrpc_header hdr; struct msghdr msg; struct kvec iov[2]; size_t len; int ret; _enter("{%d,%x}", conn->debug_id, key_serial(conn->key)); ret = key_validate(conn->key); if (ret < 0) return ret; get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -