gss_krb5_wrap.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <linux/types.h>
#include <linux/jiffies.h>
#include <linux/sunrpc/gss_krb5.h>
#include <linux/random.h>
#include <linux/pagemap.h>
#include <linux/crypto.h>

#ifdef RPC_DEBUG
# define RPCDBG_FACILITY	RPCDBG_AUTH
#endif

static inline int
gss_krb5_padding(int blocksize, int length)
{
	/* Most of the code is block-size independent but currently we
	 * use only 8: */
	BUG_ON(blocksize != 8);
	return 8 - (length & 7);
}

static inline void
gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
{
	int padding = gss_krb5_padding(blocksize, buf->len - offset);
	char *p;
	struct kvec *iov;

	if (buf->page_len || buf->tail[0].iov_len)
		iov = &buf->tail[0];
	else
		iov = &buf->head[0];
	p = iov->iov_base + iov->iov_len;
	iov->iov_len += padding;
	buf->len += padding;
	memset(p, padding, padding);
}

static inline int
gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
{
	u8 *ptr;
	u8 pad;
43
	size_t len = buf->len;
44 45 46 47 48 49 50 51 52 53

	if (len <= buf->head[0].iov_len) {
		pad = *(u8 *)(buf->head[0].iov_base + len - 1);
		if (pad > buf->head[0].iov_len)
			return -EINVAL;
		buf->head[0].iov_len -= pad;
		goto out;
	} else
		len -= buf->head[0].iov_len;
	if (len <= buf->page_len) {
54
		unsigned int last = (buf->page_base + len - 1)
55
					>>PAGE_CACHE_SHIFT;
56
		unsigned int offset = (buf->page_base + len - 1)
57
					& (PAGE_CACHE_SIZE - 1);
58
		ptr = kmap_atomic(buf->pages[last], KM_USER0);
59
		pad = *(ptr + offset);
60
		kunmap_atomic(ptr, KM_USER0);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		goto out;
	} else
		len -= buf->page_len;
	BUG_ON(len > buf->tail[0].iov_len);
	pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
out:
	/* XXX: NOTE: we do not adjust the page lengths--they represent
	 * a range of data in the real filesystem page cache, and we need
	 * to know that range so the xdr code can properly place read data.
	 * However adjusting the head length, as we do above, is harmless.
	 * In the case of a request that fits into a single page, the server
	 * also uses length and head length together to determine the original
	 * start of the request to copy the request for deferal; so it's
	 * easier on the server if we adjust head and tail length in tandem.
	 * It's not really a problem that we don't fool with the page and
	 * tail lengths, though--at worst badly formed xdr might lead the
	 * server to attempt to parse the padding.
	 * XXX: Document all these weird requirements for gss mechanism
	 * wrap/unwrap functions. */
	if (pad > blocksize)
		return -EINVAL;
	if (buf->len > pad)
		buf->len -= pad;
	else
		return -EINVAL;
	return 0;
}

89 90
static void
make_confounder(char *p, u32 conflen)
91 92 93 94 95 96 97 98 99 100 101 102 103
{
	static u64 i = 0;
	u64 *q = (u64 *)p;

	/* rfc1964 claims this should be "random".  But all that's really
	 * necessary is that it be unique.  And not even that is necessary in
	 * our case since our "gssapi" implementation exists only to support
	 * rpcsec_gss, so we know that the only buffers we will ever encrypt
	 * already begin with a unique sequence number.  Just to hedge my bets
	 * I'll make a half-hearted attempt at something unique, but ensuring
	 * uniqueness would mean worrying about atomicity and rollover, and I
	 * don't care enough. */

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	/* initialize to random value */
	if (i == 0) {
		i = random32();
		i = (i << 32) | random32();
	}

	switch (conflen) {
	case 16:
		*q++ = i++;
		/* fall through */
	case 8:
		*q++ = i++;
		break;
	default:
		BUG();
	}
120 121 122 123 124 125 126 127 128 129 130
}

/* Assumptions: the head and tail of inbuf are ours to play with.
 * The pages, however, may be real pages in the page cache and we replace
 * them with scratch pages from **pages before writing to them. */
/* XXX: obviously the above should be documentation of wrap interface,
 * and shouldn't be in this kerberos-specific file. */

/* XXX factor out common code with seal/unseal. */

u32
131
gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
132 133 134
		struct xdr_buf *buf, struct page **pages)
{
	struct krb5_ctx		*kctx = ctx->internal_ctx_id;
135 136
	char			cksumdata[16];
	struct xdr_netobj	md5cksum = {.len = 0, .data = cksumdata};
137
	int			blocksize = 0, plainlen;
138
	unsigned char		*ptr, *msg_start;
139 140 141
	s32			now;
	int			headlen;
	struct page		**tmp_pages;
142
	u32			seq_send;
143

144
	dprintk("RPC:       gss_wrap_kerberos\n");
145 146 147

