blob: a6e905637e035f54a3414321825ad97a2582b000 [file] [log] [blame]
J. Bruce Fields14ae1622005-10-13 16:55:13 -04001#include <linux/types.h>
J. Bruce Fields14ae1622005-10-13 16:55:13 -04002#include <linux/jiffies.h>
3#include <linux/sunrpc/gss_krb5.h>
4#include <linux/random.h>
5#include <linux/pagemap.h>
J. Bruce Fields14ae1622005-10-13 16:55:13 -04006#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;
Chuck Lever67f97d82007-09-26 14:38:10 -040043 size_t len = buf->len;
J. Bruce Fields14ae1622005-10-13 16:55:13 -040044
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) {
Chuck Lever67f97d82007-09-26 14:38:10 -040054 unsigned int last = (buf->page_base + len - 1)
J. Bruce Fields14ae1622005-10-13 16:55:13 -040055 >>PAGE_CACHE_SHIFT;
Chuck Lever67f97d82007-09-26 14:38:10 -040056 unsigned int offset = (buf->page_base + len - 1)
J. Bruce Fields14ae1622005-10-13 16:55:13 -040057 & (PAGE_CACHE_SIZE - 1);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050058 ptr = kmap_atomic(buf->pages[last], KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040059 pad = *(ptr + offset);
J. Bruce Fields87d918d2006-12-04 20:22:32 -050060 kunmap_atomic(ptr, KM_USER0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -040061 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
Kevin Coffman863a2482008-04-30 12:46:08 -040089static void
90make_confounder(char *p, u32 conflen)
J. Bruce Fields14ae1622005-10-13 16:55:13 -040091{
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
Kevin Coffman863a2482008-04-30 12:46:08 -0400104 /* 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 }
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400120}
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
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400131gss_wrap_kerberos(struct gss_ctx *ctx, int offset,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400132 struct xdr_buf *buf, struct page **pages)
133{
134 struct krb5_ctx *kctx = ctx->internal_ctx_id;
J. Bruce Fields9e57b302006-03-20 23:23:11 -0500135 char cksumdata[16];
136 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400137 int blocksize = 0, plainlen;
Kevin Coffmand00953a2008-04-30 12:45:53 -0400138 unsigned char *ptr, *msg_start;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400139 s32 now;
140 int headlen;
141 struct page **tmp_pages;
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500142 u32 seq_send;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400143
Chuck Lever8885cb32007-01-31 12:14:05 -0500144 dprintk("RPC: gss_wrap_kerberos\n");
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400145
146 now = get_seconds();
147
Herbert Xu378c6692006-08-22 20:33:54 +1000148 blocksize = crypto_blkcipher_blocksize(kctx->enc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400149 gss_krb5_add_padding(buf, offset, blocksize);
150 BUG_ON((buf->len - offset) % blocksize);
151 plainlen = blocksize + buf->len - offset;
152
Kevin Coffman4ab4b0b2008-03-31 10:31:44 -0400153 headlen = g_token_size(&kctx->mech_used, 24 + plainlen) -
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400154 (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
Kevin Coffmand00953a2008-04-30 12:45:53 -0400165 g_make_token_header(&kctx->mech_used,
166 GSS_KRB5_TOK_HDR_LEN + 8 + plainlen, &ptr);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400167
168
Kevin Coffmand00953a2008-04-30 12:45:53 -0400169 /* 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);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400172
Kevin Coffmand00953a2008-04-30 12:45:53 -0400173 msg_start = ptr + 24;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400174
Kevin Coffmand00953a2008-04-30 12:45:53 -0400175 *(__be16 *)(ptr + 2) = htons(SGN_ALG_DES_MAC_MD5);
176 memset(ptr + 4, 0xff, 4);
177 *(__be16 *)(ptr + 4) = htons(SEAL_ALG_DES);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400178
179 make_confounder(msg_start, blocksize);
180
181 /* XXXJBF: UGH!: */
182 tmp_pages = buf->pages;
183 buf->pages = pages;
Kevin Coffmand00953a2008-04-30 12:45:53 -0400184 if (make_checksum("md5", ptr, 8, buf,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400185 offset + headlen - blocksize, &md5cksum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500186 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400187 buf->pages = tmp_pages;
188
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500189 if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
190 md5cksum.data, md5cksum.len))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500191 return GSS_S_FAILURE;
Kevin Coffmand00953a2008-04-30 12:45:53 -0400192 memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data + md5cksum.len - 8, 8);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400193
J. Bruce Fieldseaa82ed2006-03-20 23:24:04 -0500194 spin_lock(&krb5_seq_lock);
195 seq_send = kctx->seq_send++;
196 spin_unlock(&krb5_seq_lock);
197
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400198 /* 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,
Kevin Coffmand00953a2008-04-30 12:45:53 -0400201 seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500202 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400203
204 if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
205 pages))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500206 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400207
J. Bruce Fields94efa932006-12-04 20:22:42 -0500208 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400209}
210
211u32
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400212gss_unwrap_kerberos(struct gss_ctx *ctx, int offset, struct xdr_buf *buf)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400213{
214 struct krb5_ctx *kctx = ctx->internal_ctx_id;
215 int signalg;
216 int sealalg;
J. Bruce Fields9e57b302006-03-20 23:23:11 -0500217 char cksumdata[16];
218 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400219 s32 now;
220 int direction;
221 s32 seqnum;
222 unsigned char *ptr;
223 int bodysize;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400224 void *data_start, *orig_start;
225 int data_len;
226 int blocksize;
227
Chuck Lever8885cb32007-01-31 12:14:05 -0500228 dprintk("RPC: gss_unwrap_kerberos\n");
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400229
230 ptr = (u8 *)buf->head[0].iov_base + offset;
231 if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
232 buf->len - offset))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500233 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400234
Kevin Coffmand00953a2008-04-30 12:45:53 -0400235 if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
236 (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500237 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400238
239 /* XXX sanity-check bodysize?? */
240
241 /* get the sign and seal algorithms */
242
Kevin Coffmand00953a2008-04-30 12:45:53 -0400243 signalg = ptr[2] + (ptr[3] << 8);
J. Bruce Fields94efa932006-12-04 20:22:42 -0500244 if (signalg != SGN_ALG_DES_MAC_MD5)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500245 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400246
Kevin Coffmand00953a2008-04-30 12:45:53 -0400247 sealalg = ptr[4] + (ptr[5] << 8);
J. Bruce Fieldsd922a842006-12-04 20:22:40 -0500248 if (sealalg != SEAL_ALG_DES)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500249 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields94efa932006-12-04 20:22:42 -0500250
Kevin Coffmand00953a2008-04-30 12:45:53 -0400251 if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500252 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400253
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400254 if (gss_decrypt_xdr_buf(kctx->enc, buf,
Kevin Coffmand00953a2008-04-30 12:45:53 -0400255 ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500256 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400257
Kevin Coffmand00953a2008-04-30 12:45:53 -0400258 if (make_checksum("md5", ptr, 8, buf,
259 ptr + GSS_KRB5_TOK_HDR_LEN + 8 - (unsigned char *)buf->head[0].iov_base, &md5cksum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500260 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400261
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500262 if (krb5_encrypt(kctx->seq, NULL, md5cksum.data,
263 md5cksum.data, md5cksum.len))
264 return GSS_S_FAILURE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400265
Kevin Coffmand00953a2008-04-30 12:45:53 -0400266 if (memcmp(md5cksum.data + 8, ptr + GSS_KRB5_TOK_HDR_LEN, 8))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500267 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400268
269 /* it got through unscathed. Make sure the context is unexpired */
270
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400271 now = get_seconds();
272
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400273 if (now > kctx->endtime)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500274 return GSS_S_CONTEXT_EXPIRED;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400275
276 /* do sequencing checks */
277
Kevin Coffmand00953a2008-04-30 12:45:53 -0400278 if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
279 &direction, &seqnum))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500280 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400281
282 if ((kctx->initiate && direction != 0xff) ||
283 (!kctx->initiate && direction != 0))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500284 return GSS_S_BAD_SIG;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400285
286 /* Copy the data back to the right position. XXX: Would probably be
287 * better to copy and encrypt at the same time. */
288
Herbert Xu378c6692006-08-22 20:33:54 +1000289 blocksize = crypto_blkcipher_blocksize(kctx->enc);
Kevin Coffmand00953a2008-04-30 12:45:53 -0400290 data_start = ptr + GSS_KRB5_TOK_HDR_LEN + 8 + blocksize;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400291 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
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400297 if (gss_krb5_remove_padding(buf, blocksize))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500298 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400299
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500300 return GSS_S_COMPLETE;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400301}