Path: blob/master/net/sunrpc/auth_gss/gss_krb5_seal.c
15112 views
/*1* linux/net/sunrpc/gss_krb5_seal.c2*3* Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5seal.c4*5* Copyright (c) 2000-2008 The Regents of the University of Michigan.6* All rights reserved.7*8* Andy Adamson <[email protected]>9* J. Bruce Fields <[email protected]>10*/1112/*13* Copyright 1993 by OpenVision Technologies, Inc.14*15* Permission to use, copy, modify, distribute, and sell this software16* and its documentation for any purpose is hereby granted without fee,17* provided that the above copyright notice appears in all copies and18* that both that copyright notice and this permission notice appear in19* supporting documentation, and that the name of OpenVision not be used20* in advertising or publicity pertaining to distribution of the software21* without specific, written prior permission. OpenVision makes no22* representations about the suitability of this software for any23* purpose. It is provided "as is" without express or implied warranty.24*25* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,26* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO27* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR28* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF29* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR30* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR31* PERFORMANCE OF THIS SOFTWARE.32*/3334/*35* Copyright (C) 1998 by the FundsXpress, INC.36*37* All rights reserved.38*39* Export of this software from the United States of America may require40* a specific license from the United States Government. It is the41* responsibility of any person or organization contemplating export to42* obtain such a license before exporting.43*44* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and45* distribute this software and its documentation for any purpose and46* without fee is hereby granted, provided that the above copyright47* notice appear in all copies and that both that copyright notice and48* this permission notice appear in supporting documentation, and that49* the name of FundsXpress. not be used in advertising or publicity pertaining50* to distribution of the software without specific, written prior51* permission. FundsXpress makes no representations about the suitability of52* this software for any purpose. It is provided "as is" without express53* or implied warranty.54*55* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR56* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED57* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.58*/5960#include <linux/types.h>61#include <linux/jiffies.h>62#include <linux/sunrpc/gss_krb5.h>63#include <linux/random.h>64#include <linux/crypto.h>6566#ifdef RPC_DEBUG67# define RPCDBG_FACILITY RPCDBG_AUTH68#endif6970DEFINE_SPINLOCK(krb5_seq_lock);7172static char *73setup_token(struct krb5_ctx *ctx, struct xdr_netobj *token)74{75__be16 *ptr, *krb5_hdr;76int body_size = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;7778token->len = g_token_size(&ctx->mech_used, body_size);7980ptr = (__be16 *)token->data;81g_make_token_header(&ctx->mech_used, body_size, (unsigned char **)&ptr);8283/* ptr now at start of header described in rfc 1964, section 1.2.1: */84krb5_hdr = ptr;85*ptr++ = KG_TOK_MIC_MSG;86*ptr++ = cpu_to_le16(ctx->gk5e->signalg);87*ptr++ = SEAL_ALG_NONE;88*ptr++ = 0xffff;8990return (char *)krb5_hdr;91}9293static void *94setup_token_v2(struct krb5_ctx *ctx, struct xdr_netobj *token)95{96__be16 *ptr, *krb5_hdr;97u8 *p, flags = 0x00;9899if ((ctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0)100flags |= 0x01;101if (ctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY)102flags |= 0x04;103104/* Per rfc 4121, sec 4.2.6.1, there is no header,105* just start the token */106krb5_hdr = ptr = (__be16 *)token->data;107108*ptr++ = KG2_TOK_MIC;109p = (u8 *)ptr;110*p++ = flags;111*p++ = 0xff;112ptr = (__be16 *)p;113*ptr++ = 0xffff;114*ptr++ = 0xffff;115116token->len = GSS_KRB5_TOK_HDR_LEN + ctx->gk5e->cksumlength;117return krb5_hdr;118}119120static u32121gss_get_mic_v1(struct krb5_ctx *ctx, struct xdr_buf *text,122struct xdr_netobj *token)123{124char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];125struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),126.data = cksumdata};127void *ptr;128s32 now;129u32 seq_send;130u8 *cksumkey;131132dprintk("RPC: %s\n", __func__);133BUG_ON(ctx == NULL);134135now = get_seconds();136137ptr = setup_token(ctx, token);138139if (ctx->gk5e->keyed_cksum)140cksumkey = ctx->cksum;141else142cksumkey = NULL;143144if (make_checksum(ctx, ptr, 8, text, 0, cksumkey,145KG_USAGE_SIGN, &md5cksum))146return GSS_S_FAILURE;147148memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len);149150spin_lock(&krb5_seq_lock);151seq_send = ctx->seq_send++;152spin_unlock(&krb5_seq_lock);153154if (krb5_make_seq_num(ctx, ctx->seq, ctx->initiate ? 0 : 0xff,155seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8))156return GSS_S_FAILURE;157158return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;159}160161u32162gss_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text,163struct xdr_netobj *token)164{165char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];166struct xdr_netobj cksumobj = { .len = sizeof(cksumdata),167.data = cksumdata};168void *krb5_hdr;169s32 now;170u64 seq_send;171u8 *cksumkey;172unsigned int cksum_usage;173174dprintk("RPC: %s\n", __func__);175176krb5_hdr = setup_token_v2(ctx, token);177178/* Set up the sequence number. Now 64-bits in clear179* text and w/o direction indicator */180spin_lock(&krb5_seq_lock);181seq_send = ctx->seq_send64++;182spin_unlock(&krb5_seq_lock);183*((u64 *)(krb5_hdr + 8)) = cpu_to_be64(seq_send);184185if (ctx->initiate) {186cksumkey = ctx->initiator_sign;187cksum_usage = KG_USAGE_INITIATOR_SIGN;188} else {189cksumkey = ctx->acceptor_sign;190cksum_usage = KG_USAGE_ACCEPTOR_SIGN;191}192193if (make_checksum_v2(ctx, krb5_hdr, GSS_KRB5_TOK_HDR_LEN,194text, 0, cksumkey, cksum_usage, &cksumobj))195return GSS_S_FAILURE;196197memcpy(krb5_hdr + GSS_KRB5_TOK_HDR_LEN, cksumobj.data, cksumobj.len);198199now = get_seconds();200201return (ctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE;202}203204u32205gss_get_mic_kerberos(struct gss_ctx *gss_ctx, struct xdr_buf *text,206struct xdr_netobj *token)207{208struct krb5_ctx *ctx = gss_ctx->internal_ctx_id;209210switch (ctx->enctype) {211default:212BUG();213case ENCTYPE_DES_CBC_RAW:214case ENCTYPE_DES3_CBC_RAW:215case ENCTYPE_ARCFOUR_HMAC:216return gss_get_mic_v1(ctx, text, token);217case ENCTYPE_AES128_CTS_HMAC_SHA1_96:218case ENCTYPE_AES256_CTS_HMAC_SHA1_96:219return gss_get_mic_v2(ctx, text, token);220}221}222223224225