blob: 206937187274102fc6918514cd103688953cbf2d [file] [log] [blame]
J. Bruce Fields14ae1622005-10-13 16:55:13 -04001#include <linux/types.h>
2#include <linux/slab.h>
3#include <linux/jiffies.h>
4#include <linux/sunrpc/gss_krb5.h>
5#include <linux/random.h>
6#include <linux/pagemap.h>
7#include <asm/scatterlist.h>
8#include <linux/crypto.h>
9
10#ifdef RPC_DEBUG
11# define RPCDBG_FACILITY RPCDBG_AUTH
12#endif
13
14static inline int
15gss_krb5_padding(int blocksize, int length)
16{
17 /* Most of the code is block-size independent but currently we
18 * use only 8: */
19 BUG_ON(blocksize != 8);
20 return 8 - (length & 7);
21}
22
23static inline void
24gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
25{
26 int padding = gss_krb5_padding(blocksize, buf->len - offset);
27 char *p;
28 struct kvec *iov;
29
30 if (buf->page_len || buf->tail[0].iov_len)
31 iov = &buf->tail[0];
32 else
33 iov = &buf->head[0];
34 p = iov->iov_base + iov->iov_len;
35 iov->iov_len += padding;
36 buf->len += padding;
37 memset(p, padding, padding);
38}
39
40static inline int
41gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
42{
43 u8 *ptr;
44 u8 pad;
45 int len = buf->len;
46
47 if (len <= buf->head[0].iov_len) {
48 pad = *(u8 *)(buf->head[0].iov_base + len - 1);
49 if (pad > buf->head[0].iov_len)
50 return -EINVAL;
51 buf->head[0].iov_len -= pad;
52 goto out;
53 } else
54 len -= buf->head[0].iov_len;
55 if (len <= buf->page_len) {
56 int last = (buf->page_base + len - 1)
57 >>PAGE_CACHE_SHIFT;
58 int offset = (buf->page_base + len - 1)
59 & (PAGE_CACHE_SIZE - 1);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050060 ptr = kmap_atomic(buf->pages[last], KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040061 pad = *(ptr + offset);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050062 kunmap_atomic(ptr, KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040063 goto out;
64 } else
65 len -= buf->page_len;
66 BUG_ON(len > buf->tail[0].iov_len);
67 pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
68out:
69 /* XXX: NOTE: we do not adjust the page lengths--they represent
70 * a range of data in the real filesystem page cache, and we need
71 * to know that range so the xdr code can properly place read data.
72 * However adjusting the head length, as we do above, is harmless.
73 * In the case of a request that fits into a single page, the server
74 * also uses length and head length together to determine the original
75 * start of the request to copy the request for deferal; so it's
76 * easier on the server if we adjust head and tail length in tandem.
77 * It's not really a problem that we don't fool with the page and
78 * tail lengths, though--at worst badly formed xdr might lead the
79 * server to attempt to parse the padding.
80 * XXX: Document all these weird requirements for gss mechanism
81 * wrap/unwrap functions. */
82 if (pad > blocksize)
83 return -EINVAL;
84 if (buf->len > pad)
85 buf->len -= pad;
86 else
87 return -EINVAL;
88 return 0;
89}
90
91static inline void
92make_confounder(char *p, int blocksize)
93{
94 static u64 i = 0;
95 u64 *q = (u64 *)p;
96
97 /* rfc1964 claims this should be "random". But all that's really
98 * necessary is that it be unique. And not even that is necessary in
99 * our case since our "gssapi" implementation exists only to support
100 * rpcsec_gss, so we know that the only buffers we will ever encrypt
101 * already begin with a unique sequence number. Just to hedge my bets
102 * I'll make a half-hearted attempt at something unique, but ensuring
103 * uniqueness would mean worrying about atomicity and rollover, and I
104 * don't care enough. */
105
106 BUG_ON(blocksize != 8);
107 *q = i++;
108}
109
110/* Assumptions: the head and tail of inbuf are ours to play with.
111 * The pages, however, may be real pages in the page cache and we replace
112 * them with scratch pages from **pages before writing to them. */
113/* XXX: obviously the above should be documentation of wrap interface,
114 * and shouldn't be in this kerberos-specific file. */
115
116/* XXX factor out common code with seal/unseal. */
117
118u32
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400119gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400120 struct xdr_buf *buf, struct page **pages)
121{
122 struct krb5_ctx *kctx = ctx->internal_ctx_id;
J. Bruce Fields9e57b302006-03-20 23:23:11 -0500123 char cksumdata[16];
124 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400125 int blocksize = 0, plainlen;
126 unsigned char *ptr, *krb5_hdr, *msg_start;
127 s32 now;
128 int headlen;
129 struct page **tmp_pages;
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500130 u32 seq_send;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400131
132 dprintk("RPC: gss_wrap_kerberos\n");
133
134 now = get_seconds();
135
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400136 if (kctx->sealalg != SEAL_ALG_NONE && kctx->sealalg != SEAL_ALG_DES) {
137 dprintk("RPC: gss_krb5_seal: kctx->sealalg %d not supported\n",
138 kctx->sealalg);
139 goto out_err;
140 }
141
Herbert Xu378c6692006-08-22 20:33:54 +1000142 blocksize = crypto_blkcipher_blocksize(kctx->enc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400143 gss_krb5_add_padding(buf, offset, blocksize);
144 BUG_ON((buf->len - offset) % blocksize);
145 plainlen = blocksize + buf->len - offset;
146
147 headlen = g_token_size(&kctx->mech_used, 22 + plainlen) -
148 (buf->len - offset);
149
150 ptr = buf->head[0].iov_base + offset;
151 /* shift data to make room for header. */
152 /* XXX Would be cleverer to encrypt while copying. */
153 /* XXX bounds checking, slack, etc. */
154 memmove(ptr + headlen, ptr, buf->head[0].iov_len - offset);
155 buf->head[0].iov_len += headlen;
156 buf->len += headlen;
157 BUG_ON((buf->len - offset - headlen) % blocksize);
158
159 g_make_token_header(&kctx->mech_used, 22 + plainlen, &ptr);
160
161
162 *ptr++ = (unsigned char) ((KG_TOK_WRAP_MSG>>8)&0xff);
163 *ptr++ = (unsigned char) (KG_TOK_WRAP_MSG&0xff);
164
165 /* ptr now at byte 2 of header described in rfc 1964, section 1.2.1: */
166 krb5_hdr = ptr - 2;
167 msg_start = krb5_hdr + 24;
168 /* XXXJBF: */ BUG_ON(buf->head[0].iov_base + offset + headlen != msg_start + blocksize);
169
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500170 *(__be16 *)(krb5_hdr + 2) = htons(SGN_ALG_DES_MAC_MD5);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400171 memset(krb5_hdr + 4, 0xff, 4);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700172 *(__be16 *)(krb5_hdr + 4) = htons(kctx->sealalg);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400173
174 make_confounder(msg_start, blocksize);
175
176 /* XXXJBF: UGH!: */
177 tmp_pages = buf->pages;
178 buf->pages = pages;
J. Bruce Fields2818bf82006-12-04 20:22:37 -0500179 if (make_checksum(CKSUMTYPE_RSA_MD5, krb5_hdr, 8, buf,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400180 offset + headlen - blocksize, &md5cksum))
181 goto out_err;
182 buf->pages = tmp_pages;
183
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500184 if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
185 md5cksum.data, md5cksum.len))
186 goto out_err;
187 memcpy(krb5_hdr + 16,
188 md5cksum.data + md5cksum.len - KRB5_CKSUM_LENGTH,
189 KRB5_CKSUM_LENGTH);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400190
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500191 spin_lock(&krb5_seq_lock);
192 seq_send = kctx->seq_send++;
193 spin_unlock(&krb5_seq_lock);
194
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400195 /* XXX would probably be more efficient to compute checksum
196 * and encrypt at the same time: */
197 if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500198 seq_send, krb5_hdr + 16, krb5_hdr + 8)))
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400199 goto out_err;
200
201 if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
202 pages))
203 goto out_err;
204
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400205 return ((kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE);
206out_err:
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400207 return GSS_S_FAILURE;
208}
209
210u32
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400211gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400212{
213 struct krb5_ctx *kctx = ctx->internal_ctx_id;
214 int signalg;
215 int sealalg;
J. Bruce Fields9e57b302006-03-20 23:23:11 -0500216 char cksumdata[16];
217 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400218 s32 now;
219 int direction;
220 s32 seqnum;
221 unsigned char *ptr;
222 int bodysize;
223 u32 ret = GSS_S_DEFECTIVE_TOKEN;
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 goto out;
234
235 if ((*ptr++ != ((KG_TOK_WRAP_MSG>>8)&0xff)) ||
236 (*ptr++ != (KG_TOK_WRAP_MSG &0xff)) )
237 goto out;
238
239 /* XXX sanity-check bodysize?? */
240
241 /* get the sign and seal algorithms */
242
243 signalg = ptr[0] + (ptr[1] << 8);
244 sealalg = ptr[2] + (ptr[3] << 8);
245
246 /* Sanity checks */
247
248 if ((ptr[4] != 0xff) || (ptr[5] != 0xff))
249 goto out;
250
251 if (sealalg == 0xffff)
252 goto out;
J. Bruce Fields5eb064f2006-12-04 20:22:36 -0500253 if (signalg != SGN_ALG_DES_MAC_MD5)
254 goto out;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400255
256 /* in the current spec, there is only one valid seal algorithm per
257 key type, so a simple comparison is ok */
258
259 if (sealalg != kctx->sealalg)
260 goto out;
261
262 /* there are several mappings of seal algorithms to sign algorithms,
263 but few enough that we can try them all. */
264
265 if ((kctx->sealalg == SEAL_ALG_NONE && signalg > 1) ||
266 (kctx->sealalg == SEAL_ALG_1 && signalg != SGN_ALG_3) ||
267 (kctx->sealalg == SEAL_ALG_DES3KD &&
268 signalg != SGN_ALG_HMAC_SHA1_DES3_KD))
269 goto out;
270
271 if (gss_decrypt_xdr_buf(kctx->enc, buf,
272 ptr + 22 - (unsigned char *)buf->head[0].iov_base))
273 goto out;
274
J. Bruce Fields2818bf82006-12-04 20:22:37 -0500275 ret = make_checksum(CKSUMTYPE_RSA_MD5, ptr - 2, 8, buf,
J. Bruce Fields5eb064f2006-12-04 20:22:36 -0500276 ptr + 22 - (unsigned char *)buf->head[0].iov_base, &md5cksum);
277 if (ret)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400278 goto out;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400279
J. Bruce Fields5eb064f2006-12-04 20:22:36 -0500280 ret = krb5_encrypt(kctx->seq, NULL, md5cksum.data,
281 md5cksum.data, md5cksum.len);
282 if (ret)
283 goto out;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400284
J. Bruce Fields5eb064f2006-12-04 20:22:36 -0500285 if (memcmp(md5cksum.data + 8, ptr + 14, 8)) {
286 ret = GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400287 goto out;
288 }
289
290 /* it got through unscathed. Make sure the context is unexpired */
291
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400292 now = get_seconds();
293
294 ret = GSS_S_CONTEXT_EXPIRED;
295 if (now > kctx->endtime)
296 goto out;
297
298 /* do sequencing checks */
299
300 ret = GSS_S_BAD_SIG;
301 if ((ret = krb5_get_seq_num(kctx->seq, ptr + 14, ptr + 6, &direction,
302 &seqnum)))
303 goto out;
304
305 if ((kctx->initiate && direction != 0xff) ||
306 (!kctx->initiate && direction != 0))
307 goto out;
308
309 /* Copy the data back to the right position. XXX: Would probably be
310 * better to copy and encrypt at the same time. */
311
Herbert Xu378c6692006-08-22 20:33:54 +1000312 blocksize = crypto_blkcipher_blocksize(kctx->enc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400313 data_start = ptr + 22 + blocksize;
314 orig_start = buf->head[0].iov_base + offset;
315 data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
316 memmove(orig_start, data_start, data_len);
317 buf->head[0].iov_len -= (data_start - orig_start);
318 buf->len -= (data_start - orig_start);
319
320 ret = GSS_S_DEFECTIVE_TOKEN;
321 if (gss_krb5_remove_padding(buf, blocksize))
322 goto out;
323
324 ret = GSS_S_COMPLETE;
325out:
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400326 return ret;
327}