1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2005 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$FreeBSD$
29  */
30 /*
31  * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
32  * (Royal Institute of Technology, Stockholm, Sweden).
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * 3. Neither the name of the Institute nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 /*
63  * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
64  * (Royal Institute of Technology, Stockholm, Sweden).
65  * All rights reserved.
66  *
67  * Redistribution and use in source and binary forms, with or without
68  * modification, are permitted provided that the following conditions
69  * are met:
70  *
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  *
74  * 2. Redistributions in binary form must reproduce the above copyright
75  *    notice, this list of conditions and the following disclaimer in the
76  *    documentation and/or other materials provided with the distribution.
77  *
78  * 3. Neither the name of the Institute nor the names of its contributors
79  *    may be used to endorse or promote products derived from this software
80  *    without specific prior written permission.
81  *
82  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92  * SUCH DAMAGE.
93  */
94 
95 #include <gssapi/gssapi.h>
96 #include <stdio.h>
97 #include <string.h>
98 #include <stdlib.h>
99 #include <errno.h>
100 
101 #include "mech_switch.h"
102 #include "utils.h"
103 
104 static const char *
105 calling_error(OM_uint32 v)
106 {
107     static const char *msgs[] = {
108 	NULL,			/* 0 */
109 	"A required input parameter could not be read.", /*  */
110 	"A required output parameter could not be written.", /*  */
111 	"A parameter was malformed"
112     };
113 
114     v >>= GSS_C_CALLING_ERROR_OFFSET;
115 
116     if (v == 0)
117 	return "";
118     else if (v >= sizeof(msgs)/sizeof(*msgs))
119 	return "unknown calling error";
120     else
121 	return msgs[v];
122 }
123 
124 static const char *
125 routine_error(OM_uint32 v)
126 {
127     static const char *msgs[] = {
128 	"Function completed successfully",			/* 0 */
129 	"An unsupported mechanism was requested",
130 	"An invalid name was supplied",
131 	"A supplied name was of an unsupported type",
132 	"Incorrect channel bindings were supplied",
133 	"An invalid status code was supplied",
134 	"A token had an invalid MIC",
135 	"No credentials were supplied, "
136 	"or the credentials were unavailable or inaccessible.",
137 	"No context has been established",
138 	"A token was invalid",
139 	"A credential was invalid",
140 	"The referenced credentials have expired",
141 	"The context has expired",
142 	"Miscellaneous failure (see text)",
143 	"The quality-of-protection requested could not be provide",
144 	"The operation is forbidden by local security policy",
145 	"The operation or option is not available",
146 	"The requested credential element already exists",
147 	"The provided name was not a mechanism name.",
148     };
149 
150     v >>= GSS_C_ROUTINE_ERROR_OFFSET;
151 
152     if (v >= sizeof(msgs)/sizeof(*msgs))
153 	return "unknown routine error";
154     else
155 	return msgs[v];
156 }
157 
158 static const char *
159 supplementary_error(OM_uint32 v)
160 {
161     static const char *msgs[] = {
162 	"normal completion",
163 	"continuation call to routine required",
164 	"duplicate per-message token detected",
165 	"timed-out per-message token detected",
166 	"reordered (early) per-message token detected",
167 	"skipped predecessor token(s) detected"
168     };
169 
170     v >>= GSS_C_SUPPLEMENTARY_OFFSET;
171 
172     if (v >= sizeof(msgs)/sizeof(*msgs))
173 	return "unknown routine error";
174     else
175 	return msgs[v];
176 }
177 
178 #if defined(__NO_TLS)
179 
180 /*
181  * These platforms don't support TLS on FreeBSD - threads will just
182  * have to step on each other's error values for now.
183  */
184 #define __thread
185 
186 #endif
187 
188 struct mg_thread_ctx {
189     gss_OID mech;
190     OM_uint32 maj_stat;
191     OM_uint32 min_stat;
192     gss_buffer_desc maj_error;
193     gss_buffer_desc min_error;
194 };
195 static __thread struct mg_thread_ctx last_error_context;
196 
197 static OM_uint32
198 _gss_mg_get_error(const gss_OID mech, OM_uint32 type,
199 		  OM_uint32 value, gss_buffer_t string)
200 {
201 	struct mg_thread_ctx *mg;
202 
203 	mg = &last_error_context;
204 
205 	if (mech != NULL && gss_oid_equal(mg->mech, mech) == 0)
206 		return (GSS_S_BAD_STATUS);
207 
208 	switch (type) {
209 	case GSS_C_GSS_CODE: {
210 		if (value != mg->maj_stat || mg->maj_error.length == 0)
211 			break;
212 		string->value = malloc(mg->maj_error.length);
213 		string->length = mg->maj_error.length;
214 		memcpy(string->value, mg->maj_error.value,
215 		    mg->maj_error.length);
216 		return (GSS_S_COMPLETE);
217 	}
218 	case GSS_C_MECH_CODE: {
219 		if (value != mg->min_stat || mg->min_error.length == 0)
220 			break;
221 		string->value = malloc(mg->min_error.length);
222 		string->length = mg->min_error.length;
223 		memcpy(string->value, mg->min_error.value,
224 		    mg->min_error.length);
225 		return (GSS_S_COMPLETE);
226 	}
227 	}
228 	string->value = NULL;
229 	string->length = 0;
230 	return (GSS_S_BAD_STATUS);
231 }
232 
233 void
234 _gss_mg_error(struct _gss_mech_switch *m, OM_uint32 maj, OM_uint32 min)
235 {
236 	OM_uint32 major_status, minor_status;
237 	OM_uint32 message_content;
238 	struct mg_thread_ctx *mg;
239 
240 	mg = &last_error_context;
241 
242 	gss_release_buffer(&minor_status, &mg->maj_error);
243 	gss_release_buffer(&minor_status, &mg->min_error);
244 
245 	mg->mech = &m->gm_mech_oid;
246 	mg->maj_stat = maj;
247 	mg->min_stat = min;
248 
249 	major_status = m->gm_display_status(&minor_status,
250 	    maj,
251 	    GSS_C_GSS_CODE,
252 	    &m->gm_mech_oid,
253 	    &message_content,
254 	    &mg->maj_error);
255 	if (GSS_ERROR(major_status)) {
256 		mg->maj_error.value = NULL;
257 		mg->maj_error.length = 0;
258 	}
259 	major_status = m->gm_display_status(&minor_status,
260 	    min,
261 	    GSS_C_MECH_CODE,
262 	    &m->gm_mech_oid,
263 	    &message_content,
264 	    &mg->min_error);
265 	if (GSS_ERROR(major_status)) {
266 		mg->min_error.value = NULL;
267 		mg->min_error.length = 0;
268 	}
269 }
270 
271 OM_uint32
272 gss_display_status(OM_uint32 *minor_status,
273     OM_uint32 status_value,
274     int status_type,
275     const gss_OID mech_type,
276     OM_uint32 *message_content,
277     gss_buffer_t status_string)
278 {
279 	OM_uint32 major_status;
280 
281 	_gss_buffer_zero(status_string);
282 	*message_content = 0;
283 
284 	major_status = _gss_mg_get_error(mech_type, status_type,
285 					 status_value, status_string);
286 	if (major_status == GSS_S_COMPLETE) {
287 
288 		*message_content = 0;
289 		*minor_status = 0;
290 		return (GSS_S_COMPLETE);
291 	}
292 
293 	*minor_status = 0;
294 	switch (status_type) {
295 	case GSS_C_GSS_CODE: {
296 		char *buf;
297 
298 		if (GSS_SUPPLEMENTARY_INFO(status_value))
299 		    asprintf(&buf, "%s", supplementary_error(
300 		        GSS_SUPPLEMENTARY_INFO(status_value)));
301 		else
302 		    asprintf (&buf, "%s %s",
303 		        calling_error(GSS_CALLING_ERROR(status_value)),
304 			routine_error(GSS_ROUTINE_ERROR(status_value)));
305 
306 		if (buf == NULL)
307 			break;
308 
309 		status_string->length = strlen(buf);
310 		status_string->value  = buf;
311 
312 		return (GSS_S_COMPLETE);
313 	}
314 	case GSS_C_MECH_CODE: {
315 		OM_uint32 maj_junk, min_junk;
316 		gss_buffer_desc oid;
317 		char *buf;
318 
319 		maj_junk = gss_oid_to_str(&min_junk, mech_type, &oid);
320 		if (maj_junk != GSS_S_COMPLETE) {
321 			oid.value = strdup("unknown");
322 			oid.length = 7;
323 		}
324 
325 		asprintf (&buf, "unknown mech-code %lu for mech %.*s",
326 			  (unsigned long)status_value,
327 			  (int)oid.length, (char *)oid.value);
328 		if (maj_junk == GSS_S_COMPLETE)
329 			gss_release_buffer(&min_junk, &oid);
330 
331 		if (buf == NULL)
332 		    break;
333 
334 		status_string->length = strlen(buf);
335 		status_string->value  = buf;
336 
337 		return (GSS_S_COMPLETE);
338 	}
339 	}
340 	_gss_buffer_zero(status_string);
341 	return (GSS_S_BAD_STATUS);
342 }
343 
344 void
345 _gss_mg_collect_error(gss_OID mech, OM_uint32 maj, OM_uint32 min)
346 {
347 	struct _gss_mech_switch *m;
348 
349 	m = _gss_find_mech_switch(mech);
350 	if (m != NULL)
351 		_gss_mg_error(m, maj, min);
352 }
353