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