xref: /titanic_50/usr/src/lib/libgss/g_imp_sec_context.c (revision 694c35faa87b858ecdadfe4fc592615f4eefbb07)
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  *  glue routine gss_export_sec_context
27  */
28 
29 #ifndef LEAN_CLIENT
30 
31 #include <mechglueP.h>
32 #include "gssapiP_generic.h"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 static OM_uint32
39 val_imp_sec_ctx_args(
40 	OM_uint32 *minor_status,
41 	gss_buffer_t interprocess_token,
42 	gss_ctx_id_t *context_handle)
43 {
44 
45 	/* Initialize outputs. */
46 	if (minor_status != NULL)
47 		*minor_status = 0;
48 
49 	if (context_handle != NULL)
50 		*context_handle = GSS_C_NO_CONTEXT;
51 
52 	/* Validate arguments. */
53 
54 	if (minor_status == NULL)
55 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
56 
57 	if (context_handle == NULL)
58 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
59 
60 	if (interprocess_token == GSS_C_NO_BUFFER)
61 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
62 
63 	if (GSS_EMPTY_BUFFER(interprocess_token))
64 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_DEFECTIVE_TOKEN);
65 
66 	return (GSS_S_COMPLETE);
67 }
68 
69 OM_uint32
70 gss_import_sec_context(minor_status,
71 			interprocess_token,
72 			context_handle)
73 
74 OM_uint32 *		minor_status;
75 const gss_buffer_t	interprocess_token;
76 gss_ctx_id_t 		*context_handle;
77 
78 {
79 	OM_uint32		length = 0;
80 	OM_uint32		status;
81 	char			*p;
82 	gss_union_ctx_id_t	ctx;
83 	gss_buffer_desc		token;
84 	gss_mechanism		mech;
85 
86 	status = val_imp_sec_ctx_args(minor_status,
87 				interprocess_token, context_handle);
88 	if (status != GSS_S_COMPLETE)
89 		return (status);
90 
91 	/* Initial value needed below. */
92 	status = GSS_S_FAILURE;
93 
94 	ctx = (gss_union_ctx_id_t)malloc(sizeof (gss_union_ctx_id_desc));
95 	if (!ctx)
96 		return (GSS_S_FAILURE);
97 
98 	ctx->mech_type = (gss_OID) malloc(sizeof (gss_OID_desc));
99 	if (!ctx->mech_type) {
100 		free(ctx);
101 		return (GSS_S_FAILURE);
102 	}
103 
104 	if (interprocess_token->length >= sizeof (OM_uint32)) {
105 		p = interprocess_token->value;
106 		length = (OM_uint32)*p++;
107 		length = (OM_uint32)(length << 8) + *p++;
108 		length = (OM_uint32)(length << 8) + *p++;
109 		length = (OM_uint32)(length << 8) + *p++;
110 	}
111 
112 	if (length == 0 ||
113 	    length > (interprocess_token->length - sizeof (OM_uint32))) {
114 		free(ctx);
115 		return (GSS_S_CALL_BAD_STRUCTURE | GSS_S_DEFECTIVE_TOKEN);
116 	}
117 
118 	ctx->mech_type->length = length;
119 	ctx->mech_type->elements = malloc(length);
120 	if (!ctx->mech_type->elements) {
121 		goto error_out;
122 	}
123 	(void) memcpy(ctx->mech_type->elements, p, length);
124 	p += length;
125 
126 	token.length = interprocess_token->length - sizeof (OM_uint32) - length;
127 	token.value = p;
128 
129 	/*
130 	 * select the approprate underlying mechanism routine and
131 	 * call it.
132 	 */
133 
134 	mech = __gss_get_mechanism(ctx->mech_type);
135 	if (!mech) {
136 		status = GSS_S_BAD_MECH;
137 		goto error_out;
138 	}
139 	if (!mech->gss_import_sec_context) {
140 		status = GSS_S_UNAVAILABLE;
141 		goto error_out;
142 	}
143 
144 	status = mech->gss_import_sec_context(mech->context, minor_status,
145 					&token, &ctx->internal_ctx_id);
146 
147 	if (status == GSS_S_COMPLETE) {
148 		*context_handle = (gss_ctx_id_t)ctx;
149 		return (GSS_S_COMPLETE);
150 	}
151 	map_error(minor_status, mech);
152 
153 error_out:
154 	if (ctx) {
155 		if (ctx->mech_type) {
156 			if (ctx->mech_type->elements)
157 				free(ctx->mech_type->elements);
158 			free(ctx->mech_type);
159 		}
160 		free(ctx);
161 	}
162 	return (status);
163 }
164 #endif /* LEAN_CLIENT */
165