Merge branch 'x86-pat-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / net / sunrpc / auth_gss / gss_krb5_wrap.c
1 #include <linux/types.h>
2 #include <linux/jiffies.h>
3 #include <linux/sunrpc/gss_krb5.h>
4 #include <linux/random.h>
5 #include <linux/pagemap.h>
6 #include <linux/crypto.h>
7
8 #ifdef RPC_DEBUG
9 # define RPCDBG_FACILITY        RPCDBG_AUTH
10 #endif
11
12 static inline int
13 gss_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
21 static inline void
22 gss_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
38 static inline int
39 gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
40 {
41         u8 *ptr;
42         u8 pad;
43         size_t len = buf->len;
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) {
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                 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);
66 out:
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
89 static void
90 make_confounder(char *p, u32 conflen)
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
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         }
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
130 u32
131 gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
132                 struct xdr_buf *buf, struct page **pages)
133 {
134         struct krb5_ctx         *kctx = ctx->internal_ctx_id;
135         char                    cksumdata[16];
136         struct xdr_netobj       md5cksum = {.len = 0, .data = cksumdata};
137         int                     blocksize = 0, plainlen;
138         unsigned char           *ptr, *msg_start;
139         s32                     now;
140         int                     headlen;
141         struct page             **tmp_pages;
142         u32                     seq_send;
143
144         dprintk("RPC:       gss_wrap_kerberos\n");
145
146         now = get_seconds();
147
148         blocksize = crypto_blkcipher_blocksize(kctx->enc);
149         gss_krb5_add_padding(buf, offset, blocksize);
150         BUG_ON((buf->len - offset) % blocksize);
151         plainlen = blocksize + buf->len - offset;
152
153         headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
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
165         g_make_token_header(&kctx->mech_used,
166                                 GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
167
168
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);
172
173         msg_start = ptr + 24;
174
175         *(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
176         memset(ptr + 4, 0xff, 4);
177         *(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
178
179         make_confounder(msg_start, blocksize);
180
181         /* XXXJBF: UGH!: */
182         tmp_pages = buf->pages;
183         buf->pages = pages;
184         if (make_checksum("md5", ptr, 8, buf,
185                                 offset + headlen - blocksize, &md5cksum))
186                 return GSS_S_FAILURE;
187         buf->pages = tmp_pages;
188
189         if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
190                           md5cksum.data, md5cksum.len))
191                 return GSS_S_FAILURE;
192         memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
193
194         spin_lock(&krb5_seq_lock);
195         seq_send = kctx->seq_send++;
196         spin_unlock(&krb5_seq_lock);
197
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,
201                                seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
202                 return GSS_S_FAILURE;
203
204         if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
205                                                                         pages))
206                 return GSS_S_FAILURE;
207
208         return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
209 }
210
211 u32
212 gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
213 {
214         struct krb5_ctx         *kctx = ctx->internal_ctx_id;
215         int                     signalg;
216         int                     sealalg;
217         char                    cksumdata[16];
218         struct xdr_netobj       md5cksum = {.len = 0, .data = cksumdata};
219         s32                     now;
220         int                     direction;
221         s32                     seqnum;
222         unsigned char           *ptr;
223         int                     bodysize;
224         void                    *data_start, *orig_start;
225         int                     data_len;
226         int                     blocksize;
227
228         dprintk("RPC:       gss_unwrap_kerberos\n");
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))
233                 return GSS_S_DEFECTIVE_TOKEN;
234
235         if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
236             (ptr[1] !=  (KG_TOK_WRAP_MSG & 0xff)))
237                 return GSS_S_DEFECTIVE_TOKEN;
238
239         /* XXX sanity-check bodysize?? */
240
241         /* get the sign and seal algorithms */
242
243         signalg = ptr[2] + (ptr[3] << 8);
244         if (signalg != SGN_ALG_DES_MAC_MD5)
245                 return GSS_S_DEFECTIVE_TOKEN;
246
247         sealalg = ptr[4] + (ptr[5] << 8);
248         if (sealalg != SEAL_ALG_DES)
249                 return GSS_S_DEFECTIVE_TOKEN;
250
251         if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
252                 return GSS_S_DEFECTIVE_TOKEN;
253
254         if (gss_decrypt_xdr_buf(kctx->enc, buf,
255                         ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
256                 return GSS_S_DEFECTIVE_TOKEN;
257
258         if (make_checksum("md5", ptr, 8, buf,
259                  ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
260                 return GSS_S_FAILURE;
261
262         if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
263                            md5cksum.data, md5cksum.len))
264                 return GSS_S_FAILURE;
265
266         if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
267                 return GSS_S_BAD_SIG;
268
269         /* it got through unscathed.  Make sure the context is unexpired */
270
271         now = get_seconds();
272
273         if (now > kctx->endtime)
274                 return GSS_S_CONTEXT_EXPIRED;
275
276         /* do sequencing checks */
277
278         if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
279                                     &direction, &seqnum))
280                 return GSS_S_BAD_SIG;
281
282         if ((kctx->initiate && direction != 0xff) ||
283             (!kctx->initiate && direction != 0))
284                 return GSS_S_BAD_SIG;
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
289         blocksize = crypto_blkcipher_blocksize(kctx->enc);
290         data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
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
297         if (gss_krb5_remove_padding(buf, blocksize))
298                 return GSS_S_DEFECTIVE_TOKEN;
299
300         return GSS_S_COMPLETE;
301 }