162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  linux/net/sunrpc/gss_krb5_seal.c
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5seal.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 *  J. Bruce Fields	<bfields@umich.edu>
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * Copyright 1993 by OpenVision Technologies, Inc.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software
1762306a36Sopenharmony_ci * and its documentation for any purpose is hereby granted without fee,
1862306a36Sopenharmony_ci * provided that the above copyright notice appears in all copies and
1962306a36Sopenharmony_ci * that both that copyright notice and this permission notice appear in
2062306a36Sopenharmony_ci * supporting documentation, and that the name of OpenVision not be used
2162306a36Sopenharmony_ci * in advertising or publicity pertaining to distribution of the software
2262306a36Sopenharmony_ci * without specific, written prior permission. OpenVision makes no
2362306a36Sopenharmony_ci * representations about the suitability of this software for any
2462306a36Sopenharmony_ci * purpose.  It is provided "as is" without express or implied warranty.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
2762306a36Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
2862306a36Sopenharmony_ci * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
2962306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
3062306a36Sopenharmony_ci * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
3162306a36Sopenharmony_ci * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3262306a36Sopenharmony_ci * PERFORMANCE OF THIS SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/*
3662306a36Sopenharmony_ci * Copyright (C) 1998 by the FundsXpress, INC.
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * All rights reserved.
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * Export of this software from the United States of America may require
4162306a36Sopenharmony_ci * a specific license from the United States Government.  It is the
4262306a36Sopenharmony_ci * responsibility of any person or organization contemplating export to
4362306a36Sopenharmony_ci * obtain such a license before exporting.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
4662306a36Sopenharmony_ci * distribute this software and its documentation for any purpose and
4762306a36Sopenharmony_ci * without fee is hereby granted, provided that the above copyright
4862306a36Sopenharmony_ci * notice appear in all copies and that both that copyright notice and
4962306a36Sopenharmony_ci * this permission notice appear in supporting documentation, and that
5062306a36Sopenharmony_ci * the name of FundsXpress. not be used in advertising or publicity pertaining
5162306a36Sopenharmony_ci * to distribution of the software without specific, written prior
5262306a36Sopenharmony_ci * permission.  FundsXpress makes no representations about the suitability of
5362306a36Sopenharmony_ci * this software for any purpose.  It is provided "as is" without express
5462306a36Sopenharmony_ci * or implied warranty.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
5762306a36Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
5862306a36Sopenharmony_ci * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#include <linux/types.h>
6262306a36Sopenharmony_ci#include <linux/jiffies.h>
6362306a36Sopenharmony_ci#include <linux/sunrpc/gss_krb5.h>
6462306a36Sopenharmony_ci#include <linux/random.h>
6562306a36Sopenharmony_ci#include <linux/crypto.h>
6662306a36Sopenharmony_ci#include <linux/atomic.h>
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#include "gss_krb5_internal.h"
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
7162306a36Sopenharmony_ci# define RPCDBG_FACILITY        RPCDBG_AUTH
7262306a36Sopenharmony_ci#endif
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic void *
7562306a36Sopenharmony_cisetup_token_v2(struct krb5_ctx *ctx, struct xdr_netobj *token)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	u16 *ptr;
7862306a36Sopenharmony_ci	void *krb5_hdr;
7962306a36Sopenharmony_ci	u8 *p, flags = 0x00;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	if ((ctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)
8262306a36Sopenharmony_ci		flags |= 0x01;
8362306a36Sopenharmony_ci	if (ctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)
8462306a36Sopenharmony_ci		flags |= 0x04;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/* Per rfc 4121, sec 4.2.6.1, there is no header,
8762306a36Sopenharmony_ci	 * just start the token.
8862306a36Sopenharmony_ci	 */
8962306a36Sopenharmony_ci	krb5_hdr = (u16 *)token->data;
9062306a36Sopenharmony_ci	ptr = krb5_hdr;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	*ptr++ = KG2_TOK_MIC;
9362306a36Sopenharmony_ci	p = (u8 *)ptr;
9462306a36Sopenharmony_ci	*p++ = flags;
9562306a36Sopenharmony_ci	*p++ = 0xff;
9662306a36Sopenharmony_ci	ptr = (u16 *)p;
9762306a36Sopenharmony_ci	*ptr++ = 0xffff;
9862306a36Sopenharmony_ci	*ptr = 0xffff;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	token->len = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;
10162306a36Sopenharmony_ci	return krb5_hdr;
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ciu32
10562306a36Sopenharmony_cigss_krb5_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text,
10662306a36Sopenharmony_ci		    struct xdr_netobj *token)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct crypto_ahash *tfm = ctx->initiate ?
10962306a36Sopenharmony_ci				   ctx->initiator_sign : ctx->acceptor_sign;
11062306a36Sopenharmony_ci	struct xdr_netobj cksumobj = {
11162306a36Sopenharmony_ci		.len =	ctx->gk5e->cksumlength,
11262306a36Sopenharmony_ci	};
11362306a36Sopenharmony_ci	__be64 seq_send_be64;
11462306a36Sopenharmony_ci	void *krb5_hdr;
11562306a36Sopenharmony_ci	time64_t now;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	dprintk("RPC:       %s\n", __func__);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	krb5_hdr = setup_token_v2(ctx, token);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* Set up the sequence number. Now 64-bits in clear
12262306a36Sopenharmony_ci	 * text and w/o direction indicator */
12362306a36Sopenharmony_ci	seq_send_be64 = cpu_to_be64(atomic64_fetch_inc(&ctx->seq_send64));
12462306a36Sopenharmony_ci	memcpy(krb5_hdr + 8, (char *) &seq_send_be64, 8);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	cksumobj.data = krb5_hdr + GSS_KRB5_TOK_HDR_LEN;
12762306a36Sopenharmony_ci	if (gss_krb5_checksum(tfm, krb5_hdr, GSS_KRB5_TOK_HDR_LEN,
12862306a36Sopenharmony_ci			      text, 0, &cksumobj))
12962306a36Sopenharmony_ci		return GSS_S_FAILURE;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	now = ktime_get_real_seconds();
13262306a36Sopenharmony_ci	return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;
13362306a36Sopenharmony_ci}
134