Imported Upstream version 1.20.1
[platform/upstream/krb5.git] / src / lib / gssapi / mechglue / g_imp_sec_context.c
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
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
72 gss_import_sec_context(minor_status,
73                        interprocess_token,
74                        context_handle)
75
76 OM_uint32 *             minor_status;
77 gss_buffer_t            interprocess_token;
78 gss_ctx_id_t *          context_handle;
79
80 {
81     OM_uint32           length = 0;
82     OM_uint32           status;
83     char                *p;
84     gss_union_ctx_id_t  ctx;
85     gss_ctx_id_t        mctx;
86     gss_buffer_desc     token;
87     gss_OID_desc        token_mech;
88     gss_OID             selected_mech = GSS_C_NO_OID;
89     gss_OID             public_mech;
90     gss_mechanism       mech;
91
92     status = val_imp_sec_ctx_args(minor_status,
93                                   interprocess_token, context_handle);
94     if (status != GSS_S_COMPLETE)
95         return (status);
96
97     /* Initial value needed below. */
98     status = GSS_S_FAILURE;
99
100     if (interprocess_token->length >= sizeof (OM_uint32)) {
101         p = interprocess_token->value;
102         length = (OM_uint32)*p++;
103         length = (OM_uint32)(length << 8) + *p++;
104         length = (OM_uint32)(length << 8) + *p++;
105         length = (OM_uint32)(length << 8) + *p++;
106     }
107
108     if (length == 0 ||
109         length > (interprocess_token->length - sizeof (OM_uint32))) {
110         return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
111     }
112
113     token_mech.length = length;
114     token_mech.elements = p;
115
116     p += length;
117
118     token.length = interprocess_token->length - sizeof (OM_uint32) - length;
119     token.value = p;
120
121     /*
122      * select the approprate underlying mechanism routine and
123      * call it.
124      */
125
126     status = gssint_select_mech_type(minor_status, &token_mech,
127                                      &selected_mech);
128     if (status != GSS_S_COMPLETE)
129         return status;
130
131     mech = gssint_get_mechanism(selected_mech);
132     if (!mech)
133         return GSS_S_BAD_MECH;
134     if (!mech->gssspi_import_sec_context_by_mech &&
135         !mech->gss_import_sec_context)
136         return GSS_S_UNAVAILABLE;
137
138     status = gssint_create_union_context(minor_status, selected_mech, &ctx);
139     if (status != GSS_S_COMPLETE)
140         return status;
141
142     if (mech->gssspi_import_sec_context_by_mech) {
143         public_mech = gssint_get_public_oid(selected_mech);
144         status = mech->gssspi_import_sec_context_by_mech(minor_status,
145                                                          public_mech,
146                                                          &token, &mctx);
147     } else {
148         status = mech->gss_import_sec_context(minor_status, &token, &mctx);
149     }
150     if (status == GSS_S_COMPLETE) {
151         ctx->internal_ctx_id = mctx;
152         *context_handle = (gss_ctx_id_t)ctx;
153         return (GSS_S_COMPLETE);
154     }
155     map_error(minor_status, mech);
156     free(ctx->mech_type->elements);
157     free(ctx->mech_type);
158     free(ctx);
159     return status;
160 }
161 #endif /* LEAN_CLIENT */