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