17c478bd9Sstevel@tonic-gate /*
2*ba7b222eSGlenn Barry  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
97c478bd9Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
107c478bd9Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
117c478bd9Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
127c478bd9Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
137c478bd9Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
147c478bd9Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
157c478bd9Sstevel@tonic-gate  * representations about the suitability of this software for any
167c478bd9Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
197c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
207c478bd9Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
217c478bd9Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
227c478bd9Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
237c478bd9Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
247c478bd9Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27ab9b2e15Sgtb #include "gssapiP_krb5.h"
28ab9b2e15Sgtb #include "mglueP.h"  /* SUNW15resync - for KGSS_ macros */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
31159d09a2SMark Phalan  * $Id: delete_sec_context.c 18396 2006-07-25 20:29:43Z lxs $
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
34ab9b2e15Sgtb 
357c478bd9Sstevel@tonic-gate #ifdef	 _KERNEL
36ab9b2e15Sgtb /* SUNW15resync - todo - unify these kernel rel oid funcs with user spc ones */
37ab9b2e15Sgtb 
38ab9b2e15Sgtb OM_uint32
39ab9b2e15Sgtb krb5_gss_internal_release_oid(minor_status, oid)
40ab9b2e15Sgtb     OM_uint32	*minor_status;
41ab9b2e15Sgtb     gss_OID	*oid;
427c478bd9Sstevel@tonic-gate {
43ab9b2e15Sgtb     /*
44ab9b2e15Sgtb      * This function only knows how to release internal OIDs. It will
45ab9b2e15Sgtb      * return GSS_S_CONTINUE_NEEDED for any OIDs it does not recognize.
46ab9b2e15Sgtb      */
47ab9b2e15Sgtb 
48ab9b2e15Sgtb     if ((*oid != gss_mech_krb5) &&
49ab9b2e15Sgtb 	(*oid != gss_mech_krb5_old) &&
50ab9b2e15Sgtb 	(*oid != gss_mech_krb5_wrong) &&
51ab9b2e15Sgtb 	(*oid != gss_nt_krb5_name) &&
52ab9b2e15Sgtb 	(*oid != gss_nt_krb5_principal)) {
53ab9b2e15Sgtb 	/* We don't know about this OID */
54ab9b2e15Sgtb 	return(GSS_S_CONTINUE_NEEDED);
55ab9b2e15Sgtb     }
56ab9b2e15Sgtb     else {
57ab9b2e15Sgtb 	*oid = GSS_C_NO_OID;
58ab9b2e15Sgtb 	*minor_status = 0;
59ab9b2e15Sgtb 	return(GSS_S_COMPLETE);
60ab9b2e15Sgtb     }
61ab9b2e15Sgtb }
627c478bd9Sstevel@tonic-gate 
63ab9b2e15Sgtb OM_uint32
64ab9b2e15Sgtb generic_gss_release_oid(minor_status, oid)
65ab9b2e15Sgtb     OM_uint32	*minor_status;
66ab9b2e15Sgtb     gss_OID	*oid;
67ab9b2e15Sgtb {
68ab9b2e15Sgtb     if (minor_status)
69ab9b2e15Sgtb 	*minor_status = 0;
70ab9b2e15Sgtb 
71ab9b2e15Sgtb     if (*oid == GSS_C_NO_OID)
72ab9b2e15Sgtb 	return(GSS_S_COMPLETE);
73ab9b2e15Sgtb 
74ab9b2e15Sgtb 
75ab9b2e15Sgtb     if ((*oid != GSS_C_NT_USER_NAME) &&
76ab9b2e15Sgtb 	(*oid != GSS_C_NT_MACHINE_UID_NAME) &&
77ab9b2e15Sgtb 	(*oid != GSS_C_NT_STRING_UID_NAME) &&
78ab9b2e15Sgtb 	(*oid != GSS_C_NT_HOSTBASED_SERVICE) &&
79ab9b2e15Sgtb 	(*oid != GSS_C_NT_ANONYMOUS) &&
80ab9b2e15Sgtb 	(*oid != GSS_C_NT_EXPORT_NAME) &&
81ab9b2e15Sgtb 	(*oid != gss_nt_service_name)) {
82ab9b2e15Sgtb 	FREE((*oid)->elements, (*oid)->length);
83ab9b2e15Sgtb 	FREE(*oid, sizeof(gss_OID_desc));
84ab9b2e15Sgtb     }
85ab9b2e15Sgtb     *oid = GSS_C_NO_OID;
86ab9b2e15Sgtb     return(GSS_S_COMPLETE);
87ab9b2e15Sgtb }
887c478bd9Sstevel@tonic-gate 
89ab9b2e15Sgtb OM_uint32
90ab9b2e15Sgtb krb5_gss_release_oid(minor_status, oid)
91ab9b2e15Sgtb     OM_uint32	*minor_status;
92ab9b2e15Sgtb     gss_OID	*oid;
93ab9b2e15Sgtb {
947c478bd9Sstevel@tonic-gate 
95ab9b2e15Sgtb     if (krb5_gss_internal_release_oid(minor_status, oid) != GSS_S_COMPLETE) {
96ab9b2e15Sgtb 	/* Pawn it off on the generic routine */
97ab9b2e15Sgtb 	return(generic_gss_release_oid(minor_status, oid));
98ab9b2e15Sgtb     }
99ab9b2e15Sgtb     else {
100ab9b2e15Sgtb 	*oid = GSS_C_NO_OID;
101ab9b2e15Sgtb 	*minor_status = 0;
102ab9b2e15Sgtb 	return(GSS_S_COMPLETE);
103ab9b2e15Sgtb     }
1047c478bd9Sstevel@tonic-gate }
105ab9b2e15Sgtb #endif
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1087c478bd9Sstevel@tonic-gate OM_uint32
109ab9b2e15Sgtb krb5_gss_delete_sec_context(minor_status,
110ab9b2e15Sgtb 			    context_handle,
111ab9b2e15Sgtb 			    output_token
1127c478bd9Sstevel@tonic-gate #ifdef	 _KERNEL
113ab9b2e15Sgtb 			    , gssd_ctx_verifier
1147c478bd9Sstevel@tonic-gate #endif
115ab9b2e15Sgtb 			    )
1167c478bd9Sstevel@tonic-gate      OM_uint32 *minor_status;
1177c478bd9Sstevel@tonic-gate      gss_ctx_id_t *context_handle;
1187c478bd9Sstevel@tonic-gate      gss_buffer_t output_token;
1197c478bd9Sstevel@tonic-gate #ifdef	 _KERNEL
120ab9b2e15Sgtb      OM_uint32 gssd_ctx_verifier;
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate {
123ab9b2e15Sgtb    krb5_context context;
1247c478bd9Sstevel@tonic-gate    krb5_gss_ctx_id_rec *ctx;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate    if (output_token) {
1277c478bd9Sstevel@tonic-gate       output_token->length = 0;
1287c478bd9Sstevel@tonic-gate       output_token->value = NULL;
1297c478bd9Sstevel@tonic-gate    }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate    /*SUPPRESS 29*/
1327c478bd9Sstevel@tonic-gate    if (*context_handle == GSS_C_NO_CONTEXT) {
1337c478bd9Sstevel@tonic-gate       *minor_status = 0;
134ab9b2e15Sgtb       return(GSS_S_COMPLETE);
1357c478bd9Sstevel@tonic-gate    }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate    /*SUPPRESS 29*/
1387c478bd9Sstevel@tonic-gate    /* validate the context handle */
1397c478bd9Sstevel@tonic-gate    if (! kg_validate_ctx_id(*context_handle)) {
1407c478bd9Sstevel@tonic-gate       *minor_status = (OM_uint32) G_VALIDATE_FAILED;
141ab9b2e15Sgtb       return(GSS_S_NO_CONTEXT);
1427c478bd9Sstevel@tonic-gate    }
1437c478bd9Sstevel@tonic-gate 
144ab9b2e15Sgtb    ctx = (krb5_gss_ctx_id_t) *context_handle;
145ab9b2e15Sgtb    context = ctx->k5_context;
146ab9b2e15Sgtb 
1477c478bd9Sstevel@tonic-gate    /* construct a delete context token if necessary */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate    if (output_token) {
150ab9b2e15Sgtb       OM_uint32 major;
1517c478bd9Sstevel@tonic-gate       gss_buffer_desc empty;
1527c478bd9Sstevel@tonic-gate       empty.length = 0; empty.value = NULL;
1537c478bd9Sstevel@tonic-gate 
154ab9b2e15Sgtb       if ((major = kg_seal(minor_status, *context_handle, 0,
1557c478bd9Sstevel@tonic-gate 			   GSS_C_QOP_DEFAULT,
1567c478bd9Sstevel@tonic-gate 			   &empty, NULL, output_token, KG_TOK_DEL_CTX)))
157ab9b2e15Sgtb 	 return(major);
1587c478bd9Sstevel@tonic-gate    }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate    /* invalidate the context handle */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate    (void)kg_delete_ctx_id(*context_handle);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate    /* free all the context state */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate    if (ctx->seqstate)
1677c478bd9Sstevel@tonic-gate       g_order_free(&(ctx->seqstate));
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate    if (ctx->enc)
1707c478bd9Sstevel@tonic-gate       krb5_free_keyblock(context, ctx->enc);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate    if (ctx->seq)
1737c478bd9Sstevel@tonic-gate       krb5_free_keyblock(context, ctx->seq);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate    if (ctx->here)
1767c478bd9Sstevel@tonic-gate       krb5_free_principal(context, ctx->here);
1777c478bd9Sstevel@tonic-gate    if (ctx->there)
1787c478bd9Sstevel@tonic-gate       krb5_free_principal(context, ctx->there);
1797c478bd9Sstevel@tonic-gate    if (ctx->subkey)
1807c478bd9Sstevel@tonic-gate       krb5_free_keyblock(context, ctx->subkey);
1817c478bd9Sstevel@tonic-gate    if (ctx->acceptor_subkey)
182ab9b2e15Sgtb        krb5_free_keyblock(context, ctx->acceptor_subkey);
1837c478bd9Sstevel@tonic-gate 
184ab9b2e15Sgtb    /* We never import the auth_context into the kernel */
1857c478bd9Sstevel@tonic-gate #ifndef _KERNEL
1867c478bd9Sstevel@tonic-gate    if (ctx->auth_context) {
187ab9b2e15Sgtb        if (ctx->cred_rcache)
188ab9b2e15Sgtb 	   (void)krb5_auth_con_setrcache(context, ctx->auth_context, NULL);
189ab9b2e15Sgtb 
1907c478bd9Sstevel@tonic-gate        krb5_auth_con_free(context, ctx->auth_context);
1917c478bd9Sstevel@tonic-gate    }
1927c478bd9Sstevel@tonic-gate #endif
1937c478bd9Sstevel@tonic-gate 
194ab9b2e15Sgtb    if (ctx->mech_used)
195ab9b2e15Sgtb        (void) KGSS_RELEASE_OID(minor_status, &ctx->mech_used);
196159d09a2SMark Phalan 
197*ba7b222eSGlenn Barry    if (ctx->authdata)
198*ba7b222eSGlenn Barry 	krb5_free_authdata(context, ctx->authdata);
199*ba7b222eSGlenn Barry 
200ab9b2e15Sgtb    if (ctx->k5_context)
201ab9b2e15Sgtb        krb5_free_context(ctx->k5_context);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate    /* Zero out context */
2047c478bd9Sstevel@tonic-gate    (void) memset(ctx, 0, sizeof(*ctx));
2057c478bd9Sstevel@tonic-gate    xfree_wrap(ctx, sizeof (krb5_gss_ctx_id_rec));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate    /* zero the handle itself */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate    *context_handle = GSS_C_NO_CONTEXT;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate    *minor_status = 0;
212ab9b2e15Sgtb    return(GSS_S_COMPLETE);
2137c478bd9Sstevel@tonic-gate }
214