Path: blob/master/net/sunrpc/auth_gss/gss_krb5_unseal.c
15112 views
/*1* linux/net/sunrpc/gss_krb5_unseal.c2*3* Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5unseal.c4*5* Copyright (c) 2000-2008 The Regents of the University of Michigan.6* All rights reserved.7*8* Andy Adamson <[email protected]>9*/1011/*12* Copyright 1993 by OpenVision Technologies, Inc.13*14* Permission to use, copy, modify, distribute, and sell this software15* and its documentation for any purpose is hereby granted without fee,16* provided that the above copyright notice appears in all copies and17* that both that copyright notice and this permission notice appear in18* supporting documentation, and that the name of OpenVision not be used19* in advertising or publicity pertaining to distribution of the software20* without specific, written prior permission. OpenVision makes no21* representations about the suitability of this software for any22* purpose. It is provided "as is" without express or implied warranty.23*24* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,25* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO26* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR27* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF28* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR29* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR30* PERFORMANCE OF THIS SOFTWARE.31*/3233/*34* Copyright (C) 1998 by the FundsXpress, INC.35*36* All rights reserved.37*38* Export of this software from the United States of America may require39* a specific license from the United States Government. It is the40* responsibility of any person or organization contemplating export to41* obtain such a license before exporting.42*43* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and44* distribute this software and its documentation for any purpose and45* without fee is hereby granted, provided that the above copyright46* notice appear in all copies and that both that copyright notice and47* this permission notice appear in supporting documentation, and that48* the name of FundsXpress. not be used in advertising or publicity pertaining49* to distribution of the software without specific, written prior50* permission. FundsXpress makes no representations about the suitability of51* this software for any purpose. It is provided "as is" without express52* or implied warranty.53*54* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR55* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED56* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.57*/5859#include <linux/types.h>60#include <linux/jiffies.h>61#include <linux/sunrpc/gss_krb5.h>62#include <linux/crypto.h>6364#ifdef RPC_DEBUG65# define RPCDBG_FACILITY RPCDBG_AUTH66#endif676869/* read_token is a mic token, and message_buffer is the data that the mic was70* supposedly taken over. */7172static u3273gss_verify_mic_v1(struct krb5_ctx *ctx,74struct xdr_buf *message_buffer, struct xdr_netobj *read_token)75{76int signalg;77int sealalg;78char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];79struct xdr_netobj md5cksum = {.len = sizeof(cksumdata),80.data = cksumdata};81s32 now;82int direction;83u32 seqnum;84unsigned char *ptr = (unsigned char *)read_token->data;85int bodysize;86u8 *cksumkey;8788dprintk("RPC: krb5_read_token\n");8990if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr,91read_token->len))92return GSS_S_DEFECTIVE_TOKEN;9394if ((ptr[0] != ((KG_TOK_MIC_MSG >> 8) & 0xff)) ||95(ptr[1] != (KG_TOK_MIC_MSG & 0xff)))96return GSS_S_DEFECTIVE_TOKEN;9798/* XXX sanity-check bodysize?? */99100signalg = ptr[2] + (ptr[3] << 8);101if (signalg != ctx->gk5e->signalg)102return GSS_S_DEFECTIVE_TOKEN;103104sealalg = ptr[4] + (ptr[5] << 8);105if (sealalg != SEAL_ALG_NONE)106return GSS_S_DEFECTIVE_TOKEN;107108if ((ptr[6] != 0xff) || (ptr[7] != 0xff))109return GSS_S_DEFECTIVE_TOKEN;110111if (ctx->gk5e->keyed_cksum)112cksumkey = ctx->cksum;113else114cksumkey = NULL;115116if (make_checksum(ctx, ptr, 8, message_buffer, 0,117cksumkey, KG_USAGE_SIGN, &md5cksum))118return GSS_S_FAILURE;119120if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN,121ctx->gk5e->cksumlength))122return GSS_S_BAD_SIG;123124/* it got through unscathed. Make sure the context is unexpired */125126now = get_seconds();127128if (now > ctx->endtime)129return GSS_S_CONTEXT_EXPIRED;130131/* do sequencing checks */132133if (krb5_get_seq_num(ctx, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8,134&direction, &seqnum))135return GSS_S_FAILURE;136137if ((ctx->initiate && direction != 0xff) ||138(!ctx->initiate && direction != 0))139return GSS_S_BAD_SIG;140141return GSS_S_COMPLETE;142}143144static u32145gss_verify_mic_v2(struct krb5_ctx *ctx,146struct xdr_buf *message_buffer, struct xdr_netobj *read_token)147{148char cksumdata[GSS_KRB5_MAX_CKSUM_LEN];149struct xdr_netobj cksumobj = {.len = sizeof(cksumdata),150.data = cksumdata};151s32 now;152u64 seqnum;153u8 *ptr = read_token->data;154u8 *cksumkey;155u8 flags;156int i;157unsigned int cksum_usage;158159dprintk("RPC: %s\n", __func__);160161if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_MIC)162return GSS_S_DEFECTIVE_TOKEN;163164flags = ptr[2];165if ((!ctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) ||166(ctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)))167return GSS_S_BAD_SIG;168169if (flags & KG2_TOKEN_FLAG_SEALED) {170dprintk("%s: token has unexpected sealed flag\n", __func__);171return GSS_S_FAILURE;172}173174for (i = 3; i < 8; i++)175if (ptr[i] != 0xff)176return GSS_S_DEFECTIVE_TOKEN;177178if (ctx->initiate) {179cksumkey = ctx->acceptor_sign;180cksum_usage = KG_USAGE_ACCEPTOR_SIGN;181} else {182cksumkey = ctx->initiator_sign;183cksum_usage = KG_USAGE_INITIATOR_SIGN;184}185186if (make_checksum_v2(ctx, ptr, GSS_KRB5_TOK_HDR_LEN, message_buffer, 0,187cksumkey, cksum_usage, &cksumobj))188return GSS_S_FAILURE;189190if (memcmp(cksumobj.data, ptr + GSS_KRB5_TOK_HDR_LEN,191ctx->gk5e->cksumlength))192return GSS_S_BAD_SIG;193194/* it got through unscathed. Make sure the context is unexpired */195now = get_seconds();196if (now > ctx->endtime)197return GSS_S_CONTEXT_EXPIRED;198199/* do sequencing checks */200201seqnum = be64_to_cpup((__be64 *)ptr + 8);202203return GSS_S_COMPLETE;204}205206u32207gss_verify_mic_kerberos(struct gss_ctx *gss_ctx,208struct xdr_buf *message_buffer,209struct xdr_netobj *read_token)210{211struct krb5_ctx *ctx = gss_ctx->internal_ctx_id;212213switch (ctx->enctype) {214default:215BUG();216case ENCTYPE_DES_CBC_RAW:217case ENCTYPE_DES3_CBC_RAW:218case ENCTYPE_ARCFOUR_HMAC:219return gss_verify_mic_v1(ctx, message_buffer, read_token);220case ENCTYPE_AES128_CTS_HMAC_SHA1_96:221case ENCTYPE_AES256_CTS_HMAC_SHA1_96:222return gss_verify_mic_v2(ctx, message_buffer, read_token);223}224}225226227228