1 /*
2  *  linux/net/sunrpc/gss_krb5_seal.c
3  *
4  *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5seal.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  *  J. Bruce Fields	<bfields@umich.edu>
11  */
12 
13 /*
14  * Copyright 1993 by OpenVision Technologies, Inc.
15  *
16  * Permission to use, copy, modify, distribute, and sell this software
17  * and its documentation for any purpose is hereby granted without fee,
18  * provided that the above copyright notice appears in all copies and
19  * that both that copyright notice and this permission notice appear in
20  * supporting documentation, and that the name of OpenVision not be used
21  * in advertising or publicity pertaining to distribution of the software
22  * without specific, written prior permission. OpenVision makes no
23  * representations about the suitability of this software for any
24  * purpose.  It is provided "as is" without express or implied warranty.
25  *
26  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
27  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
28  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
29  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
30  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
31  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32  * PERFORMANCE OF THIS SOFTWARE.
33  */
34 
35 /*
36  * Copyright (C) 1998 by the FundsXpress, INC.
37  *
38  * All rights reserved.
39  *
40  * Export of this software from the United States of America may require
41  * a specific license from the United States Government.  It is the
42  * responsibility of any person or organization contemplating export to
43  * obtain such a license before exporting.
44  *
45  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
46  * distribute this software and its documentation for any purpose and
47  * without fee is hereby granted, provided that the above copyright
48  * notice appear in all copies and that both that copyright notice and
49  * this permission notice appear in supporting documentation, and that
50  * the name of FundsXpress. not be used in advertising or publicity pertaining
51  * to distribution of the software without specific, written prior
52  * permission.  FundsXpress makes no representations about the suitability of
53  * this software for any purpose.  It is provided "as is" without express
54  * or implied warranty.
55  *
56  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
57  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
58  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
59  */
60 
61 #include <linux/types.h>
62 #include <linux/jiffies.h>
63 #include <linux/sunrpc/gss_krb5.h>
64 #include <linux/random.h>
65 #include <linux/crypto.h>
66 #include <linux/atomic.h>
67 
68 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
69 # define RPCDBG_FACILITY        RPCDBG_AUTH
70 #endif
71 
72 static void *
73 setup_token(struct krb5_ctx *ctx, struct xdr_netobj *token)
74 {
75 	u16 *ptr;
76 	void *krb5_hdr;
77 	int body_size = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;
78 
79 	token->len = g_token_size(&ctx->mech_used, body_size);
80 
81 	ptr = (u16 *)token->data;
82 	g_make_token_header(&ctx->mech_used, body_size, (unsigned char **)&ptr);
83 
84 	/* ptr now at start of header described in rfc 1964, section 1.2.1: */
85 	krb5_hdr = ptr;
86 	*ptr++ = KG_TOK_MIC_MSG;
87 	/*
88 	 * signalg is stored as if it were converted from LE to host endian, even
89 	 * though it's an opaque pair of bytes according to the RFC.
90 	 */
91 	*ptr++ = (__force u16)cpu_to_le16(ctx->gk5e->signalg);
92 	*ptr++ = SEAL_ALG_NONE;
93 	*ptr = 0xffff;
94 
95 	return krb5_hdr;
96 }
97 
98 static void *
99 setup_token_v2(struct krb5_ctx *ctx, struct xdr_netobj *token)
100 {
101 	u16 *ptr;
102 	void *krb5_hdr;
103 	u8 *p, flags = 0x00;
104 
105 	if ((ctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
106 		flags |= 0x01;
107 	if (ctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
108 		flags |= 0x04;
109 
110 	/* Per rfc 4121, sec 4.2.6.1, there is no header,
111 	 * just start the token */
112 	krb5_hdr = ptr = (u16 *)token->data;
113 
114 	*ptr++ = KG2_TOK_MIC;
115 	p = (u8 *)ptr;
116 	*p++ = flags;
117 	*p++ = 0xff;
118 	ptr = (u16 *)p;
119 	*ptr++ = 0xffff;
120 	*ptr = 0xffff;
121 
122 	token->len = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;
123 	return krb5_hdr;
124 }
125 
126 static u32
127 gss_get_mic_v1(struct krb5_ctx *ctx, struct xdr_buf *text,
128 		struct xdr_netobj *token)
129 {
130 	char			cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
131 	struct xdr_netobj	md5cksum = {.len = sizeof(cksumdata),
132 					    .data = cksumdata};
133 	void			*ptr;
134 	time64_t		now;
135 	u32			seq_send;
136 	u8			*cksumkey;
137 
138 	dprintk("RPC:       %s\n", __func__);
139 	BUG_ON(ctx == NULL);
140 
141 	now = ktime_get_real_seconds();
142 
143 	ptr = setup_token(ctx, token);
144 
145 	if (ctx->gk5e->keyed_cksum)
146 		cksumkey = ctx->cksum;
147 	else
148 		cksumkey = NULL;
149 
150 	if (make_checksum(ctx, ptr, 8, text, 0, cksumkey,
151 			  KG_USAGE_SIGN, &md5cksum))
152 		return GSS_S_FAILURE;
153 
154 	memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
155 
156 	seq_send = atomic_fetch_inc(&ctx->seq_send);
157 
158 	if (krb5_make_seq_num(ctx, ctx->seq, ctx->initiate ? 0 : 0xff,
159 			      seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8))
160 		return GSS_S_FAILURE;
161 
162 	return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
163 }
164 
165 static u32
166 gss_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text,
167 		struct xdr_netobj *token)
168 {
169 	char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
170 	struct xdr_netobj cksumobj = { .len = sizeof(cksumdata),
171 				       .data = cksumdata};
172 	void *krb5_hdr;
173 	time64_t now;
174 	u8 *cksumkey;
175 	unsigned int cksum_usage;
176 	__be64 seq_send_be64;
177 
178 	dprintk("RPC:       %s\n", __func__);
179 
180 	krb5_hdr = setup_token_v2(ctx, token);
181 
182 	/* Set up the sequence number. Now 64-bits in clear
183 	 * text and w/o direction indicator */
184 	seq_send_be64 = cpu_to_be64(atomic64_fetch_inc(&ctx->seq_send64));
185 	memcpy(krb5_hdr + 8, (char *) &seq_send_be64, 8);
186 
187 	if (ctx->initiate) {
188 		cksumkey = ctx->initiator_sign;
189 		cksum_usage = KG_USAGE_INITIATOR_SIGN;
190 	} else {
191 		cksumkey = ctx->acceptor_sign;
192 		cksum_usage = KG_USAGE_ACCEPTOR_SIGN;
193 	}
194 
195 	if (make_checksum_v2(ctx, krb5_hdr, GSS_KRB5_TOK_HDR_LEN,
196 			     text, 0, cksumkey, cksum_usage, &cksumobj))
197 		return GSS_S_FAILURE;
198 
199 	memcpy(krb5_hdr + GSS_KRB5_TOK_HDR_LEN, cksumobj.data, cksumobj.len);
200 
201 	now = ktime_get_real_seconds();
202 
203 	return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
204 }
205 
206 u32
207 gss_get_mic_kerberos(struct gss_ctx *gss_ctx, struct xdr_buf *text,
208 		     struct xdr_netobj *token)
209 {
210 	struct krb5_ctx		*ctx = gss_ctx->internal_ctx_id;
211 
212 	switch (ctx->enctype) {
213 	default:
214 		BUG();
215 	case ENCTYPE_DES_CBC_RAW:
216 	case ENCTYPE_DES3_CBC_RAW:
217 		return gss_get_mic_v1(ctx, text, token);
218 	case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
219 	case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
220 		return gss_get_mic_v2(ctx, text, token);
221 	}
222 }
223