3 * The Regents of the University of Michigan
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
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
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
33 #include <linux/sunrpc/gss_krb5.h>
34 #include <linux/random.h>
35 #include <linux/pagemap.h>
36 #include <linux/crypto.h>
39 # define RPCDBG_FACILITY RPCDBG_AUTH
43 gss_krb5_padding(int blocksize, int length)
45 return blocksize - (length % blocksize);
49 gss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize)
51 int padding = gss_krb5_padding(blocksize, buf->len - offset);
55 if (buf->page_len || buf->tail[0].iov_len)
59 p = iov->iov_base + iov->iov_len;
60 iov->iov_len += padding;
62 memset(p, padding, padding);
66 gss_krb5_remove_padding(struct xdr_buf *buf, int blocksize)
70 size_t len = buf->len;
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)
76 buf->head[0].iov_len -= pad;
79 len -= buf->head[0].iov_len;
80 if (len <= buf->page_len) {
81 unsigned int last = (buf->page_base + len - 1)
83 unsigned int offset = (buf->page_base + len - 1)
84 & (PAGE_CACHE_SIZE - 1);
85 ptr = kmap_atomic(buf->pages[last], KM_USER0);
86 pad = *(ptr + offset);
87 kunmap_atomic(ptr, KM_USER0);
91 BUG_ON(len > buf->tail[0].iov_len);
92 pad = *(u8 *)(buf->tail[0].iov_base + len - 1);
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. */
117 make_confounder(char *p, u32 conflen)
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. */
131 /* initialize to random value */
134 i = (i << 32) | random32();
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. */
155 /* XXX factor out common code with seal/unseal. */
158 gss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset,
159 struct xdr_buf *buf, struct page **pages)
161 char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
162 struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
164 int blocksize = 0, plainlen;
165 unsigned char *ptr, *msg_start;
168 struct page **tmp_pages;
172 dprintk("RPC: %s\n", __func__);
176 blocksize = crypto_blkcipher_blocksize(kctx->enc);
177 gss_krb5_add_padding(buf, offset, blocksize);
178 BUG_ON((buf->len - offset) % blocksize);
179 plainlen = blocksize + buf->len - offset;
181 headlen = g_token_size(&kctx->mech_used,
182 GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) -
185 ptr = buf->head[0].iov_base + offset;
186 /* shift data to make room for header. */
187 xdr_extend_head(buf, offset, headlen);
189 /* XXX Would be cleverer to encrypt while copying. */
190 BUG_ON((buf->len - offset - headlen) % blocksize);
192 g_make_token_header(&kctx->mech_used,
193 GSS_KRB5_TOK_HDR_LEN +
194 kctx->gk5e->cksumlength + plainlen, &ptr);
197 /* ptr now at header described in rfc 1964, section 1.2.1: */
198 ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff);
199 ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff);
201 msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength;
203 *(__be16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg);
204 memset(ptr + 4, 0xff, 4);
205 *(__be16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg);
207 make_confounder(msg_start, blocksize);
209 if (kctx->gk5e->keyed_cksum)
210 cksumkey = kctx->cksum;
215 tmp_pages = buf->pages;
217 if (make_checksum(kctx, ptr, 8, buf, offset + headlen - blocksize,
218 cksumkey, &md5cksum))
219 return GSS_S_FAILURE;
220 buf->pages = tmp_pages;
222 memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
224 spin_lock(&krb5_seq_lock);
225 seq_send = kctx->seq_send++;
226 spin_unlock(&krb5_seq_lock);
228 /* XXX would probably be more efficient to compute checksum
229 * and encrypt at the same time: */
230 if ((krb5_make_seq_num(kctx->seq, kctx->initiate ? 0 : 0xff,
231 seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8)))
232 return GSS_S_FAILURE;
234 if (gss_encrypt_xdr_buf(kctx->enc, buf, offset + headlen - blocksize,
236 return GSS_S_FAILURE;
238 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
242 gss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
246 char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
247 struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),
254 void *data_start, *orig_start;
260 dprintk("RPC: gss_unwrap_kerberos\n");
262 ptr = (u8 *)buf->head[0].iov_base + offset;
263 if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr,
265 return GSS_S_DEFECTIVE_TOKEN;
267 if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) ||
268 (ptr[1] != (KG_TOK_WRAP_MSG & 0xff)))
269 return GSS_S_DEFECTIVE_TOKEN;
271 /* XXX sanity-check bodysize?? */
273 /* get the sign and seal algorithms */
275 signalg = ptr[2] + (ptr[3] << 8);
276 if (signalg != kctx->gk5e->signalg)
277 return GSS_S_DEFECTIVE_TOKEN;
279 sealalg = ptr[4] + (ptr[5] << 8);
280 if (sealalg != kctx->gk5e->sealalg)
281 return GSS_S_DEFECTIVE_TOKEN;
283 if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
284 return GSS_S_DEFECTIVE_TOKEN;
287 * Data starts after token header and checksum. ptr points
288 * to the beginning of the token header
290 crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) -
291 (unsigned char *)buf->head[0].iov_base;
292 if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset))
293 return GSS_S_DEFECTIVE_TOKEN;
295 if (kctx->gk5e->keyed_cksum)
296 cksumkey = kctx->cksum;
300 if (make_checksum(kctx, ptr, 8, buf, crypt_offset,
301 cksumkey, &md5cksum))
302 return GSS_S_FAILURE;
304 if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
305 kctx->gk5e->cksumlength))
306 return GSS_S_BAD_SIG;
308 /* it got through unscathed. Make sure the context is unexpired */
312 if (now > kctx->endtime)
313 return GSS_S_CONTEXT_EXPIRED;
315 /* do sequencing checks */
317 if (krb5_get_seq_num(kctx->seq, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
318 &direction, &seqnum))
319 return GSS_S_BAD_SIG;
321 if ((kctx->initiate && direction != 0xff) ||
322 (!kctx->initiate && direction != 0))
323 return GSS_S_BAD_SIG;
325 /* Copy the data back to the right position. XXX: Would probably be
326 * better to copy and encrypt at the same time. */
328 blocksize = crypto_blkcipher_blocksize(kctx->enc);
329 data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) +
331 orig_start = buf->head[0].iov_base + offset;
332 data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start;
333 memmove(orig_start, data_start, data_len);
334 buf->head[0].iov_len -= (data_start - orig_start);
335 buf->len -= (data_start - orig_start);
337 if (gss_krb5_remove_padding(buf, blocksize))
338 return GSS_S_DEFECTIVE_TOKEN;
340 return GSS_S_COMPLETE;
344 * We cannot currently handle tokens with rotated data. We need a
345 * generalized routine to rotate the data in place. It is anticipated
346 * that we won't encounter rotated data in the general case.
349 rotate_left(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf, u16 rrc)
351 unsigned int realrrc = rrc % (buf->len - offset - GSS_KRB5_TOK_HDR_LEN);
356 dprintk("%s: cannot process token with rotated data: "
357 "rrc %u, realrrc %u\n", __func__, rrc, realrrc);
362 gss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset,
363 struct xdr_buf *buf, struct page **pages)
369 __be16 *be16ptr, ec = 0;
373 dprintk("RPC: %s\n", __func__);
375 if (kctx->gk5e->encrypt_v2 == NULL)
376 return GSS_S_FAILURE;
378 /* make room for gss token header */
379 if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN))
380 return GSS_S_FAILURE;
382 /* construct gss token header */
383 ptr = plainhdr = buf->head[0].iov_base + offset;
384 *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff);
385 *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff);
387 if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
388 flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR;
389 if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0)
390 flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY;
391 /* We always do confidentiality in wrap tokens */
392 flags |= KG2_TOKEN_FLAG_SEALED;
396 be16ptr = (__be16 *)ptr;
398 blocksize = crypto_blkcipher_blocksize(kctx->acceptor_enc);
399 *be16ptr++ = cpu_to_be16(ec);
400 /* "inner" token header always uses 0 for RRC */
401 *be16ptr++ = cpu_to_be16(0);
403 be64ptr = (__be64 *)be16ptr;
404 spin_lock(&krb5_seq_lock);
405 *be64ptr = cpu_to_be64(kctx->seq_send64++);
406 spin_unlock(&krb5_seq_lock);
408 err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, ec, pages);
413 return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
417 gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
425 u32 headskip, tailskip;
426 u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN];
427 unsigned int movelen;
430 dprintk("RPC: %s\n", __func__);
432 if (kctx->gk5e->decrypt_v2 == NULL)
433 return GSS_S_FAILURE;
435 ptr = buf->head[0].iov_base + offset;
437 if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP)
438 return GSS_S_DEFECTIVE_TOKEN;
441 if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
442 (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
443 return GSS_S_BAD_SIG;
445 if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) {
446 dprintk("%s: token missing expected sealed flag\n", __func__);
447 return GSS_S_DEFECTIVE_TOKEN;
451 return GSS_S_DEFECTIVE_TOKEN;
453 ec = be16_to_cpup((__be16 *)(ptr + 4));
454 rrc = be16_to_cpup((__be16 *)(ptr + 6));
456 seqnum = be64_to_cpup((__be64 *)(ptr + 8));
459 err = rotate_left(kctx, offset, buf, rrc);
461 return GSS_S_FAILURE;
464 err = (*kctx->gk5e->decrypt_v2)(kctx, offset, buf,
465 &headskip, &tailskip);
467 return GSS_S_FAILURE;
470 * Retrieve the decrypted gss token header and verify
471 * it against the original
473 err = read_bytes_from_xdr_buf(buf,
474 buf->len - GSS_KRB5_TOK_HDR_LEN - tailskip,
475 decrypted_hdr, GSS_KRB5_TOK_HDR_LEN);
477 dprintk("%s: error %u getting decrypted_hdr\n", __func__, err);
478 return GSS_S_FAILURE;
480 if (memcmp(ptr, decrypted_hdr, 6)
481 || memcmp(ptr + 8, decrypted_hdr + 8, 8)) {
482 dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__);
483 return GSS_S_FAILURE;
486 /* do sequencing checks */
488 /* it got through unscathed. Make sure the context is unexpired */
490 if (now > kctx->endtime)
491 return GSS_S_CONTEXT_EXPIRED;
494 * Move the head data back to the right position in xdr_buf.
495 * We ignore any "ec" data since it might be in the head or
496 * the tail, and we really don't need to deal with it.
497 * Note that buf->head[0].iov_len may indicate the available
498 * head buffer space rather than that actually occupied.
500 movelen = min_t(unsigned int, buf->head[0].iov_len, buf->len);
501 movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip;
502 BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen >
503 buf->head[0].iov_len);
504 memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen);
505 buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
506 buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
508 return GSS_S_COMPLETE;
512 gss_wrap_kerberos(struct gss_ctx *gctx, int offset,
513 struct xdr_buf *buf, struct page **pages)
515 struct krb5_ctx *kctx = gctx->internal_ctx_id;
517 switch (kctx->enctype) {
520 case ENCTYPE_DES_CBC_RAW:
521 case ENCTYPE_DES3_CBC_RAW:
522 return gss_wrap_kerberos_v1(kctx, offset, buf, pages);
523 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
524 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
525 return gss_wrap_kerberos_v2(kctx, offset, buf, pages);
530 gss_unwrap_kerberos(struct gss_ctx *gctx, int offset, struct xdr_buf *buf)
532 struct krb5_ctx *kctx = gctx->internal_ctx_id;
534 switch (kctx->enctype) {
537 case ENCTYPE_DES_CBC_RAW:
538 case ENCTYPE_DES3_CBC_RAW:
539 return gss_unwrap_kerberos_v1(kctx, offset, buf);
540 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
541 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
542 return gss_unwrap_kerberos_v2(kctx, offset, buf);