xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_imp_sec_context.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* #pragma ident	"@(#)g_imp_sec_context.c	1.18	04/02/23 SMI" */
2 
3 /*
4  * Copyright 1996 by Sun Microsystems, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear in
10  * supporting documentation, and that the name of Sun Microsystems not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. Sun Microsystems makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24 
25 /*
26  *  glue routine gss_export_sec_context
27  */
28 
29 #ifndef LEAN_CLIENT
30 
31 #include "mglueP.h"
32 #include <stdio.h>
33 #include <errno.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <string.h>
38 
39 static OM_uint32
val_imp_sec_ctx_args(OM_uint32 * minor_status,gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)40 val_imp_sec_ctx_args(
41     OM_uint32 *minor_status,
42     gss_buffer_t interprocess_token,
43     gss_ctx_id_t *context_handle)
44 {
45 
46     /* Initialize outputs. */
47     if (minor_status != NULL)
48 	*minor_status = 0;
49 
50     if (context_handle != NULL)
51 	*context_handle = GSS_C_NO_CONTEXT;
52 
53     /* Validate arguments. */
54 
55     if (minor_status == NULL)
56 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
57 
58     if (context_handle == NULL)
59 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
60 
61     if (interprocess_token == GSS_C_NO_BUFFER)
62 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
63 
64     if (GSS_EMPTY_BUFFER(interprocess_token))
65 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
66 
67     return (GSS_S_COMPLETE);
68 }
69 
70 
71 OM_uint32 KRB5_CALLCONV
gss_import_sec_context(OM_uint32 * minor_status,gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)72 gss_import_sec_context(OM_uint32 *minor_status,
73 		       gss_buffer_t interprocess_token,
74 		       gss_ctx_id_t *context_handle)
75 {
76     OM_uint32		length = 0;
77     OM_uint32		status;
78     char		*p;
79     gss_union_ctx_id_t	ctx;
80     gss_ctx_id_t	mctx;
81     gss_buffer_desc	token;
82     gss_OID_desc	token_mech;
83     gss_OID		selected_mech = GSS_C_NO_OID;
84     gss_OID		public_mech;
85     gss_mechanism	mech;
86 
87     status = val_imp_sec_ctx_args(minor_status,
88 				  interprocess_token, context_handle);
89     if (status != GSS_S_COMPLETE)
90 	return (status);
91 
92     /* Initial value needed below. */
93     status = GSS_S_FAILURE;
94 
95     if (interprocess_token->length >= sizeof (OM_uint32)) {
96 	p = interprocess_token->value;
97 	length = (OM_uint32)*p++;
98 	length = (OM_uint32)(length << 8) + *p++;
99 	length = (OM_uint32)(length << 8) + *p++;
100 	length = (OM_uint32)(length << 8) + *p++;
101     }
102 
103     if (length == 0 ||
104 	length > (interprocess_token->length - sizeof (OM_uint32))) {
105 	return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
106     }
107 
108     token_mech.length = length;
109     token_mech.elements = p;
110 
111     p += length;
112 
113     token.length = interprocess_token->length - sizeof (OM_uint32) - length;
114     token.value = p;
115 
116     /*
117      * select the approprate underlying mechanism routine and
118      * call it.
119      */
120 
121     status = gssint_select_mech_type(minor_status, &token_mech,
122 				     &selected_mech);
123     if (status != GSS_S_COMPLETE)
124 	return status;
125 
126     mech = gssint_get_mechanism(selected_mech);
127     if (!mech)
128 	return GSS_S_BAD_MECH;
129     if (!mech->gssspi_import_sec_context_by_mech &&
130 	!mech->gss_import_sec_context)
131 	return GSS_S_UNAVAILABLE;
132 
133     status = gssint_create_union_context(minor_status, selected_mech, &ctx);
134     if (status != GSS_S_COMPLETE)
135 	return status;
136 
137     if (mech->gssspi_import_sec_context_by_mech) {
138 	public_mech = gssint_get_public_oid(selected_mech);
139 	status = mech->gssspi_import_sec_context_by_mech(minor_status,
140 							 public_mech,
141 							 &token, &mctx);
142     } else {
143 	status = mech->gss_import_sec_context(minor_status, &token, &mctx);
144     }
145     if (status == GSS_S_COMPLETE) {
146 	ctx->internal_ctx_id = mctx;
147 	*context_handle = (gss_ctx_id_t)ctx;
148 	return (GSS_S_COMPLETE);
149     }
150     map_error(minor_status, mech);
151     free(ctx->mech_type->elements);
152     free(ctx->mech_type);
153     free(ctx);
154     return status;
155 }
156 #endif /* LEAN_CLIENT */
157