	now = get_seconds();

148
	blocksize = crypto_blkcipher_blocksize(kctx->enc);
149 150 151 152
	gss_krb5_add_padding(buf, offset, blocksize);
	BUG_ON((buf->len - offset) % blocksize);
	plainlen = blocksize + buf->len - offset;

153
	headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
154 155 156 157
						(buf->len - offset);

	ptr = buf->head[0].iov_base + offset;
	/* shift data to make room for header. */
158 159
	xdr_extend_head(buf, offset, headlen);

160 161 162
	/* XXX Would be cleverer to encrypt while copying. */
	BUG_ON((buf->len - offset - headlen) % blocksize);

163 164
	g_make_token_header(&kctx->mech_used,
				GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
165 166


167 168 169
	/* ptr now at header described in rfc 1964, section 1.2.1: */
	ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
	ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
170

171
	msg_start = ptr + 24;
172

173 174 175
	*(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
	memset(ptr + 4, 0xff, 4);
	*(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
176 177 178 179 180 181

	make_confounder(msg_start, blocksize);

	/* XXXJBF: UGH!: */
	tmp_pages = buf->pages;
	buf->pages = pages;
182
	if (make_checksum("md5", ptr, 8, buf,
183
				offset + headlen - blocksize, &md5cksum))
184
		return GSS_S_FAILURE;
185 186
	buf->pages = tmp_pages;

187 188
	if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
			  md5cksum.data, md5cksum.len))
189
		return GSS_S_FAILURE;
190
	memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
191

192 193 194 195
	spin_lock(&krb5_seq_lock);
	seq_send = kctx->seq_send++;
	spin_unlock(&krb5_seq_lock);

196 197 198
	/* XXX would probably be more efficient to compute checksum
	 * and encrypt at the same time: */
	if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
199
			       seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
200
		return GSS_S_FAILURE;
201 202 203

	if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
									pages))
204
		return GSS_S_FAILURE;
205

206
	return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
207 208 209
}

u32
210
gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
211 212 213 214
{
	struct krb5_ctx		*kctx = ctx->internal_ctx_id;
	int			signalg;
	int			sealalg;
215 216
	char			cksumdata[16];
	struct xdr_netobj	md5cksum = {.len = 0, .data = cksumdata};
217 218 219 220 221 222 223 224 225
	s32			now;
	int			direction;
	s32			seqnum;
	unsigned char		*ptr;
	int			bodysize;
	void			*data_start, *orig_start;
	int			data_len;
	int			blocksize;

226
	dprintk("RPC:       gss_unwrap_kerberos\n");
227 228 229 230

	ptr = (u8 *)buf->head[0].iov_base + offset;
	if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
					buf->len - offset))
231
		return GSS_S_DEFECTIVE_TOKEN;
232

233 234
	if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
	    (ptr[1] !=  (KG_TOK_WRAP_MSG & 0xff)))
235
		return GSS_S_DEFECTIVE_TOKEN;
236 237 238 239 240

	/* XXX sanity-check bodysize?? */

	/* get the sign and seal algorithms */

241
	signalg = ptr[2] + (ptr[3] << 8);
242
	if (signalg != SGN_ALG_DES_MAC_MD5)
243
		return GSS_S_DEFECTIVE_TOKEN;
244

245
	sealalg = ptr[4] + (ptr[5] << 8);
246
	if (sealalg != SEAL_ALG_DES)
247
		return GSS_S_DEFECTIVE_TOKEN;
248

249
	if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
250
		return GSS_S_DEFECTIVE_TOKEN;
251 252

	if (gss_decrypt_xdr_buf(kctx->enc, buf,
253
			ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
254
		return GSS_S_DEFECTIVE_TOKEN;
255

256 257
	if (make_checksum("md5", ptr, 8, buf,
		 ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
258
		return GSS_S_FAILURE;
259

260 261 262
	if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
			   md5cksum.data, md5cksum.len))
		return GSS_S_FAILURE;
263

264
	if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
265
		return GSS_S_BAD_SIG;
266 267 268 269 270 271

	/* it got through unscathed.  Make sure the context is unexpired */

	now = get_seconds();

	if (now > kctx->endtime)
272
		return GSS_S_CONTEXT_EXPIRED;
273 274 275

	/* do sequencing checks */

276 277
	if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
				    &direction, &seqnum))
278
		return GSS_S_BAD_SIG;
279 280 281

	if ((kctx->initiate && direction != 0xff) ||
	    (!kctx->initiate && direction != 0))
282
		return GSS_S_BAD_SIG;
283 284 285 286

	/* Copy the data back to the right position.  XXX: Would probably be
	 * better to copy and encrypt at the same time. */

287
	blocksize = crypto_blkcipher_blocksize(kctx->enc);
288
	data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
289 290 291 292 293 294 295
	orig_start = buf->head[0].iov_base + offset;
	data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
	memmove(orig_start, data_start, data_len);
	buf->head[0].iov_len -= (data_start - orig_start);
	buf->len -= (data_start - orig_start);

	if (gss_krb5_remove_padding(buf, blocksize))
296
		return GSS_S_DEFECTIVE_TOKEN;
297

298
	return GSS_S_COMPLETE;
299
}