162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  linux/net/sunrpc/gss_krb5_unseal.c
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5unseal.c
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *  Copyright (c) 2000-2008 The Regents of the University of Michigan.
762306a36Sopenharmony_ci *  All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  Andy Adamson   <andros@umich.edu>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/*
1362306a36Sopenharmony_ci * Copyright 1993 by OpenVision Technologies, Inc.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software
1662306a36Sopenharmony_ci * and its documentation for any purpose is hereby granted without fee,
1762306a36Sopenharmony_ci * provided that the above copyright notice appears in all copies and
1862306a36Sopenharmony_ci * that both that copyright notice and this permission notice appear in
1962306a36Sopenharmony_ci * supporting documentation, and that the name of OpenVision not be used
2062306a36Sopenharmony_ci * in advertising or publicity pertaining to distribution of the software
2162306a36Sopenharmony_ci * without specific, written prior permission. OpenVision makes no
2262306a36Sopenharmony_ci * representations about the suitability of this software for any
2362306a36Sopenharmony_ci * purpose.  It is provided "as is" without express or implied warranty.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
2662306a36Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
2762306a36Sopenharmony_ci * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
2862306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
2962306a36Sopenharmony_ci * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
3062306a36Sopenharmony_ci * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3162306a36Sopenharmony_ci * PERFORMANCE OF THIS SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * Copyright (C) 1998 by the FundsXpress, INC.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * All rights reserved.
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * Export of this software from the United States of America may require
4062306a36Sopenharmony_ci * a specific license from the United States Government.  It is the
4162306a36Sopenharmony_ci * responsibility of any person or organization contemplating export to
4262306a36Sopenharmony_ci * obtain such a license before exporting.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
4562306a36Sopenharmony_ci * distribute this software and its documentation for any purpose and
4662306a36Sopenharmony_ci * without fee is hereby granted, provided that the above copyright
4762306a36Sopenharmony_ci * notice appear in all copies and that both that copyright notice and
4862306a36Sopenharmony_ci * this permission notice appear in supporting documentation, and that
4962306a36Sopenharmony_ci * the name of FundsXpress. not be used in advertising or publicity pertaining
5062306a36Sopenharmony_ci * to distribution of the software without specific, written prior
5162306a36Sopenharmony_ci * permission.  FundsXpress makes no representations about the suitability of
5262306a36Sopenharmony_ci * this software for any purpose.  It is provided "as is" without express
5362306a36Sopenharmony_ci * or implied warranty.
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
5662306a36Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
5762306a36Sopenharmony_ci * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#include <crypto/algapi.h>
6162306a36Sopenharmony_ci#include <linux/types.h>
6262306a36Sopenharmony_ci#include <linux/jiffies.h>
6362306a36Sopenharmony_ci#include <linux/sunrpc/gss_krb5.h>
6462306a36Sopenharmony_ci#include <linux/crypto.h>
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#include "gss_krb5_internal.h"
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
6962306a36Sopenharmony_ci# define RPCDBG_FACILITY        RPCDBG_AUTH
7062306a36Sopenharmony_ci#endif
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciu32
7362306a36Sopenharmony_cigss_krb5_verify_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *message_buffer,
7462306a36Sopenharmony_ci		       struct xdr_netobj *read_token)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct crypto_ahash *tfm = ctx->initiate ?
7762306a36Sopenharmony_ci				   ctx->acceptor_sign : ctx->initiator_sign;
7862306a36Sopenharmony_ci	char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];
7962306a36Sopenharmony_ci	struct xdr_netobj cksumobj = {
8062306a36Sopenharmony_ci		.len	= ctx->gk5e->cksumlength,
8162306a36Sopenharmony_ci		.data	= cksumdata,
8262306a36Sopenharmony_ci	};
8362306a36Sopenharmony_ci	u8 *ptr = read_token->data;
8462306a36Sopenharmony_ci	__be16 be16_ptr;
8562306a36Sopenharmony_ci	time64_t now;
8662306a36Sopenharmony_ci	u8 flags;
8762306a36Sopenharmony_ci	int i;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	dprintk("RPC:       %s\n", __func__);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	memcpy(&be16_ptr, (char *) ptr, 2);
9262306a36Sopenharmony_ci	if (be16_to_cpu(be16_ptr) != KG2_TOK_MIC)
9362306a36Sopenharmony_ci		return GSS_S_DEFECTIVE_TOKEN;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	flags = ptr[2];
9662306a36Sopenharmony_ci	if ((!ctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||
9762306a36Sopenharmony_ci	    (ctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))
9862306a36Sopenharmony_ci		return GSS_S_BAD_SIG;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	if (flags & KG2_TOKEN_FLAG_SEALED) {
10162306a36Sopenharmony_ci		dprintk("%s: token has unexpected sealed flag\n", __func__);
10262306a36Sopenharmony_ci		return GSS_S_FAILURE;
10362306a36Sopenharmony_ci	}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	for (i = 3; i < 8; i++)
10662306a36Sopenharmony_ci		if (ptr[i] != 0xff)
10762306a36Sopenharmony_ci			return GSS_S_DEFECTIVE_TOKEN;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	if (gss_krb5_checksum(tfm, ptr, GSS_KRB5_TOK_HDR_LEN,
11062306a36Sopenharmony_ci			      message_buffer, 0, &cksumobj))
11162306a36Sopenharmony_ci		return GSS_S_FAILURE;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	if (memcmp(cksumobj.data, ptr + GSS_KRB5_TOK_HDR_LEN,
11462306a36Sopenharmony_ci				ctx->gk5e->cksumlength))
11562306a36Sopenharmony_ci		return GSS_S_BAD_SIG;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/* it got through unscathed.  Make sure the context is unexpired */
11862306a36Sopenharmony_ci	now = ktime_get_real_seconds();
11962306a36Sopenharmony_ci	if (now > ctx->endtime)
12062306a36Sopenharmony_ci		return GSS_S_CONTEXT_EXPIRED;
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	/*
12362306a36Sopenharmony_ci	 * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
12462306a36Sopenharmony_ci	 * doesn't want it checked; see page 6 of rfc 2203.
12562306a36Sopenharmony_ci	 */
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	return GSS_S_COMPLETE;
12862306a36Sopenharmony_ci}
129