blob: 75a75a6d133657ed12546e28c9f11b83e7c6cf87 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_krb5_unseal.c
3 *
4 * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5unseal.c
5 *
6 * Copyright (c) 2000 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Andy Adamson <andros@umich.edu>
10 */
11
12/*
13 * Copyright 1993 by OpenVision Technologies, Inc.
14 *
15 * Permission to use, copy, modify, distribute, and sell this software
16 * and its documentation for any purpose is hereby granted without fee,
17 * provided that the above copyright notice appears in all copies and
18 * that both that copyright notice and this permission notice appear in
19 * supporting documentation, and that the name of OpenVision not be used
20 * in advertising or publicity pertaining to distribution of the software
21 * without specific, written prior permission. OpenVision makes no
22 * representations about the suitability of this software for any
23 * purpose. It is provided "as is" without express or implied warranty.
24 *
25 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
27 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
28 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
29 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31 * PERFORMANCE OF THIS SOFTWARE.
32 */
33
34/*
35 * Copyright (C) 1998 by the FundsXpress, INC.
36 *
37 * All rights reserved.
38 *
39 * Export of this software from the United States of America may require
40 * a specific license from the United States Government. It is the
41 * responsibility of any person or organization contemplating export to
42 * obtain such a license before exporting.
43 *
44 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45 * distribute this software and its documentation for any purpose and
46 * without fee is hereby granted, provided that the above copyright
47 * notice appear in all copies and that both that copyright notice and
48 * this permission notice appear in supporting documentation, and that
49 * the name of FundsXpress. not be used in advertising or publicity pertaining
50 * to distribution of the software without specific, written prior
51 * permission. FundsXpress makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
54 *
55 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
57 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
58 */
59
60#include <linux/types.h>
61#include <linux/slab.h>
62#include <linux/jiffies.h>
63#include <linux/sunrpc/gss_krb5.h>
64#include <linux/crypto.h>
65
66#ifdef RPC_DEBUG
67# define RPCDBG_FACILITY RPCDBG_AUTH
68#endif
69
70
J. Bruce Fieldsbfa91512005-10-13 16:55:08 -040071/* read_token is a mic token, and message_buffer is the data that the mic was
72 * supposedly taken over. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074u32
J. Bruce Fieldsa0857d02005-10-13 16:55:23 -040075gss_verify_mic_kerberos(struct gss_ctx *gss_ctx,
76 struct xdr_buf *message_buffer, struct xdr_netobj *read_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
J. Bruce Fieldsa0857d02005-10-13 16:55:23 -040078 struct krb5_ctx *ctx = gss_ctx->internal_ctx_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int signalg;
80 int sealalg;
J. Bruce Fields9e57b302006-03-20 23:23:11 -050081 char cksumdata[16];
82 struct xdr_netobj md5cksum = {.len = 0, .data = cksumdata};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 s32 now;
84 int direction;
85 s32 seqnum;
86 unsigned char *ptr = (unsigned char *)read_token->data;
87 int bodysize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 dprintk("RPC: krb5_read_token\n");
90
91 if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr,
92 read_token->len))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -050093 return GSS_S_DEFECTIVE_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
J. Bruce Fieldsbfa91512005-10-13 16:55:08 -040095 if ((*ptr++ != ((KG_TOK_MIC_MSG>>8)&0xff)) ||
96 (*ptr++ != ( KG_TOK_MIC_MSG &0xff)) )
J. Bruce Fields39a21dd2006-12-04 20:22:39 -050097 return GSS_S_DEFECTIVE_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* XXX sanity-check bodysize?? */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 signalg = ptr[0] + (ptr[1] << 8);
102 sealalg = ptr[2] + (ptr[3] << 8);
103
104 /* Sanity checks */
105
106 if ((ptr[4] != 0xff) || (ptr[5] != 0xff))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500107 return GSS_S_DEFECTIVE_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
J. Bruce Fieldsbfa91512005-10-13 16:55:08 -0400109 if (sealalg != 0xffff)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500110 return GSS_S_DEFECTIVE_TOKEN;
J. Bruce Fields5eb064f2006-12-04 20:22:36 -0500111 if (signalg != SGN_ALG_DES_MAC_MD5)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500112 return GSS_S_DEFECTIVE_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500114 if (make_checksum("md5", ptr - 2, 8, message_buffer, 0, &md5cksum))
115 return GSS_S_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500117 if (krb5_encrypt(ctx->seq, NULL, md5cksum.data, md5cksum.data, 16))
118 return GSS_S_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500120 if (memcmp(md5cksum.data + 8, ptr + 14, 8))
121 return GSS_S_BAD_SIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* it got through unscathed. Make sure the context is unexpired */
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 now = get_seconds();
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (now > ctx->endtime)
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500128 return GSS_S_CONTEXT_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* do sequencing checks */
131
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500132 if (krb5_get_seq_num(ctx->seq, ptr + 14, ptr + 6, &direction, &seqnum))
133 return GSS_S_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if ((ctx->initiate && direction != 0xff) ||
136 (!ctx->initiate && direction != 0))
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500137 return GSS_S_BAD_SIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
J. Bruce Fields39a21dd2006-12-04 20:22:39 -0500139 return GSS_S_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}