xref: /linux/net/sunrpc/auth_gss/gss_krb5_unseal.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1 /*
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-2008 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 <crypto/algapi.h>
61 #include <linux/types.h>
62 #include <linux/jiffies.h>
63 #include <linux/sunrpc/gss_krb5.h>
64 #include <linux/crypto.h>
65 
66 #include "gss_krb5_internal.h"
67 
68 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
69 # define RPCDBG_FACILITY        RPCDBG_AUTH
70 #endif
71 
72 
73 #if defined(CONFIG_RPCSEC_GSS_KRB5_SIMPLIFIED)
74 /* read_token is a mic token, and message_buffer is the data that the mic was
75  * supposedly taken over. */
76 u32
77 gss_krb5_verify_mic_v1(struct krb5_ctx *ctx, struct xdr_buf *message_buffer,
78 		       struct xdr_netobj *read_token)
79 {
80 	int			signalg;
81 	int			sealalg;
82 	char			cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
83 	struct xdr_netobj	md5cksum = {.len = sizeof(cksumdata),
84 					    .data = cksumdata};
85 	s32			now;
86 	int			direction;
87 	u32			seqnum;
88 	unsigned char		*ptr = (unsigned char *)read_token->data;
89 	int			bodysize;
90 	u8			*cksumkey;
91 
92 	dprintk("RPC:       krb5_read_token\n");
93 
94 	if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr,
95 					read_token->len))
96 		return GSS_S_DEFECTIVE_TOKEN;
97 
98 	if ((ptr[0] != ((KG_TOK_MIC_MSG >> 8) & 0xff)) ||
99 	    (ptr[1] !=  (KG_TOK_MIC_MSG & 0xff)))
100 		return GSS_S_DEFECTIVE_TOKEN;
101 
102 	/* XXX sanity-check bodysize?? */
103 
104 	signalg = ptr[2] + (ptr[3] << 8);
105 	if (signalg != ctx->gk5e->signalg)
106 		return GSS_S_DEFECTIVE_TOKEN;
107 
108 	sealalg = ptr[4] + (ptr[5] << 8);
109 	if (sealalg != SEAL_ALG_NONE)
110 		return GSS_S_DEFECTIVE_TOKEN;
111 
112 	if ((ptr[6] != 0xff) || (ptr[7] != 0xff))
113 		return GSS_S_DEFECTIVE_TOKEN;
114 
115 	if (ctx->gk5e->keyed_cksum)
116 		cksumkey = ctx->cksum;
117 	else
118 		cksumkey = NULL;
119 
120 	if (make_checksum(ctx, ptr, 8, message_buffer, 0,
121 			  cksumkey, KG_USAGE_SIGN, &md5cksum))
122 		return GSS_S_FAILURE;
123 
124 	if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,
125 					ctx->gk5e->cksumlength))
126 		return GSS_S_BAD_SIG;
127 
128 	/* it got through unscathed.  Make sure the context is unexpired */
129 
130 	now = ktime_get_real_seconds();
131 
132 	if (now > ctx->endtime)
133 		return GSS_S_CONTEXT_EXPIRED;
134 
135 	/* do sequencing checks */
136 
137 	if (krb5_get_seq_num(ctx, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,
138 			     &direction, &seqnum))
139 		return GSS_S_FAILURE;
140 
141 	if ((ctx->initiate && direction != 0xff) ||
142 	    (!ctx->initiate && direction != 0))
143 		return GSS_S_BAD_SIG;
144 
145 	return GSS_S_COMPLETE;
146 }
147 #endif
148 
149 u32
150 gss_krb5_verify_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *message_buffer,
151 		       struct xdr_netobj *read_token)
152 {
153 	struct crypto_ahash *tfm = ctx->initiate ?
154 				   ctx->acceptor_sign : ctx->initiator_sign;
155 	char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
156 	struct xdr_netobj cksumobj = {
157 		.len	= ctx->gk5e->cksumlength,
158 		.data	= cksumdata,
159 	};
160 	u8 *ptr = read_token->data;
161 	__be16 be16_ptr;
162 	time64_t now;
163 	u8 flags;
164 	int i;
165 
166 	dprintk("RPC:       %s\n", __func__);
167 
168 	memcpy(&be16_ptr, (char *) ptr, 2);
169 	if (be16_to_cpu(be16_ptr) != KG2_TOK_MIC)
170 		return GSS_S_DEFECTIVE_TOKEN;
171 
172 	flags = ptr[2];
173 	if ((!ctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
174 	    (ctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
175 		return GSS_S_BAD_SIG;
176 
177 	if (flags & KG2_TOKEN_FLAG_SEALED) {
178 		dprintk("%s: token has unexpected sealed flag\n", __func__);
179 		return GSS_S_FAILURE;
180 	}
181 
182 	for (i = 3; i < 8; i++)
183 		if (ptr[i] != 0xff)
184 			return GSS_S_DEFECTIVE_TOKEN;
185 
186 	if (gss_krb5_checksum(tfm, ptr, GSS_KRB5_TOK_HDR_LEN,
187 			      message_buffer, 0, &cksumobj))
188 		return GSS_S_FAILURE;
189 
190 	if (memcmp(cksumobj.data, ptr + GSS_KRB5_TOK_HDR_LEN,
191 				ctx->gk5e->cksumlength))
192 		return GSS_S_BAD_SIG;
193 
194 	/* it got through unscathed.  Make sure the context is unexpired */
195 	now = ktime_get_real_seconds();
196 	if (now > ctx->endtime)
197 		return GSS_S_CONTEXT_EXPIRED;
198 
199 	/*
200 	 * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
201 	 * doesn't want it checked; see page 6 of rfc 2203.
202 	 */
203 
204 	return GSS_S_COMPLETE;
205 }
206