]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sunrpc/auth_gss/gss_krb5_wrap.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-artful-kernel.git] / net / sunrpc / auth_gss / gss_krb5_wrap.c
CommitLineData
14ae162c 1#include <linux/types.h>
14ae162c
BF
2#include <linux/jiffies.h>
3#include <linux/sunrpc/gss_krb5.h>
4#include <linux/random.h>
5#include <linux/pagemap.h>
14ae162c
BF
6#include <linux/crypto.h>
7
8#ifdef RPC_DEBUG
9# define RPCDBG_FACILITY RPCDBG_AUTH
10#endif
11
12static inline int
13gss_krb5_padding(int blocksize, int length)
14{
15 /* Most of the code is block-size independent but currently we
16 * use only 8: */
17 BUG_ON(blocksize != 8);
18 return 8 - (length & 7);
19}
20
21static inline void
22gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
23{
24 int padding = gss_krb5_padding(blocksize, buf->len - offset);
25 char *p;
26 struct kvec *iov;
27
28 if (buf->page_len || buf->tail[0].iov_len)
29 iov = &buf->tail[0];
30 else
31 iov = &buf->head[0];
32 p = iov->iov_base + iov->iov_len;
33 iov->iov_len += padding;
34 buf->len += padding;
35 memset(p, padding, padding);
36}
37
38static inline int
39gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
40{
41 u8 *ptr;
42 u8 pad;
67f97d83 43 size_t len = buf->len;
14ae162c
BF
44
45 if (len <= buf->head[0].iov_len) {
46 pad = *(u8 *)(buf->head[0].iov_base + len - 1);
47 if (pad > buf->head[0].iov_len)
48 return -EINVAL;
49 buf->head[0].iov_len -= pad;
50 goto out;
51 } else
52 len -= buf->head[0].iov_len;
53 if (len <= buf->page_len) {
67f97d83 54 unsigned int last = (buf->page_base + len - 1)
14ae162c 55 >>PAGE_CACHE_SHIFT;
67f97d83 56 unsigned int offset = (buf->page_base + len - 1)
14ae162c 57 & (PAGE_CACHE_SIZE - 1);
87d918d6 58 ptr = kmap_atomic(buf->pages[last], KM_USER0);
14ae162c 59 pad = *(ptr + offset);
87d918d6 60 kunmap_atomic(ptr, KM_USER0);
14ae162c
BF
61 goto out;
62 } else
63 len -= buf->page_len;
64 BUG_ON(len > buf->tail[0].iov_len);
65 pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
66out:
67 /* XXX: NOTE: we do not adjust the page lengths--they represent
68 * a range of data in the real filesystem page cache, and we need
69 * to know that range so the xdr code can properly place read data.
70 * However adjusting the head length, as we do above, is harmless.
71 * In the case of a request that fits into a single page, the server
72 * also uses length and head length together to determine the original
73 * start of the request to copy the request for deferal; so it's
74 * easier on the server if we adjust head and tail length in tandem.
75 * It's not really a problem that we don't fool with the page and
76 * tail lengths, though--at worst badly formed xdr might lead the
77 * server to attempt to parse the padding.
78 * XXX: Document all these weird requirements for gss mechanism
79 * wrap/unwrap functions. */
80 if (pad > blocksize)
81 return -EINVAL;
82 if (buf->len > pad)
83 buf->len -= pad;
84 else
85 return -EINVAL;
86 return 0;
87}
88
863a2488
KC
89static void
90make_confounder(char *p, u32 conflen)
14ae162c
BF
91{
92 static u64 i = 0;
93 u64 *q = (u64 *)p;
94
95 /* rfc1964 claims this should be "random". But all that's really
96 * necessary is that it be unique. And not even that is necessary in
97 * our case since our "gssapi" implementation exists only to support
98 * rpcsec_gss, so we know that the only buffers we will ever encrypt
99 * already begin with a unique sequence number. Just to hedge my bets
100 * I'll make a half-hearted attempt at something unique, but ensuring
101 * uniqueness would mean worrying about atomicity and rollover, and I
102 * don't care enough. */
103
863a2488
KC
104 /* initialize to random value */
105 if (i == 0) {
106 i = random32();
107 i = (i << 32) | random32();
108 }
109
110 switch (conflen) {
111 case 16:
112 *q++ = i++;
113 /* fall through */
114 case 8:
115 *q++ = i++;
116 break;
117 default:
118 BUG();
119 }
14ae162c
BF
120}
121
122/* Assumptions: the head and tail of inbuf are ours to play with.
123 * The pages, however, may be real pages in the page cache and we replace
124 * them with scratch pages from **pages before writing to them. */
125/* XXX: obviously the above should be documentation of wrap interface,
126 * and shouldn't be in this kerberos-specific file. */
127
128/* XXX factor out common code with seal/unseal. */
129
130u32
00fd6e14 131gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
14ae162c
BF
132 struct xdr_buf *buf, struct page **pages)
133{
134 struct krb5_ctx *kctx = ctx->internal_ctx_id;
9e57b302
BF
135 char cksumdata[16];
136 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
14ae162c 137 int blocksize = 0, plainlen;
d00953a5 138 unsigned char *ptr, *msg_start;
14ae162c
BF
139 s32 now;
140 int headlen;
141 struct page **tmp_pages;
eaa82edf 142 u32 seq_send;
14ae162c 143
8885cb36 144 dprintk("RPC: gss_wrap_kerberos\n");
14ae162c
BF
145
146 now = get_seconds();
147
378c6697 148 blocksize = crypto_blkcipher_blocksize(kctx->enc);
14ae162c
BF
149 gss_krb5_add_padding(buf, offset, blocksize);
150 BUG_ON((buf->len - offset) % blocksize);
151 plainlen = blocksize + buf->len - offset;
152
4ab4b0be 153 headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
14ae162c
BF
154 (buf->len - offset);
155
156 ptr = buf->head[0].iov_base + offset;
157 /* shift data to make room for header. */
158 /* XXX Would be cleverer to encrypt while copying. */
159 /* XXX bounds checking, slack, etc. */
160 memmove(ptr + headlen, ptr, buf->head[0].iov_len - offset);
161 buf->head[0].iov_len += headlen;
162 buf->len += headlen;
163 BUG_ON((buf->len - offset - headlen) % blocksize);
164
d00953a5
KC
165 g_make_token_header(&kctx->mech_used,
166 GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
14ae162c
BF
167
168
d00953a5
KC
169 /* ptr now at header described in rfc 1964, section 1.2.1: */
170 ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
171 ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
14ae162c 172
d00953a5 173 msg_start = ptr + 24;
14ae162c 174
d00953a5
KC
175 *(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
176 memset(ptr + 4, 0xff, 4);
177 *(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
14ae162c
BF
178
179 make_confounder(msg_start, blocksize);
180
181 /* XXXJBF: UGH!: */
182 tmp_pages = buf->pages;
183 buf->pages = pages;
d00953a5 184 if (make_checksum("md5", ptr, 8, buf,
14ae162c 185 offset + headlen - blocksize, &md5cksum))
39a21dd1 186 return GSS_S_FAILURE;
14ae162c
BF
187 buf->pages = tmp_pages;
188
e678e06b
BF
189 if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
190 md5cksum.data, md5cksum.len))
39a21dd1 191 return GSS_S_FAILURE;
d00953a5 192 memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
14ae162c 193
eaa82edf
BF
194 spin_lock(&krb5_seq_lock);
195 seq_send = kctx->seq_send++;
196 spin_unlock(&krb5_seq_lock);
197
14ae162c
BF
198 /* XXX would probably be more efficient to compute checksum
199 * and encrypt at the same time: */
200 if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
d00953a5 201 seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
39a21dd1 202 return GSS_S_FAILURE;
14ae162c
BF
203
204 if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
205 pages))
39a21dd1 206 return GSS_S_FAILURE;
14ae162c 207
94efa934 208 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
14ae162c
BF
209}
210
211u32
00fd6e14 212gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
14ae162c
BF
213{
214 struct krb5_ctx *kctx = ctx->internal_ctx_id;
215 int signalg;
216 int sealalg;
9e57b302
BF
217 char cksumdata[16];
218 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
14ae162c
BF
219 s32 now;
220 int direction;
221 s32 seqnum;
222 unsigned char *ptr;
223 int bodysize;
14ae162c
BF
224 void *data_start, *orig_start;
225 int data_len;
226 int blocksize;
227
8885cb36 228 dprintk("RPC: gss_unwrap_kerberos\n");
14ae162c
BF
229
230 ptr = (u8 *)buf->head[0].iov_base + offset;
231 if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
232 buf->len - offset))
39a21dd1 233 return GSS_S_DEFECTIVE_TOKEN;
14ae162c 234
d00953a5
KC
235 if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
236 (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
39a21dd1 237 return GSS_S_DEFECTIVE_TOKEN;
14ae162c
BF
238
239 /* XXX sanity-check bodysize?? */
240
241 /* get the sign and seal algorithms */
242
d00953a5 243 signalg = ptr[2] + (ptr[3] << 8);
94efa934 244 if (signalg != SGN_ALG_DES_MAC_MD5)
39a21dd1 245 return GSS_S_DEFECTIVE_TOKEN;
14ae162c 246
d00953a5 247 sealalg = ptr[4] + (ptr[5] << 8);
d922a84a 248 if (sealalg != SEAL_ALG_DES)
39a21dd1 249 return GSS_S_DEFECTIVE_TOKEN;
94efa934 250
d00953a5 251 if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
39a21dd1 252 return GSS_S_DEFECTIVE_TOKEN;
14ae162c 253
14ae162c 254 if (gss_decrypt_xdr_buf(kctx->enc, buf,
d00953a5 255 ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
39a21dd1 256 return GSS_S_DEFECTIVE_TOKEN;
14ae162c 257
d00953a5
KC
258 if (make_checksum("md5", ptr, 8, buf,
259 ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
39a21dd1 260 return GSS_S_FAILURE;
5eb064f9 261
39a21dd1
BF
262 if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
263 md5cksum.data, md5cksum.len))
264 return GSS_S_FAILURE;
14ae162c 265
d00953a5 266 if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
39a21dd1 267 return GSS_S_BAD_SIG;
14ae162c
BF
268
269 /* it got through unscathed. Make sure the context is unexpired */
270
14ae162c
BF
271 now = get_seconds();
272
14ae162c 273 if (now > kctx->endtime)
39a21dd1 274 return GSS_S_CONTEXT_EXPIRED;
14ae162c
BF
275
276 /* do sequencing checks */
277
d00953a5
KC
278 if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
279 &direction, &seqnum))
39a21dd1 280 return GSS_S_BAD_SIG;
14ae162c
BF
281
282 if ((kctx->initiate && direction != 0xff) ||
283 (!kctx->initiate && direction != 0))
39a21dd1 284 return GSS_S_BAD_SIG;
14ae162c
BF
285
286 /* Copy the data back to the right position. XXX: Would probably be
287 * better to copy and encrypt at the same time. */
288
378c6697 289 blocksize = crypto_blkcipher_blocksize(kctx->enc);
d00953a5 290 data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
14ae162c
BF
291 orig_start = buf->head[0].iov_base + offset;
292 data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
293 memmove(orig_start, data_start, data_len);
294 buf->head[0].iov_len -= (data_start - orig_start);
295 buf->len -= (data_start - orig_start);
296
14ae162c 297 if (gss_krb5_remove_padding(buf, blocksize))
39a21dd1 298 return GSS_S_DEFECTIVE_TOKEN;
14ae162c 299
39a21dd1 300 return GSS_S_COMPLETE;
14ae162c 301}