xref: /onnv-gate/usr/src/uts/common/gssapi/mechs/krb5/mech/k5unseal.c (revision 13132:9615cdbf7b70)
10Sstevel@tonic-gate /*
2*13132SGlenn.Barry@oracle.com  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate /*
57934SMark.Phalan@Sun.COM  * Copyright 2001 by the Massachusetts Institute of Technology.
60Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
77934SMark.Phalan@Sun.COM  *
80Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
90Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
100Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
110Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
120Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
130Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
140Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
150Sstevel@tonic-gate  * representations about the suitability of this software for any
160Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
177934SMark.Phalan@Sun.COM  *
180Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
190Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
200Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
210Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
220Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
230Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
240Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * All rights reserved.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Export of this software from the United States of America may require
330Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
340Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
350Sstevel@tonic-gate  * obtain such a license before exporting.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
380Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
390Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
400Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
410Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
420Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
430Sstevel@tonic-gate  * to distribution of the software without specific, written prior
440Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
450Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
460Sstevel@tonic-gate  * or implied warranty.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
490Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
500Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
537934SMark.Phalan@Sun.COM #include "gssapiP_krb5.h"
547934SMark.Phalan@Sun.COM #include "k5-int.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* message_buffer is an input if SIGN, output if SEAL, and ignored if DEL_CTX
577934SMark.Phalan@Sun.COM    conf_state is only valid if SEAL. */
580Sstevel@tonic-gate 
597934SMark.Phalan@Sun.COM static OM_uint32
kg_unseal_v1(context,minor_status,ctx,ptr,bodysize,message_buffer,conf_state,qop_state,toktype)600Sstevel@tonic-gate kg_unseal_v1(context, minor_status, ctx, ptr, bodysize, message_buffer,
610Sstevel@tonic-gate 	     conf_state, qop_state, toktype)
620Sstevel@tonic-gate     krb5_context context;
630Sstevel@tonic-gate     OM_uint32 *minor_status;
640Sstevel@tonic-gate     krb5_gss_ctx_id_rec *ctx;
650Sstevel@tonic-gate     unsigned char *ptr;
660Sstevel@tonic-gate     int bodysize;
670Sstevel@tonic-gate     gss_buffer_t message_buffer;
680Sstevel@tonic-gate     int *conf_state;
690Sstevel@tonic-gate     int *qop_state;
700Sstevel@tonic-gate     int toktype;
710Sstevel@tonic-gate {
720Sstevel@tonic-gate     krb5_error_code code;
730Sstevel@tonic-gate     int conflen = 0;
740Sstevel@tonic-gate     int signalg;
750Sstevel@tonic-gate     int sealalg;
760Sstevel@tonic-gate     gss_buffer_desc token;
770Sstevel@tonic-gate     krb5_checksum cksum;
780Sstevel@tonic-gate     krb5_checksum md5cksum;
790Sstevel@tonic-gate     krb5_data plaind;
800Sstevel@tonic-gate     char *data_ptr;
810Sstevel@tonic-gate     krb5_timestamp now;
820Sstevel@tonic-gate     unsigned char *plain;
837934SMark.Phalan@Sun.COM     unsigned int cksum_len = 0;
847934SMark.Phalan@Sun.COM     size_t plainlen;
850Sstevel@tonic-gate     int direction;
860Sstevel@tonic-gate     krb5_ui_4 seqnum;
870Sstevel@tonic-gate     OM_uint32 retval;
886815Ssemery     size_t sumlen, blocksize;
890Sstevel@tonic-gate     int tmsglen;
900Sstevel@tonic-gate     krb5_keyusage sign_usage = KG_USAGE_SIGN;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() start\n");
930Sstevel@tonic-gate 
940Sstevel@tonic-gate     /* Solaris Kerberos:  make sure this is initialized */
950Sstevel@tonic-gate     *minor_status = 0;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate     if (toktype == KG_TOK_SEAL_MSG) {
980Sstevel@tonic-gate 	message_buffer->length = 0;
990Sstevel@tonic-gate 	message_buffer->value = NULL;
1000Sstevel@tonic-gate     }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate     /* get the sign and seal algorithms */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate     signalg = ptr[0] + (ptr[1]<<8);
1050Sstevel@tonic-gate     sealalg = ptr[2] + (ptr[3]<<8);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate     /* Sanity checks */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate     if ((ptr[4] != 0xff) || (ptr[5] != 0xff)) {
1100Sstevel@tonic-gate 	*minor_status = 0;
1110Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error GSS_S_DEFECTIVE_TOKEN\n");
1120Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
1130Sstevel@tonic-gate     }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate     if ((toktype != KG_TOK_SEAL_MSG) &&
1160Sstevel@tonic-gate 	(sealalg != 0xffff)) {
1170Sstevel@tonic-gate 	*minor_status = 0;
1180Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error2 GSS_S_DEFECTIVE_TOKEN\n");
1190Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
1200Sstevel@tonic-gate     }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate     /* in the current spec, there is only one valid seal algorithm per
1230Sstevel@tonic-gate        key type, so a simple comparison is ok */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate     if ((toktype == KG_TOK_SEAL_MSG) &&
1260Sstevel@tonic-gate 	!((sealalg == 0xffff) ||
1270Sstevel@tonic-gate 	  (sealalg == ctx->sealalg))) {
1280Sstevel@tonic-gate 	*minor_status = 0;
1290Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error3 GSS_S_DEFECTIVE_TOKEN\n");
1300Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
1310Sstevel@tonic-gate     }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate     /* there are several mappings of seal algorithms to sign algorithms,
1340Sstevel@tonic-gate        but few enough that we can try them all. */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate     if ((ctx->sealalg == SEAL_ALG_NONE && signalg > 1) ||
1377934SMark.Phalan@Sun.COM 	(ctx->sealalg == SEAL_ALG_1 && signalg != SGN_ALG_3) ||
1380Sstevel@tonic-gate 	(ctx->sealalg == SEAL_ALG_DES3KD &&
1397934SMark.Phalan@Sun.COM 	 signalg != SGN_ALG_HMAC_SHA1_DES3_KD)||
1400Sstevel@tonic-gate 	(ctx->sealalg == SEAL_ALG_MICROSOFT_RC4 &&
1417934SMark.Phalan@Sun.COM 	signalg != SGN_ALG_HMAC_MD5)) {
1420Sstevel@tonic-gate 	*minor_status = 0;
1430Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error4 GSS_S_DEFECTIVE_TOKEN\n");
1440Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
1450Sstevel@tonic-gate     }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate     KRB5_LOG(KRB5_INFO, "kg_unseal_v1() signalg = %d\n", signalg);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate     switch (signalg) {
1500Sstevel@tonic-gate     case SGN_ALG_DES_MAC_MD5:
1510Sstevel@tonic-gate     case SGN_ALG_MD2_5:
1520Sstevel@tonic-gate     case SGN_ALG_HMAC_MD5:
1530Sstevel@tonic-gate 	cksum_len = 8;
1540Sstevel@tonic-gate 	if (toktype != KG_TOK_SEAL_MSG)
1550Sstevel@tonic-gate 	  sign_usage = 15;
1567934SMark.Phalan@Sun.COM 	    break;
1570Sstevel@tonic-gate     case SGN_ALG_3:
1580Sstevel@tonic-gate 	cksum_len = 16;
1590Sstevel@tonic-gate 	break;
1600Sstevel@tonic-gate     case SGN_ALG_HMAC_SHA1_DES3_KD:
1610Sstevel@tonic-gate 	cksum_len = 20;
1620Sstevel@tonic-gate 	break;
1630Sstevel@tonic-gate     default:
1640Sstevel@tonic-gate 	*minor_status = 0;
1650Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, error signalg=%d\n", signalg);
1660Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
1670Sstevel@tonic-gate     }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate #ifdef _KERNEL
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Because the ARCFOUR code bypasses the standard
1720Sstevel@tonic-gate 	 * crypto interfaces, we must make sure the kernel
1730Sstevel@tonic-gate 	 * crypto framework mechanism types are properly
1740Sstevel@tonic-gate 	 * initialized here.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	context->kef_cipher_mt = get_cipher_mech_type(context,
1770Sstevel@tonic-gate 					ctx->seq);
1780Sstevel@tonic-gate 	context->kef_hash_mt = get_hash_mech_type(context,
1790Sstevel@tonic-gate 					ctx->seq);
1800Sstevel@tonic-gate 	if ((code = init_key_kef(context->kef_cipher_mt,
1810Sstevel@tonic-gate 				ctx->seq))) {
1820Sstevel@tonic-gate 		*minor_status = code;
1830Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	if ((code = init_key_kef(context->kef_cipher_mt,
1860Sstevel@tonic-gate 			ctx->enc))) {
1870Sstevel@tonic-gate 		*minor_status = code;
1880Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate #endif /* _KERNEL */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate     /* get the token parameters */
1937934SMark.Phalan@Sun.COM 
1940Sstevel@tonic-gate     if ((code = kg_get_seq_num(context, ctx->seq, ptr+14, ptr+6, &direction,
1957934SMark.Phalan@Sun.COM 			       &seqnum))) {
1960Sstevel@tonic-gate 	*minor_status = code;
1970Sstevel@tonic-gate 	return(GSS_S_BAD_SIG);
1980Sstevel@tonic-gate     }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate     /* decode the message, if SEAL */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate     if (toktype == KG_TOK_SEAL_MSG) {
2030Sstevel@tonic-gate 	tmsglen = bodysize-(14+cksum_len);
2040Sstevel@tonic-gate 	KRB5_LOG1(KRB5_INFO, "kg_unseal_v1() tmsglen = %d cksum_len = %d",
2050Sstevel@tonic-gate 		tmsglen, cksum_len);
2060Sstevel@tonic-gate 	KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() toktype == KG_TOK_SEAL_MSG\n");
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (sealalg != 0xffff) {
2090Sstevel@tonic-gate 	    if ((plain = (unsigned char *) xmalloc(tmsglen)) == NULL) {
2100Sstevel@tonic-gate 		*minor_status = ENOMEM;
2110Sstevel@tonic-gate 		KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error ENOMEM\n");
2120Sstevel@tonic-gate 		return(GSS_S_FAILURE);
2130Sstevel@tonic-gate 	    }
2147934SMark.Phalan@Sun.COM 	    if (ctx->enc->enctype == ENCTYPE_ARCFOUR_HMAC) {
2157934SMark.Phalan@Sun.COM 	      unsigned char bigend_seqnum[4];
2167934SMark.Phalan@Sun.COM 	      krb5_keyblock *enc_key;
2177934SMark.Phalan@Sun.COM 	      int i;
2187934SMark.Phalan@Sun.COM 	      bigend_seqnum[0] = (seqnum>>24) & 0xff;
2197934SMark.Phalan@Sun.COM 	      bigend_seqnum[1] = (seqnum>>16) & 0xff;
2207934SMark.Phalan@Sun.COM 	      bigend_seqnum[2] = (seqnum>>8) & 0xff;
2217934SMark.Phalan@Sun.COM 	      bigend_seqnum[3] = seqnum & 0xff;
2227934SMark.Phalan@Sun.COM 	      code = krb5_copy_keyblock (context, ctx->enc, &enc_key);
2237934SMark.Phalan@Sun.COM 	      if (code)
2240Sstevel@tonic-gate 		{
2257934SMark.Phalan@Sun.COM 		  xfree_wrap(plain, tmsglen);
2260Sstevel@tonic-gate 		  *minor_status = code;
2270Sstevel@tonic-gate 		  return(GSS_S_FAILURE);
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 
2307934SMark.Phalan@Sun.COM 	      for (i = 0; i <= 15; i++)
2317934SMark.Phalan@Sun.COM 		((char *) enc_key->contents)[i] ^=0xf0;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate #ifndef _KERNEL
2340Sstevel@tonic-gate 		/*
2350Sstevel@tonic-gate 		 * The enc_key contents were modified, delete the
2360Sstevel@tonic-gate 		 * key object so it doesn't get used later.
2370Sstevel@tonic-gate 		 */
2380Sstevel@tonic-gate 		if (enc_key->hKey != CK_INVALID_HANDLE) {
2390Sstevel@tonic-gate 			(void)C_DestroyObject(krb_ctx_hSession(context),
2400Sstevel@tonic-gate 				enc_key->hKey);
2410Sstevel@tonic-gate 			enc_key->hKey = CK_INVALID_HANDLE;
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate 		KRB5_LOG(KRB5_INFO, "kg_unseal_v1() enc_key->enctype = %d",
2450Sstevel@tonic-gate 			enc_key->enctype);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		code = kg_arcfour_docrypt (context,
2480Sstevel@tonic-gate 				enc_key, 0,
2490Sstevel@tonic-gate 				&bigend_seqnum[0], 4,
2500Sstevel@tonic-gate 				ptr+14+cksum_len, tmsglen,
2510Sstevel@tonic-gate 				plain);
2520Sstevel@tonic-gate 		krb5_free_keyblock (context, enc_key);
2530Sstevel@tonic-gate             } else {
2540Sstevel@tonic-gate 		code = kg_decrypt(context, ctx->enc, KG_USAGE_SEAL, NULL,
2550Sstevel@tonic-gate 			ptr+14+cksum_len, plain, tmsglen);
2560Sstevel@tonic-gate 	    }
2570Sstevel@tonic-gate             if (code) {
2580Sstevel@tonic-gate                 xfree_wrap(plain, tmsglen);
2590Sstevel@tonic-gate 		*minor_status = code;
2600Sstevel@tonic-gate 		return(GSS_S_FAILURE);
2617934SMark.Phalan@Sun.COM 	    }
2620Sstevel@tonic-gate 	} else {
2630Sstevel@tonic-gate 	    plain = ptr+14+cksum_len;
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	plainlen = tmsglen;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if ((sealalg == 0xffff) && ctx->big_endian) {
2690Sstevel@tonic-gate 	    token.length = tmsglen;
2700Sstevel@tonic-gate 	} else {
2710Sstevel@tonic-gate 	    conflen = kg_confounder_size(context, ctx->enc);
2726815Ssemery 	    /*
2736815Ssemery 	     * Solaris Kerberos: we want to perform a sanity check on the
2746815Ssemery 	     * pad length, so we know it can not be more than the blocksize.
2756815Ssemery 	     */
2766815Ssemery 	    code = krb5_c_block_size(context, ctx->enc->enctype, &blocksize);
2776815Ssemery 	    if (code != 0) {
2786815Ssemery 		if (sealalg != 0xffff)
2796815Ssemery 		    xfree_wrap(plain, tmsglen);
2806815Ssemery 		*minor_status = code;
2816815Ssemery 		return(GSS_S_FAILURE);
2826815Ssemery 	    }
2836815Ssemery 	    if (plain[tmsglen-1] > blocksize) {
2846815Ssemery 		if (sealalg != 0xffff)
2856815Ssemery 		    xfree_wrap(plain, tmsglen);
2866815Ssemery 		*minor_status = KG_BAD_LENGTH;
2876815Ssemery 		return(GSS_S_FAILURE);
2886815Ssemery 	    }
2890Sstevel@tonic-gate 	    token.length = tmsglen - conflen - plain[tmsglen-1];
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (token.length) {
2930Sstevel@tonic-gate 	    if ((token.value = (void *) xmalloc(token.length)) == NULL) {
2940Sstevel@tonic-gate 		if (sealalg != 0xffff)
2950Sstevel@tonic-gate 		    xfree_wrap(plain, tmsglen);
2960Sstevel@tonic-gate 		*minor_status = ENOMEM;
2970Sstevel@tonic-gate 		KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error2 ENOMEM\n");
2980Sstevel@tonic-gate 		return(GSS_S_FAILURE);
2990Sstevel@tonic-gate 	    }
3000Sstevel@tonic-gate 	    (void) memcpy(token.value, plain+conflen, token.length);
3010Sstevel@tonic-gate 	} else {
3020Sstevel@tonic-gate 	    token.value = NULL;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate     } else if (toktype == KG_TOK_SIGN_MSG) {
3050Sstevel@tonic-gate 	KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() toktype == KG_TOK_SIGN_MSG\n");
3060Sstevel@tonic-gate 	token = *message_buffer;
3070Sstevel@tonic-gate 	plain = token.value;
3080Sstevel@tonic-gate 	plainlen = token.length;
3090Sstevel@tonic-gate     } else {
3100Sstevel@tonic-gate 	KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() toktype == NULL\n");
3110Sstevel@tonic-gate 	token.length = 0;
3120Sstevel@tonic-gate 	token.value = NULL;
3130Sstevel@tonic-gate 	plain = token.value;
3140Sstevel@tonic-gate 	plainlen = token.length;
3150Sstevel@tonic-gate     }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate     /* compute the checksum of the message */
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate     /* initialize the the cksum */
3200Sstevel@tonic-gate     switch (signalg) {
3210Sstevel@tonic-gate     case SGN_ALG_DES_MAC_MD5:
3220Sstevel@tonic-gate     case SGN_ALG_MD2_5:
3230Sstevel@tonic-gate     case SGN_ALG_DES_MAC:
3240Sstevel@tonic-gate     case SGN_ALG_3:
3250Sstevel@tonic-gate 	md5cksum.checksum_type = CKSUMTYPE_RSA_MD5;
3260Sstevel@tonic-gate 	break;
3277934SMark.Phalan@Sun.COM     case SGN_ALG_HMAC_MD5:
3287934SMark.Phalan@Sun.COM       md5cksum.checksum_type = CKSUMTYPE_HMAC_MD5_ARCFOUR;
3297934SMark.Phalan@Sun.COM       break;
3300Sstevel@tonic-gate     case SGN_ALG_HMAC_SHA1_DES3_KD:
3310Sstevel@tonic-gate 	md5cksum.checksum_type = CKSUMTYPE_HMAC_SHA1_DES3;
3320Sstevel@tonic-gate 	break;
3330Sstevel@tonic-gate     default:
3340Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, error2 signalg=%d\n", signalg);
3350Sstevel@tonic-gate #ifndef	_KERNEL
3360Sstevel@tonic-gate 	abort ();
3370Sstevel@tonic-gate #else
3380Sstevel@tonic-gate 	*minor_status = 0;
3390Sstevel@tonic-gate 	return(GSS_S_DEFECTIVE_TOKEN);
3400Sstevel@tonic-gate #endif /* _KERNEL */
3410Sstevel@tonic-gate     }
3420Sstevel@tonic-gate 
3437934SMark.Phalan@Sun.COM     code = krb5_c_checksum_length(context, md5cksum.checksum_type, &sumlen);
3447934SMark.Phalan@Sun.COM     if (code)
3450Sstevel@tonic-gate     {
3460Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, krb5_c_checksum_length() error "
3470Sstevel@tonic-gate 		"code=%d\n", code);
3480Sstevel@tonic-gate 	return(code);
3490Sstevel@tonic-gate     }
3500Sstevel@tonic-gate     md5cksum.length = (size_t)sumlen;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate     switch (signalg) {
3530Sstevel@tonic-gate     case SGN_ALG_DES_MAC_MD5:
3540Sstevel@tonic-gate     case SGN_ALG_3:
3550Sstevel@tonic-gate 	/* compute the checksum of the message */
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/* 8 = bytes of token body to be checksummed according to spec */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (! (data_ptr = (void *)
3600Sstevel@tonic-gate 	       xmalloc(8 + (ctx->big_endian ? token.length : plainlen)))) {
3610Sstevel@tonic-gate 	    if (sealalg != 0xffff)
3620Sstevel@tonic-gate 		xfree_wrap(plain, tmsglen);
3630Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
3640Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
3650Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
3660Sstevel@tonic-gate 		 * output parameter.
3670Sstevel@tonic-gate 		 */
3680Sstevel@tonic-gate 		token.value = NULL;
3690Sstevel@tonic-gate 		token.length = 0;
3700Sstevel@tonic-gate 	    }
3710Sstevel@tonic-gate 	    *minor_status = ENOMEM;
3720Sstevel@tonic-gate 	    KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error3 ENOMEM\n");
3730Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	(void) memcpy(data_ptr, ptr-2, 8);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (ctx->big_endian)
3790Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8, token.value, token.length);
3800Sstevel@tonic-gate 	else
3810Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8, plain, plainlen);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	plaind.length = 8 + (ctx->big_endian ? token.length : plainlen);
3840Sstevel@tonic-gate 	plaind.data = data_ptr;
3850Sstevel@tonic-gate 	code = krb5_c_make_checksum(context, md5cksum.checksum_type,
3860Sstevel@tonic-gate 				    ctx->seq, sign_usage,
3870Sstevel@tonic-gate 				    &plaind, &md5cksum);
3880Sstevel@tonic-gate 	xfree_wrap(data_ptr, 8 + (ctx->big_endian ? token.length : plainlen));
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (code) {
3910Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
3920Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
3930Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
3940Sstevel@tonic-gate 		 * output parameter.
3950Sstevel@tonic-gate 		 */
3960Sstevel@tonic-gate 		token.value = NULL;
3970Sstevel@tonic-gate 		token.length = 0;
3980Sstevel@tonic-gate 	    }
3990Sstevel@tonic-gate 	    *minor_status = code;
4000Sstevel@tonic-gate 	    KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, krb5_c_make_checksum() "
4010Sstevel@tonic-gate 		    "error code = %d\n", code);
4020Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if ((code = kg_encrypt(context, ctx->seq, KG_USAGE_SEAL,
4065053Sgtb 			       (g_OID_equal(ctx->mech_used, gss_mech_krb5_old) ?
4070Sstevel@tonic-gate 				ctx->seq->contents : NULL),
4080Sstevel@tonic-gate 			       md5cksum.contents, md5cksum.contents, 16))) {
4090Sstevel@tonic-gate 	    xfree_wrap(md5cksum.contents, md5cksum.length);
4100Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
4110Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
4120Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
4130Sstevel@tonic-gate 		 * output parameter.
4140Sstevel@tonic-gate 		 */
4150Sstevel@tonic-gate 		token.value = NULL;
4160Sstevel@tonic-gate 		token.length = 0;
4170Sstevel@tonic-gate 	    }
4180Sstevel@tonic-gate 	    *minor_status = code;
4190Sstevel@tonic-gate 	    KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, kg_encrypt() error"
4200Sstevel@tonic-gate 		    "code = %d\n", code);
4210Sstevel@tonic-gate 	    return GSS_S_FAILURE;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (signalg == 0)
4250Sstevel@tonic-gate 	    cksum.length = 8;
4260Sstevel@tonic-gate 	else
4270Sstevel@tonic-gate 	    cksum.length = 16;
4280Sstevel@tonic-gate 	cksum.contents = md5cksum.contents + 16 - cksum.length;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	code = memcmp(cksum.contents, ptr+14, cksum.length);
4310Sstevel@tonic-gate 	break;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate     case SGN_ALG_MD2_5:
4340Sstevel@tonic-gate 	if (!ctx->seed_init &&
4350Sstevel@tonic-gate 	    (code = kg_make_seed(context, ctx->subkey, ctx->seed))) {
4360Sstevel@tonic-gate 	    xfree_wrap(md5cksum.contents, md5cksum.length);
4370Sstevel@tonic-gate 	    if (sealalg != 0xffff)
4380Sstevel@tonic-gate 		xfree_wrap(plain, tmsglen);
4390Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
4400Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
4410Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
4420Sstevel@tonic-gate 		 * output parameter.
4430Sstevel@tonic-gate 		 */
4440Sstevel@tonic-gate 		token.value = NULL;
4450Sstevel@tonic-gate 		token.length = 0;
4460Sstevel@tonic-gate 	    }
4470Sstevel@tonic-gate 	    *minor_status = code;
4480Sstevel@tonic-gate 	    return GSS_S_FAILURE;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if (! (data_ptr = (void *)
4520Sstevel@tonic-gate 	       xmalloc(sizeof(ctx->seed) + 8 +
4530Sstevel@tonic-gate 		       (ctx->big_endian ? token.length : plainlen)))) {
4540Sstevel@tonic-gate 	    xfree_wrap(md5cksum.contents, md5cksum.length);
4550Sstevel@tonic-gate 	    if (sealalg == 0)
4560Sstevel@tonic-gate 		xfree_wrap(plain, tmsglen);
4570Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
4580Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
4590Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
4600Sstevel@tonic-gate 		 * output parameter.
4610Sstevel@tonic-gate 		 */
4620Sstevel@tonic-gate 		token.value = NULL;
4630Sstevel@tonic-gate 		token.length = 0;
4640Sstevel@tonic-gate 	    }
4650Sstevel@tonic-gate 	    *minor_status = ENOMEM;
4660Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	(void) memcpy(data_ptr, ptr-2, 8);
4690Sstevel@tonic-gate 	(void) memcpy(data_ptr+8, ctx->seed, sizeof(ctx->seed));
4700Sstevel@tonic-gate 	if (ctx->big_endian)
4710Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8+sizeof(ctx->seed),
4720Sstevel@tonic-gate 			  token.value, token.length);
4730Sstevel@tonic-gate 	else
4740Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8+sizeof(ctx->seed),
4750Sstevel@tonic-gate 			  plain, plainlen);
4760Sstevel@tonic-gate 	plaind.length = 8 + sizeof(ctx->seed) +
4770Sstevel@tonic-gate 	    (ctx->big_endian ? token.length : plainlen);
4780Sstevel@tonic-gate 	plaind.data = data_ptr;
4790Sstevel@tonic-gate 	xfree_wrap(md5cksum.contents, md5cksum.length);
4800Sstevel@tonic-gate 	code = krb5_c_make_checksum(context, md5cksum.checksum_type,
4810Sstevel@tonic-gate 				    ctx->seq, KG_USAGE_SIGN,
4820Sstevel@tonic-gate 				    &plaind, &md5cksum);
4830Sstevel@tonic-gate 	xfree_wrap(data_ptr, 8 + sizeof(ctx->seed) +
4840Sstevel@tonic-gate             (ctx->big_endian ? token.length : plainlen));
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (code) {
4870Sstevel@tonic-gate 	    if (sealalg == 0)
4880Sstevel@tonic-gate 		xfree_wrap(plain, tmsglen);
4890Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
4900Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
4910Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
4920Sstevel@tonic-gate 		 * output parameter.
4930Sstevel@tonic-gate 		 */
4940Sstevel@tonic-gate 		token.value = NULL;
4950Sstevel@tonic-gate 		token.length = 0;
4960Sstevel@tonic-gate 	    }
4970Sstevel@tonic-gate 	    *minor_status = code;
4980Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	code = memcmp(md5cksum.contents, ptr+14, 8);
5020Sstevel@tonic-gate 	/* Falls through to defective-token??  */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate     default:
5050Sstevel@tonic-gate 	*minor_status = 0;
5060Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error SGN_ALG_MD2_5 "
5070Sstevel@tonic-gate 		"GSS_S_DEFECTIVE_TOKEN\n");
5080Sstevel@tonic-gate 	return(GSS_S_DEFECTIVE_TOKEN);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate     case SGN_ALG_HMAC_SHA1_DES3_KD:
5110Sstevel@tonic-gate     case SGN_ALG_HMAC_MD5:
5120Sstevel@tonic-gate 	/* compute the checksum of the message */
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* 8 = bytes of token body to be checksummed according to spec */
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (! (data_ptr = (void *)
5170Sstevel@tonic-gate 	       xmalloc(8 + (ctx->big_endian ? token.length : plainlen)))) {
5180Sstevel@tonic-gate 	    if (sealalg != 0xffff)
5190Sstevel@tonic-gate 		xfree_wrap(plain, tmsglen);
5200Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
5210Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
5220Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
5230Sstevel@tonic-gate 		 * output parameter.
5240Sstevel@tonic-gate 		 */
5250Sstevel@tonic-gate 		token.value = NULL;
5260Sstevel@tonic-gate 		token.length = 0;
5270Sstevel@tonic-gate 	    }
5280Sstevel@tonic-gate 	    *minor_status = ENOMEM;
5290Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	(void) memcpy(data_ptr, ptr-2, 8);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (ctx->big_endian) {
5350Sstevel@tonic-gate 	    KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() ctx->big_endian = 1\n");
5360Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8, token.value, token.length);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	else {
5390Sstevel@tonic-gate 	    KRB5_LOG0(KRB5_INFO, "kg_unseal_v1() ctx->big_endian = 0\n");
5400Sstevel@tonic-gate 	    (void) memcpy(data_ptr+8, plain, plainlen);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	plaind.length = 8 + (ctx->big_endian ? token.length : plainlen);
5440Sstevel@tonic-gate 	plaind.data = data_ptr;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	code = krb5_c_make_checksum(context, md5cksum.checksum_type,
5470Sstevel@tonic-gate 				    ctx->seq, sign_usage,
5480Sstevel@tonic-gate 				    &plaind, &md5cksum);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	xfree_wrap(data_ptr, 8 + (ctx->big_endian ? token.length : plainlen));
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if (code) {
5530Sstevel@tonic-gate 	    if (toktype == KG_TOK_SEAL_MSG) {
5540Sstevel@tonic-gate 		xfree_wrap(token.value, token.length);
5550Sstevel@tonic-gate 		/* Solaris Kerberos: just to be safe since token.value is an
5560Sstevel@tonic-gate 		 * output parameter.
5570Sstevel@tonic-gate 		 */
5580Sstevel@tonic-gate 		token.value = NULL;
5590Sstevel@tonic-gate 		token.length = 0;
5600Sstevel@tonic-gate 	    }
5610Sstevel@tonic-gate 	    *minor_status = code;
5620Sstevel@tonic-gate 	    KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error "
5630Sstevel@tonic-gate 		    "SGN_ALG_HMAC_SHA1_DES3_KD GSS_S_FAILURE\n");
5640Sstevel@tonic-gate 	    return(GSS_S_FAILURE);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* compare the computed checksum against the transmitted checksum */
5680Sstevel@tonic-gate 	code = memcmp(md5cksum.contents, ptr+14, cksum_len);
5690Sstevel@tonic-gate 	KRB5_LOG(KRB5_INFO, "kg_unseal_v1() memcmp %d bytes", cksum_len);
5700Sstevel@tonic-gate 	break;
5710Sstevel@tonic-gate     }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate     xfree_wrap(md5cksum.contents, md5cksum.length);
5740Sstevel@tonic-gate     if (sealalg != 0xffff)
5750Sstevel@tonic-gate 	xfree_wrap(plain, tmsglen);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate     if (code) {
5780Sstevel@tonic-gate 	if (toktype == KG_TOK_SEAL_MSG) {
5790Sstevel@tonic-gate 	    xfree_wrap(token.value, token.length);
5800Sstevel@tonic-gate 	    /* Solaris Kerberos: just to be safe since token.value is an
5810Sstevel@tonic-gate 	     * output parameter.
5820Sstevel@tonic-gate 	     */
5830Sstevel@tonic-gate 	    token.value = NULL;
5840Sstevel@tonic-gate 	    token.length = 0;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	*minor_status = 0;
5870Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal_v1() end, error GSS_S_BAD_SIG\n");
5880Sstevel@tonic-gate 	return(GSS_S_BAD_SIG);
5890Sstevel@tonic-gate     }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate     if (conf_state)
5920Sstevel@tonic-gate 	*conf_state = (sealalg != 0xffff);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate     if (qop_state)
5950Sstevel@tonic-gate 	*qop_state = GSS_C_QOP_DEFAULT;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate     if ((code = krb5_timeofday(context, &now))) {
5980Sstevel@tonic-gate 	*minor_status = code;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "kg_unseal_v1() end, krb5_timeofday()"
6010Sstevel@tonic-gate 		"error code = %d\n", code);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	return(GSS_S_FAILURE);
6040Sstevel@tonic-gate     }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate     if (now > ctx->endtime) {
6070Sstevel@tonic-gate 	*minor_status = 0;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	KRB5_LOG1(KRB5_ERR, "kg_unseal_v1() end, error "
6100Sstevel@tonic-gate 		"now %d > ctx->endtime %d\n", now, ctx->endtime);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	return(GSS_S_CONTEXT_EXPIRED);
6130Sstevel@tonic-gate     }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate     /* do sequencing checks */
6160Sstevel@tonic-gate     if ((ctx->initiate && direction != 0xff) ||
6170Sstevel@tonic-gate 	(!ctx->initiate && direction != 0)) {
6180Sstevel@tonic-gate 	if (toktype == KG_TOK_SEAL_MSG) {
6190Sstevel@tonic-gate 	    xfree_wrap(token.value, token.length);
6200Sstevel@tonic-gate 	    /* Solaris Kerberos: just to be safe since token.value is an
6210Sstevel@tonic-gate 	     * output parameter.
6220Sstevel@tonic-gate 	     */
6230Sstevel@tonic-gate 	    token.value = NULL;
6240Sstevel@tonic-gate 	    token.length = 0;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	*minor_status = (OM_uint32) G_BAD_DIRECTION;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	KRB5_LOG1(KRB5_ERR, "kg_unseal_v1() end, error GSS_S_BAD_SIG "
6290Sstevel@tonic-gate 		"G_BAD_DIRECTION ctx->initiate = %d "
6300Sstevel@tonic-gate 		"direction = %d\n", ctx->initiate, direction);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	return(GSS_S_BAD_SIG);
6330Sstevel@tonic-gate     }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate     retval = g_order_check(&(ctx->seqstate), (gssint_uint64)seqnum);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate     /* It got through unscathed, adjust the output message buffer. */
6380Sstevel@tonic-gate     if (retval == 0 && toktype == KG_TOK_SEAL_MSG)
6390Sstevel@tonic-gate 	*message_buffer = token;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate     *minor_status = 0;
6420Sstevel@tonic-gate     KRB5_LOG(KRB5_INFO, "kg_unseal_v1() end, retval = %d\n", retval);
6430Sstevel@tonic-gate     return(retval);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /* message_buffer is an input if SIGN, output if SEAL, and ignored if DEL_CTX
6470Sstevel@tonic-gate    conf_state is only valid if SEAL. */
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate OM_uint32
kg_unseal(minor_status,context_handle,input_token_buffer,message_buffer,conf_state,qop_state,toktype)6505053Sgtb kg_unseal(minor_status, context_handle, input_token_buffer,
6510Sstevel@tonic-gate 	  message_buffer, conf_state, qop_state, toktype)
6520Sstevel@tonic-gate     OM_uint32 *minor_status;
6530Sstevel@tonic-gate     gss_ctx_id_t context_handle;
6540Sstevel@tonic-gate     gss_buffer_t input_token_buffer;
6550Sstevel@tonic-gate     gss_buffer_t message_buffer;
6560Sstevel@tonic-gate     int *conf_state;
6570Sstevel@tonic-gate     int *qop_state;
6580Sstevel@tonic-gate     int toktype;
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate     krb5_gss_ctx_id_rec *ctx;
6610Sstevel@tonic-gate     unsigned char *ptr;
6620Sstevel@tonic-gate     int bodysize;
6630Sstevel@tonic-gate     int err;
6640Sstevel@tonic-gate     int toktype2;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "kg_unseal() start \n");
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate     /* validate the context handle */
6690Sstevel@tonic-gate     if (! kg_validate_ctx_id(context_handle)) {
6700Sstevel@tonic-gate 	*minor_status = (OM_uint32) G_VALIDATE_FAILED;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal() end, kg_validate_ctx_id() error "
6730Sstevel@tonic-gate 		"G_VALIDATE_FAILED \n");
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	return(GSS_S_NO_CONTEXT);
6760Sstevel@tonic-gate     }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate     ctx = (krb5_gss_ctx_id_rec *) context_handle;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate     if (! ctx->established) {
6810Sstevel@tonic-gate 	*minor_status = KG_CTX_INCOMPLETE;
6820Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "kg_unseal() end, error ! ctx->established \n");
6830Sstevel@tonic-gate 	return(GSS_S_NO_CONTEXT);
6840Sstevel@tonic-gate     }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate     /* parse the token, leave the data in message_buffer, setting conf_state */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate     /* verify the header */
6890Sstevel@tonic-gate     ptr = (unsigned char *) input_token_buffer->value;
6900Sstevel@tonic-gate     if (ctx->proto)
6910Sstevel@tonic-gate 	switch (toktype) {
6920Sstevel@tonic-gate 	case KG_TOK_SIGN_MSG:
6937934SMark.Phalan@Sun.COM 	    toktype2 = 0x0404;
6947934SMark.Phalan@Sun.COM 	    break;
6950Sstevel@tonic-gate 	case KG_TOK_SEAL_MSG:
6967934SMark.Phalan@Sun.COM 	    toktype2 = 0x0504;
6977934SMark.Phalan@Sun.COM 	    break;
6980Sstevel@tonic-gate 	case KG_TOK_DEL_CTX:
6997934SMark.Phalan@Sun.COM 	    toktype2 = 0x0405;
7007934SMark.Phalan@Sun.COM 	    break;
7010Sstevel@tonic-gate 	default:
7027934SMark.Phalan@Sun.COM 	    toktype2 = toktype;
7037934SMark.Phalan@Sun.COM 	    break;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate     else
7067934SMark.Phalan@Sun.COM 	toktype2 = toktype;
7075053Sgtb     err = g_verify_token_header(ctx->mech_used,
7080Sstevel@tonic-gate 				(uint32_t *)&bodysize, &ptr, toktype2,
7090Sstevel@tonic-gate 				input_token_buffer->length,
7100Sstevel@tonic-gate 				!ctx->proto);
7110Sstevel@tonic-gate     if (err) {
7120Sstevel@tonic-gate 	*minor_status = err;
7130Sstevel@tonic-gate 	return GSS_S_DEFECTIVE_TOKEN;
7140Sstevel@tonic-gate     }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate     if (ctx->proto == 0) {
7175053Sgtb 	err = kg_unseal_v1(ctx->k5_context, minor_status, ctx, ptr, bodysize,
7180Sstevel@tonic-gate 			    message_buffer, conf_state, qop_state,
7190Sstevel@tonic-gate 			    toktype);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate     } else {
7227934SMark.Phalan@Sun.COM 	err = gss_krb5int_unseal_token_v3(&ctx->k5_context, minor_status, ctx,
7230Sstevel@tonic-gate                                            ptr, bodysize, message_buffer,
7240Sstevel@tonic-gate                                            conf_state, qop_state, toktype);
7250Sstevel@tonic-gate     }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate     *minor_status = err;
7280Sstevel@tonic-gate 
729*13132SGlenn.Barry@oracle.com #ifndef _KERNEL
730*13132SGlenn.Barry@oracle.com     if (err != 0)
731*13132SGlenn.Barry@oracle.com 	save_error_info (*minor_status, ctx->k5_context);
732*13132SGlenn.Barry@oracle.com #endif
733*13132SGlenn.Barry@oracle.com 
7340Sstevel@tonic-gate     KRB5_LOG(KRB5_INFO, "kg_unseal() end, err = %d", err);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate     return(err);
7370Sstevel@tonic-gate }
738