18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  linux/net/sunrpc/gss_krb5_seal.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5seal.c
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Copyright (c) 2000-2008 The Regents of the University of Michigan.
78c2ecf20Sopenharmony_ci *  All rights reserved.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Andy Adamson	<andros@umich.edu>
108c2ecf20Sopenharmony_ci *  J. Bruce Fields	<bfields@umich.edu>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * Copyright 1993 by OpenVision Technologies, Inc.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software
178c2ecf20Sopenharmony_ci * and its documentation for any purpose is hereby granted without fee,
188c2ecf20Sopenharmony_ci * provided that the above copyright notice appears in all copies and
198c2ecf20Sopenharmony_ci * that both that copyright notice and this permission notice appear in
208c2ecf20Sopenharmony_ci * supporting documentation, and that the name of OpenVision not be used
218c2ecf20Sopenharmony_ci * in advertising or publicity pertaining to distribution of the software
228c2ecf20Sopenharmony_ci * without specific, written prior permission. OpenVision makes no
238c2ecf20Sopenharmony_ci * representations about the suitability of this software for any
248c2ecf20Sopenharmony_ci * purpose.  It is provided "as is" without express or implied warranty.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
278c2ecf20Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
288c2ecf20Sopenharmony_ci * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
298c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
308c2ecf20Sopenharmony_ci * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
318c2ecf20Sopenharmony_ci * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
328c2ecf20Sopenharmony_ci * PERFORMANCE OF THIS SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci * Copyright (C) 1998 by the FundsXpress, INC.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * All rights reserved.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * Export of this software from the United States of America may require
418c2ecf20Sopenharmony_ci * a specific license from the United States Government.  It is the
428c2ecf20Sopenharmony_ci * responsibility of any person or organization contemplating export to
438c2ecf20Sopenharmony_ci * obtain such a license before exporting.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
468c2ecf20Sopenharmony_ci * distribute this software and its documentation for any purpose and
478c2ecf20Sopenharmony_ci * without fee is hereby granted, provided that the above copyright
488c2ecf20Sopenharmony_ci * notice appear in all copies and that both that copyright notice and
498c2ecf20Sopenharmony_ci * this permission notice appear in supporting documentation, and that
508c2ecf20Sopenharmony_ci * the name of FundsXpress. not be used in advertising or publicity pertaining
518c2ecf20Sopenharmony_ci * to distribution of the software without specific, written prior
528c2ecf20Sopenharmony_ci * permission.  FundsXpress makes no representations about the suitability of
538c2ecf20Sopenharmony_ci * this software for any purpose.  It is provided "as is" without express
548c2ecf20Sopenharmony_ci * or implied warranty.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
578c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
588c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#include <linux/types.h>
628c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
638c2ecf20Sopenharmony_ci#include <linux/sunrpc/gss_krb5.h>
648c2ecf20Sopenharmony_ci#include <linux/random.h>
658c2ecf20Sopenharmony_ci#include <linux/crypto.h>
668c2ecf20Sopenharmony_ci#include <linux/atomic.h>
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
698c2ecf20Sopenharmony_ci# define RPCDBG_FACILITY        RPCDBG_AUTH
708c2ecf20Sopenharmony_ci#endif
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void *
738c2ecf20Sopenharmony_cisetup_token(struct krb5_ctx *ctx, struct xdr_netobj *token)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	u16 *ptr;
768c2ecf20Sopenharmony_ci	void *krb5_hdr;
778c2ecf20Sopenharmony_ci	int body_size = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	token->len = g_token_size(&ctx->mech_used, body_size);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	ptr = (u16 *)token->data;
828c2ecf20Sopenharmony_ci	g_make_token_header(&ctx->mech_used, body_size, (unsigned char **)&ptr);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* ptr now at start of header described in rfc 1964, section 1.2.1: */
858c2ecf20Sopenharmony_ci	krb5_hdr = ptr;
868c2ecf20Sopenharmony_ci	*ptr++ = KG_TOK_MIC_MSG;
878c2ecf20Sopenharmony_ci	/*
888c2ecf20Sopenharmony_ci	 * signalg is stored as if it were converted from LE to host endian, even
898c2ecf20Sopenharmony_ci	 * though it's an opaque pair of bytes according to the RFC.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	*ptr++ = (__force u16)cpu_to_le16(ctx->gk5e->signalg);
928c2ecf20Sopenharmony_ci	*ptr++ = SEAL_ALG_NONE;
938c2ecf20Sopenharmony_ci	*ptr = 0xffff;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return krb5_hdr;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void *
998c2ecf20Sopenharmony_cisetup_token_v2(struct krb5_ctx *ctx, struct xdr_netobj *token)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	u16 *ptr;
1028c2ecf20Sopenharmony_ci	void *krb5_hdr;
1038c2ecf20Sopenharmony_ci	u8 *p, flags = 0x00;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if ((ctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
1068c2ecf20Sopenharmony_ci		flags |= 0x01;
1078c2ecf20Sopenharmony_ci	if (ctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
1088c2ecf20Sopenharmony_ci		flags |= 0x04;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Per rfc 4121, sec 4.2.6.1, there is no header,
1118c2ecf20Sopenharmony_ci	 * just start the token */
1128c2ecf20Sopenharmony_ci	krb5_hdr = ptr = (u16 *)token->data;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	*ptr++ = KG2_TOK_MIC;
1158c2ecf20Sopenharmony_ci	p = (u8 *)ptr;
1168c2ecf20Sopenharmony_ci	*p++ = flags;
1178c2ecf20Sopenharmony_ci	*p++ = 0xff;
1188c2ecf20Sopenharmony_ci	ptr = (u16 *)p;
1198c2ecf20Sopenharmony_ci	*ptr++ = 0xffff;
1208c2ecf20Sopenharmony_ci	*ptr = 0xffff;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	token->len = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;
1238c2ecf20Sopenharmony_ci	return krb5_hdr;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic u32
1278c2ecf20Sopenharmony_cigss_get_mic_v1(struct krb5_ctx *ctx, struct xdr_buf *text,
1288c2ecf20Sopenharmony_ci		struct xdr_netobj *token)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	char			cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
1318c2ecf20Sopenharmony_ci	struct xdr_netobj	md5cksum = {.len = sizeof(cksumdata),
1328c2ecf20Sopenharmony_ci					    .data = cksumdata};
1338c2ecf20Sopenharmony_ci	void			*ptr;
1348c2ecf20Sopenharmony_ci	time64_t		now;
1358c2ecf20Sopenharmony_ci	u32			seq_send;
1368c2ecf20Sopenharmony_ci	u8			*cksumkey;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	dprintk("RPC:       %s\n", __func__);
1398c2ecf20Sopenharmony_ci	BUG_ON(ctx == NULL);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	now = ktime_get_real_seconds();
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ptr = setup_token(ctx, token);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (ctx->gk5e->keyed_cksum)
1468c2ecf20Sopenharmony_ci		cksumkey = ctx->cksum;
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		cksumkey = NULL;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (make_checksum(ctx, ptr, 8, text, 0, cksumkey,
1518c2ecf20Sopenharmony_ci			  KG_USAGE_SIGN, &md5cksum))
1528c2ecf20Sopenharmony_ci		return GSS_S_FAILURE;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	seq_send = atomic_fetch_inc(&ctx->seq_send);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (krb5_make_seq_num(ctx, ctx->seq, ctx->initiate ? 0 : 0xff,
1598c2ecf20Sopenharmony_ci			      seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8))
1608c2ecf20Sopenharmony_ci		return GSS_S_FAILURE;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic u32
1668c2ecf20Sopenharmony_cigss_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text,
1678c2ecf20Sopenharmony_ci		struct xdr_netobj *token)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
1708c2ecf20Sopenharmony_ci	struct xdr_netobj cksumobj = { .len = sizeof(cksumdata),
1718c2ecf20Sopenharmony_ci				       .data = cksumdata};
1728c2ecf20Sopenharmony_ci	void *krb5_hdr;
1738c2ecf20Sopenharmony_ci	time64_t now;
1748c2ecf20Sopenharmony_ci	u8 *cksumkey;
1758c2ecf20Sopenharmony_ci	unsigned int cksum_usage;
1768c2ecf20Sopenharmony_ci	__be64 seq_send_be64;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	dprintk("RPC:       %s\n", __func__);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	krb5_hdr = setup_token_v2(ctx, token);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Set up the sequence number. Now 64-bits in clear
1838c2ecf20Sopenharmony_ci	 * text and w/o direction indicator */
1848c2ecf20Sopenharmony_ci	seq_send_be64 = cpu_to_be64(atomic64_fetch_inc(&ctx->seq_send64));
1858c2ecf20Sopenharmony_ci	memcpy(krb5_hdr + 8, (char *) &seq_send_be64, 8);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (ctx->initiate) {
1888c2ecf20Sopenharmony_ci		cksumkey = ctx->initiator_sign;
1898c2ecf20Sopenharmony_ci		cksum_usage = KG_USAGE_INITIATOR_SIGN;
1908c2ecf20Sopenharmony_ci	} else {
1918c2ecf20Sopenharmony_ci		cksumkey = ctx->acceptor_sign;
1928c2ecf20Sopenharmony_ci		cksum_usage = KG_USAGE_ACCEPTOR_SIGN;
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (make_checksum_v2(ctx, krb5_hdr, GSS_KRB5_TOK_HDR_LEN,
1968c2ecf20Sopenharmony_ci			     text, 0, cksumkey, cksum_usage, &cksumobj))
1978c2ecf20Sopenharmony_ci		return GSS_S_FAILURE;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	memcpy(krb5_hdr + GSS_KRB5_TOK_HDR_LEN, cksumobj.data, cksumobj.len);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	now = ktime_get_real_seconds();
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciu32
2078c2ecf20Sopenharmony_cigss_get_mic_kerberos(struct gss_ctx *gss_ctx, struct xdr_buf *text,
2088c2ecf20Sopenharmony_ci		     struct xdr_netobj *token)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct krb5_ctx		*ctx = gss_ctx->internal_ctx_id;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	switch (ctx->enctype) {
2138c2ecf20Sopenharmony_ci	default:
2148c2ecf20Sopenharmony_ci		BUG();
2158c2ecf20Sopenharmony_ci	case ENCTYPE_DES_CBC_RAW:
2168c2ecf20Sopenharmony_ci	case ENCTYPE_DES3_CBC_RAW:
2178c2ecf20Sopenharmony_ci		return gss_get_mic_v1(ctx, text, token);
2188c2ecf20Sopenharmony_ci	case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
2198c2ecf20Sopenharmony_ci	case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
2208c2ecf20Sopenharmony_ci		return gss_get_mic_v2(ctx, text, token);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci}
223