blob: 383db891c8350eb42178099773e8df536b425e5d [file] [log] [blame]
Kevin Coffman81d4a432010-03-17 13:02:51 -04001/*
2 * COPYRIGHT (c) 2008
3 * The Regents of the University of Michigan
4 * ALL RIGHTS RESERVED
5 *
6 * Permission is granted to use, copy, create derivative works
7 * and redistribute this software and such derivative works
8 * for any purpose, so long as the name of The University of
9 * Michigan is not used in any advertising or publicity
10 * pertaining to the use of distribution of this software
11 * without specific, written prior authorization. If the
12 * above copyright notice or any other identification of the
13 * University of Michigan is included in any copy of any
14 * portion of this software, then the disclaimer below must
15 * also be included.
16 *
17 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGES.
29 */
30
J. Bruce Fields14ae1622005-10-13 16:55:13 -040031#include <linux/types.h>
J. Bruce Fields14ae1622005-10-13 16:55:13 -040032#include <linux/jiffies.h>
33#include <linux/sunrpc/gss_krb5.h>
34#include <linux/random.h>
35#include <linux/pagemap.h>
J. Bruce Fields14ae1622005-10-13 16:55:13 -040036#include <linux/crypto.h>
37
38#ifdef RPC_DEBUG
39# define RPCDBG_FACILITY RPCDBG_AUTH
40#endif
41
42static inline int
43gss_krb5_padding(int blocksize, int length)
44{
J. Bruce Fields54ec3d42010-03-17 13:02:48 -040045 return blocksize - (length % blocksize);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040046}
47
48static inline void
49gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
50{
51 int padding = gss_krb5_padding(blocksize, buf->len - offset);
52 char *p;
53 struct kvec *iov;
54
55 if (buf->page_len || buf->tail[0].iov_len)
56 iov = &buf->tail[0];
57 else
58 iov = &buf->head[0];
59 p = iov->iov_base + iov->iov_len;
60 iov->iov_len += padding;
61 buf->len += padding;
62 memset(p, padding, padding);
63}
64
65static inline int
66gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
67{
68 u8 *ptr;
69 u8 pad;
Chuck Lever67f97d82007-09-26 14:38:10 -040070 size_t len = buf->len;
J. Bruce Fields14ae1622005-10-13 16:55:13 -040071
72 if (len <= buf->head[0].iov_len) {
73 pad = *(u8 *)(buf->head[0].iov_base + len - 1);
74 if (pad > buf->head[0].iov_len)
75 return -EINVAL;
76 buf->head[0].iov_len -= pad;
77 goto out;
78 } else
79 len -= buf->head[0].iov_len;
80 if (len <= buf->page_len) {
Chuck Lever67f97d82007-09-26 14:38:10 -040081 unsigned int last = (buf->page_base + len - 1)
J. Bruce Fields14ae1622005-10-13 16:55:13 -040082 >>PAGE_CACHE_SHIFT;
Chuck Lever67f97d82007-09-26 14:38:10 -040083 unsigned int offset = (buf->page_base + len - 1)
J. Bruce Fields14ae1622005-10-13 16:55:13 -040084 & (PAGE_CACHE_SIZE - 1);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050085 ptr = kmap_atomic(buf->pages[last], KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040086 pad = *(ptr + offset);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050087 kunmap_atomic(ptr, KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040088 goto out;
89 } else
90 len -= buf->page_len;
91 BUG_ON(len > buf->tail[0].iov_len);
92 pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
93out:
94 /* XXX: NOTE: we do not adjust the page lengths--they represent
95 * a range of data in the real filesystem page cache, and we need
96 * to know that range so the xdr code can properly place read data.
97 * However adjusting the head length, as we do above, is harmless.
98 * In the case of a request that fits into a single page, the server
99 * also uses length and head length together to determine the original
100 * start of the request to copy the request for deferal; so it's
101 * easier on the server if we adjust head and tail length in tandem.
102 * It's not really a problem that we don't fool with the page and
103 * tail lengths, though--at worst badly formed xdr might lead the
104 * server to attempt to parse the padding.
105 * XXX: Document all these weird requirements for gss mechanism
106 * wrap/unwrap functions. */
107 if (pad > blocksize)
108 return -EINVAL;
109 if (buf->len > pad)
110 buf->len -= pad;
111 else
112 return -EINVAL;
113 return 0;
114}
115
Kevin Coffman934a95a2010-03-17 13:03:00 -0400116void
117gss_krb5_make_confounder(char *p, u32 conflen)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400118{
119 static u64 i = 0;
120 u64 *q = (u64 *)p;
121
122 /* rfc1964 claims this should be "random". But all that's really
123 * necessary is that it be unique. And not even that is necessary in
124 * our case since our "gssapi" implementation exists only to support
125 * rpcsec_gss, so we know that the only buffers we will ever encrypt
126 * already begin with a unique sequence number. Just to hedge my bets
127 * I'll make a half-hearted attempt at something unique, but ensuring
128 * uniqueness would mean worrying about atomicity and rollover, and I
129 * don't care enough. */
130
Kevin Coffman863a2482008-04-30 12:46:08 -0400131 /* initialize to random value */
132 if (i == 0) {
133 i = random32();
134 i = (i << 32) | random32();
135 }
136
137 switch (conflen) {
138 case 16:
139 *q++ = i++;
140 /* fall through */
141 case 8:
142 *q++ = i++;
143 break;
144 default:
145 BUG();
146 }
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400147}
148
149/* Assumptions: the head and tail of inbuf are ours to play with.
150 * The pages, however, may be real pages in the page cache and we replace
151 * them with scratch pages from **pages before writing to them. */
152/* XXX: obviously the above should be documentation of wrap interface,
153 * and shouldn't be in this kerberos-specific file. */
154
155/* XXX factor out common code with seal/unseal. */
156
Kevin Coffman1ac37192010-03-17 13:02:49 -0400157static u32
158gss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400159 struct xdr_buf *buf, struct page **pages)
160{
Kevin Coffman81d4a432010-03-17 13:02:51 -0400161 char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
162 struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
163 .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400164 int blocksize = 0, plainlen;
Kevin Coffmand00953a2008-04-30 12:45:53 -0400165 unsigned char *ptr, *msg_start;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400166 s32 now;
167 int headlen;
168 struct page **tmp_pages;
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500169 u32 seq_send;
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400170 u8 *cksumkey;
Kevin Coffman5af46542010-03-17 13:03:05 -0400171 u32 conflen = kctx->gk5e->conflen;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400172
Kevin Coffman81d4a432010-03-17 13:02:51 -0400173 dprintk("RPC: %s\n", __func__);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400174
175 now = get_seconds();
176
Herbert Xu378c6692006-08-22 20:33:54 +1000177 blocksize = crypto_blkcipher_blocksize(kctx->enc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400178 gss_krb5_add_padding(buf, offset, blocksize);
179 BUG_ON((buf->len - offset) % blocksize);
Kevin Coffman5af46542010-03-17 13:03:05 -0400180 plainlen = conflen + buf->len - offset;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400181
Kevin Coffman81d4a432010-03-17 13:02:51 -0400182 headlen = g_token_size(&kctx->mech_used,
183 GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) -
184 (buf->len - offset);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400185
186 ptr = buf->head[0].iov_base + offset;
187 /* shift data to make room for header. */
Kevin Coffman725f2862010-03-17 13:02:46 -0400188 xdr_extend_head(buf, offset, headlen);
189
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400190 /* XXX Would be cleverer to encrypt while copying. */
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400191 BUG_ON((buf->len - offset - headlen) % blocksize);
192
Kevin Coffmand00953a2008-04-30 12:45:53 -0400193 g_make_token_header(&kctx->mech_used,
Kevin Coffman81d4a432010-03-17 13:02:51 -0400194 GSS_KRB5_TOK_HDR_LEN +
195 kctx->gk5e->cksumlength + plainlen, &ptr);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400196
197
Kevin Coffmand00953a2008-04-30 12:45:53 -0400198 /* ptr now at header described in rfc 1964, section 1.2.1: */
199 ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
200 ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400201
Kevin Coffman81d4a432010-03-17 13:02:51 -0400202 msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400203
Kevin Coffman81d4a432010-03-17 13:02:51 -0400204 *(__be16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg);
Kevin Coffmand00953a2008-04-30 12:45:53 -0400205 memset(ptr + 4, 0xff, 4);
Kevin Coffman81d4a432010-03-17 13:02:51 -0400206 *(__be16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400207
Kevin Coffman5af46542010-03-17 13:03:05 -0400208 gss_krb5_make_confounder(msg_start, conflen);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400209
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400210 if (kctx->gk5e->keyed_cksum)
211 cksumkey = kctx->cksum;
212 else
213 cksumkey = NULL;
214
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400215 /* XXXJBF: UGH!: */
216 tmp_pages = buf->pages;
217 buf->pages = pages;
Kevin Coffman5af46542010-03-17 13:03:05 -0400218 if (make_checksum(kctx, ptr, 8, buf, offset + headlen - conflen,
Kevin Coffman8b237072010-03-17 13:03:02 -0400219 cksumkey, KG_USAGE_SEAL, &md5cksum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500220 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400221 buf->pages = tmp_pages;
222
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400223 memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400224
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500225 spin_lock(&krb5_seq_lock);
226 seq_send = kctx->seq_send++;
227 spin_unlock(&krb5_seq_lock);
228
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400229 /* XXX would probably be more efficient to compute checksum
230 * and encrypt at the same time: */
Kevin Coffman1dbd9022010-03-17 13:03:04 -0400231 if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff,
Kevin Coffmand00953a2008-04-30 12:45:53 -0400232 seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500233 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400234
Kevin Coffman5af46542010-03-17 13:03:05 -0400235 if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - conflen,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400236 pages))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500237 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400238
J. Bruce Fields94efa932006-12-04 20:22:42 -0500239 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400240}
241
Kevin Coffman1ac37192010-03-17 13:02:49 -0400242static u32
243gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400244{
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400245 int signalg;
246 int sealalg;
Kevin Coffman81d4a432010-03-17 13:02:51 -0400247 char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
248 struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
249 .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400250 s32 now;
251 int direction;
252 s32 seqnum;
253 unsigned char *ptr;
254 int bodysize;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400255 void *data_start, *orig_start;
256 int data_len;
257 int blocksize;
Kevin Coffman5af46542010-03-17 13:03:05 -0400258 u32 conflen = kctx->gk5e->conflen;
Kevin Coffman81d4a432010-03-17 13:02:51 -0400259 int crypt_offset;
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400260 u8 *cksumkey;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400261
Chuck Lever8885cb32007-01-31 12:14:05 -0500262 dprintk("RPC: gss_unwrap_kerberos\n");
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400263
264 ptr = (u8 *)buf->head[0].iov_base + offset;
265 if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
266 buf->len - offset))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500267 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400268
Kevin Coffmand00953a2008-04-30 12:45:53 -0400269 if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
270 (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500271 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400272
273 /* XXX sanity-check bodysize?? */
274
275 /* get the sign and seal algorithms */
276
Kevin Coffmand00953a2008-04-30 12:45:53 -0400277 signalg = ptr[2] + (ptr[3] << 8);
Kevin Coffman81d4a432010-03-17 13:02:51 -0400278 if (signalg != kctx->gk5e->signalg)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500279 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400280
Kevin Coffmand00953a2008-04-30 12:45:53 -0400281 sealalg = ptr[4] + (ptr[5] << 8);
Kevin Coffman81d4a432010-03-17 13:02:51 -0400282 if (sealalg != kctx->gk5e->sealalg)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500283 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields94efa932006-12-04 20:22:42 -0500284
Kevin Coffmand00953a2008-04-30 12:45:53 -0400285 if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500286 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400287
Kevin Coffman81d4a432010-03-17 13:02:51 -0400288 /*
289 * Data starts after token header and checksum. ptr points
290 * to the beginning of the token header
291 */
292 crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) -
293 (unsigned char *)buf->head[0].iov_base;
294 if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500295 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400296
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400297 if (kctx->gk5e->keyed_cksum)
298 cksumkey = kctx->cksum;
299 else
300 cksumkey = NULL;
301
302 if (make_checksum(kctx, ptr, 8, buf, crypt_offset,
Kevin Coffman8b237072010-03-17 13:03:02 -0400303 cksumkey, KG_USAGE_SEAL, &md5cksum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500304 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400305
Kevin Coffmane1f6c072010-03-17 13:02:52 -0400306 if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
307 kctx->gk5e->cksumlength))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500308 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400309
310 /* it got through unscathed. Make sure the context is unexpired */
311
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400312 now = get_seconds();
313
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400314 if (now > kctx->endtime)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500315 return GSS_S_CONTEXT_EXPIRED;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400316
317 /* do sequencing checks */
318
Kevin Coffman1dbd9022010-03-17 13:03:04 -0400319 if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN,
320 ptr + 8, &direction, &seqnum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500321 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400322
323 if ((kctx->initiate && direction != 0xff) ||
324 (!kctx->initiate && direction != 0))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500325 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400326
327 /* Copy the data back to the right position. XXX: Would probably be
328 * better to copy and encrypt at the same time. */
329
Herbert Xu378c6692006-08-22 20:33:54 +1000330 blocksize = crypto_blkcipher_blocksize(kctx->enc);
Kevin Coffman81d4a432010-03-17 13:02:51 -0400331 data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) +
Kevin Coffman5af46542010-03-17 13:03:05 -0400332 conflen;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400333 orig_start = buf->head[0].iov_base + offset;
334 data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
335 memmove(orig_start, data_start, data_len);
336 buf->head[0].iov_len -= (data_start - orig_start);
337 buf->len -= (data_start - orig_start);
338
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400339 if (gss_krb5_remove_padding(buf, blocksize))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500340 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400341
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500342 return GSS_S_COMPLETE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400343}
Kevin Coffman1ac37192010-03-17 13:02:49 -0400344
Kevin Coffmande9c17e2010-03-17 13:02:59 -0400345/*
346 * We cannot currently handle tokens with rotated data. We need a
347 * generalized routine to rotate the data in place. It is anticipated
348 * that we won't encounter rotated data in the general case.
349 */
350static u32
351rotate_left(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf, u16 rrc)
352{
353 unsigned int realrrc = rrc % (buf->len - offset - GSS_KRB5_TOK_HDR_LEN);
354
355 if (realrrc == 0)
356 return 0;
357
358 dprintk("%s: cannot process token with rotated data: "
359 "rrc %u, realrrc %u\n", __func__, rrc, realrrc);
360 return 1;
361}
362
363static u32
364gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset,
365 struct xdr_buf *buf, struct page **pages)
366{
367 int blocksize;
368 u8 *ptr, *plainhdr;
369 s32 now;
370 u8 flags = 0x00;
371 __be16 *be16ptr, ec = 0;
372 __be64 *be64ptr;
373 u32 err;
374
375 dprintk("RPC: %s\n", __func__);
376
377 if (kctx->gk5e->encrypt_v2 == NULL)
378 return GSS_S_FAILURE;
379
380 /* make room for gss token header */
381 if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN))
382 return GSS_S_FAILURE;
383
384 /* construct gss token header */
385 ptr = plainhdr = buf->head[0].iov_base + offset;
386 *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff);
387 *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff);
388
389 if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
390 flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR;
391 if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0)
392 flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY;
393 /* We always do confidentiality in wrap tokens */
394 flags |= KG2_TOKEN_FLAG_SEALED;
395
396 *ptr++ = flags;
397 *ptr++ = 0xff;
398 be16ptr = (__be16 *)ptr;
399
400 blocksize = crypto_blkcipher_blocksize(kctx->acceptor_enc);
401 *be16ptr++ = cpu_to_be16(ec);
402 /* "inner" token header always uses 0 for RRC */
403 *be16ptr++ = cpu_to_be16(0);
404
405 be64ptr = (__be64 *)be16ptr;
406 spin_lock(&krb5_seq_lock);
407 *be64ptr = cpu_to_be64(kctx->seq_send64++);
408 spin_unlock(&krb5_seq_lock);
409
410 err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, ec, pages);
411 if (err)
412 return err;
413
414 now = get_seconds();
415 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
416}
417
418static u32
419gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
420{
421 s32 now;
422 u64 seqnum;
423 u8 *ptr;
424 u8 flags = 0x00;
425 u16 ec, rrc;
426 int err;
427 u32 headskip, tailskip;
428 u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN];
429 unsigned int movelen;
430
431
432 dprintk("RPC: %s\n", __func__);
433
434 if (kctx->gk5e->decrypt_v2 == NULL)
435 return GSS_S_FAILURE;
436
437 ptr = buf->head[0].iov_base + offset;
438
439 if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP)
440 return GSS_S_DEFECTIVE_TOKEN;
441
442 flags = ptr[2];
443 if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
444 (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
445 return GSS_S_BAD_SIG;
446
447 if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) {
448 dprintk("%s: token missing expected sealed flag\n", __func__);
449 return GSS_S_DEFECTIVE_TOKEN;
450 }
451
452 if (ptr[3] != 0xff)
453 return GSS_S_DEFECTIVE_TOKEN;
454
455 ec = be16_to_cpup((__be16 *)(ptr + 4));
456 rrc = be16_to_cpup((__be16 *)(ptr + 6));
457
458 seqnum = be64_to_cpup((__be64 *)(ptr + 8));
459
460 if (rrc != 0) {
461 err = rotate_left(kctx, offset, buf, rrc);
462 if (err)
463 return GSS_S_FAILURE;
464 }
465
466 err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf,
467 &headskip, &tailskip);
468 if (err)
469 return GSS_S_FAILURE;
470
471 /*
472 * Retrieve the decrypted gss token header and verify
473 * it against the original
474 */
475 err = read_bytes_from_xdr_buf(buf,
476 buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip,
477 decrypted_hdr, GSS_KRB5_TOK_HDR_LEN);
478 if (err) {
479 dprintk("%s: error %u getting decrypted_hdr\n", __func__, err);
480 return GSS_S_FAILURE;
481 }
482 if (memcmp(ptr, decrypted_hdr, 6)
483 || memcmp(ptr + 8, decrypted_hdr + 8, 8)) {
484 dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__);
485 return GSS_S_FAILURE;
486 }
487
488 /* do sequencing checks */
489
490 /* it got through unscathed. Make sure the context is unexpired */
491 now = get_seconds();
492 if (now > kctx->endtime)
493 return GSS_S_CONTEXT_EXPIRED;
494
495 /*
496 * Move the head data back to the right position in xdr_buf.
497 * We ignore any "ec" data since it might be in the head or
498 * the tail, and we really don't need to deal with it.
499 * Note that buf->head[0].iov_len may indicate the available
500 * head buffer space rather than that actually occupied.
501 */
502 movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len);
503 movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip;
504 BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen >
505 buf->head[0].iov_len);
506 memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen);
507 buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
508 buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
509
510 return GSS_S_COMPLETE;
511}
512
Kevin Coffman1ac37192010-03-17 13:02:49 -0400513u32
514gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
515 struct xdr_buf *buf, struct page **pages)
516{
517 struct krb5_ctx *kctx = gctx->internal_ctx_id;
518
519 switch (kctx->enctype) {
520 default:
521 BUG();
522 case ENCTYPE_DES_CBC_RAW:
Kevin Coffman958142e2010-03-17 13:02:55 -0400523 case ENCTYPE_DES3_CBC_RAW:
Kevin Coffman1ac37192010-03-17 13:02:49 -0400524 return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
Kevin Coffmande9c17e2010-03-17 13:02:59 -0400525 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
526 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
527 return gss_wrap_kerberos_v2(kctx, offset, buf, pages);
Kevin Coffman1ac37192010-03-17 13:02:49 -0400528 }
529}
530
531u32
532gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
533{
534 struct krb5_ctx *kctx = gctx->internal_ctx_id;
535
536 switch (kctx->enctype) {
537 default:
538 BUG();
539 case ENCTYPE_DES_CBC_RAW:
Kevin Coffman958142e2010-03-17 13:02:55 -0400540 case ENCTYPE_DES3_CBC_RAW:
Kevin Coffman1ac37192010-03-17 13:02:49 -0400541 return gss_unwrap_kerberos_v1(kctx, offset, buf);
Kevin Coffmande9c17e2010-03-17 13:02:59 -0400542 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
543 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
544 return gss_unwrap_kerberos_v2(kctx, offset, buf);
Kevin Coffman1ac37192010-03-17 13:02:49 -0400545 }
546}
547