xref: /titanic_44/usr/src/lib/libgss/g_imp_sec_context.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *  glue routine gss_export_sec_context
31  */
32 
33 #include <mechglueP.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 OM_uint32
40 gss_import_sec_context(minor_status,
41 			interprocess_token,
42 			context_handle)
43 
44 OM_uint32 *		minor_status;
45 const gss_buffer_t	interprocess_token;
46 gss_ctx_id_t 		*context_handle;
47 
48 {
49 	OM_uint32		length = 0;
50 	OM_uint32		status;
51 	char			*p;
52 	gss_union_ctx_id_t	ctx;
53 	gss_buffer_desc		token;
54 	gss_mechanism		mech;
55 
56 	if (minor_status == NULL)
57 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
58 	*minor_status = 0;
59 
60 	if (context_handle == NULL)
61 		return (GSS_S_CALL_INACCESSIBLE_WRITE | GSS_S_NO_CONTEXT);
62 	*context_handle = GSS_C_NO_CONTEXT;
63 
64 	if (GSS_EMPTY_BUFFER(interprocess_token))
65 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
66 
67 	status = GSS_S_FAILURE;
68 
69 	ctx = (gss_union_ctx_id_t)malloc(sizeof (gss_union_ctx_id_desc));
70 	if (!ctx)
71 		return (GSS_S_FAILURE);
72 
73 	ctx->mech_type = (gss_OID) malloc(sizeof (gss_OID_desc));
74 	if (!ctx->mech_type) {
75 		free(ctx);
76 		return (GSS_S_FAILURE);
77 	}
78 
79 	if (interprocess_token->length >= sizeof (OM_uint32)) {
80 		p = interprocess_token->value;
81 		length = (OM_uint32)*p++;
82 		length = (OM_uint32)(length << 8) + *p++;
83 		length = (OM_uint32)(length << 8) + *p++;
84 		length = (OM_uint32)(length << 8) + *p++;
85 	}
86 
87 	if (length == 0 ||
88 	    length > (interprocess_token->length - sizeof (OM_uint32))) {
89 		free(ctx);
90 		return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
91 	}
92 
93 	ctx->mech_type->length = length;
94 	ctx->mech_type->elements = malloc(length);
95 	if (!ctx->mech_type->elements) {
96 		goto error_out;
97 	}
98 	(void) memcpy(ctx->mech_type->elements, p, length);
99 	p += length;
100 
101 	token.length = interprocess_token->length - sizeof (OM_uint32) - length;
102 	token.value = p;
103 
104 	/*
105 	 * select the approprate underlying mechanism routine and
106 	 * call it.
107 	 */
108 
109 	mech = __gss_get_mechanism(ctx->mech_type);
110 	if (!mech) {
111 		status = GSS_S_BAD_MECH;
112 		goto error_out;
113 	}
114 	if (!mech->gss_import_sec_context) {
115 		status = GSS_S_UNAVAILABLE;
116 		goto error_out;
117 	}
118 
119 	status = mech->gss_import_sec_context(mech->context, minor_status,
120 					&token, &ctx->internal_ctx_id);
121 
122 	if (status == GSS_S_COMPLETE) {
123 		*context_handle = (gss_ctx_id_t)ctx;
124 		return (GSS_S_COMPLETE);
125 	}
126 
127 error_out:
128 	if (ctx) {
129 		if (ctx->mech_type) {
130 			if (ctx->mech_type->elements)
131 				free(ctx->mech_type->elements);
132 			free(ctx->mech_type);
133 		}
134 		free(ctx);
135 	}
136 	return (status);
137 }
138