2 * Copyright (c) 2005 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
30 * (Royal Institute of Technology, Stockholm, Sweden).
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the Institute nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
62 * (Royal Institute of Technology, Stockholm, Sweden).
63 * All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
76 * 3. Neither the name of the Institute nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 #include <gssapi/gssapi.h>
99 #include "mech_switch.h"
103 calling_error(OM_uint32 v)
105 static const char *msgs[] = {
107 "A required input parameter could not be read.", /* */
108 "A required output parameter could not be written.", /* */
109 "A parameter was malformed"
112 v >>= GSS_C_CALLING_ERROR_OFFSET;
116 else if (v >= sizeof(msgs)/sizeof(*msgs))
117 return "unknown calling error";
123 routine_error(OM_uint32 v)
125 static const char *msgs[] = {
126 "Function completed successfully", /* 0 */
127 "An unsupported mechanism was requested",
128 "An invalid name was supplied",
129 "A supplied name was of an unsupported type",
130 "Incorrect channel bindings were supplied",
131 "An invalid status code was supplied",
132 "A token had an invalid MIC",
133 "No credentials were supplied, "
134 "or the credentials were unavailable or inaccessible.",
135 "No context has been established",
136 "A token was invalid",
137 "A credential was invalid",
138 "The referenced credentials have expired",
139 "The context has expired",
140 "Miscellaneous failure (see text)",
141 "The quality-of-protection requested could not be provide",
142 "The operation is forbidden by local security policy",
143 "The operation or option is not available",
144 "The requested credential element already exists",
145 "The provided name was not a mechanism name.",
148 v >>= GSS_C_ROUTINE_ERROR_OFFSET;
150 if (v >= sizeof(msgs)/sizeof(*msgs))
151 return "unknown routine error";
157 supplementary_error(OM_uint32 v)
159 static const char *msgs[] = {
161 "continuation call to routine required",
162 "duplicate per-message token detected",
163 "timed-out per-message token detected",
164 "reordered (early) per-message token detected",
165 "skipped predecessor token(s) detected"
168 v >>= GSS_C_SUPPLEMENTARY_OFFSET;
170 if (v >= sizeof(msgs)/sizeof(*msgs))
171 return "unknown routine error";
176 #if defined(__sparc64__) || defined(__arm__) || defined(__mips__)
179 * These platforms don't support TLS on FreeBSD - threads will just
180 * have to step on each other's error values for now.
186 struct mg_thread_ctx {
190 gss_buffer_desc maj_error;
191 gss_buffer_desc min_error;
193 static __thread struct mg_thread_ctx last_error_context;
196 _gss_mg_get_error(const gss_OID mech, OM_uint32 type,
197 OM_uint32 value, gss_buffer_t string)
199 struct mg_thread_ctx *mg;
201 mg = &last_error_context;
203 if (mech != NULL && gss_oid_equal(mg->mech, mech) == 0)
204 return (GSS_S_BAD_STATUS);
207 case GSS_C_GSS_CODE: {
208 if (value != mg->maj_stat || mg->maj_error.length == 0)
210 string->value = malloc(mg->maj_error.length);
211 string->length = mg->maj_error.length;
212 memcpy(string->value, mg->maj_error.value,
213 mg->maj_error.length);
214 return (GSS_S_COMPLETE);
216 case GSS_C_MECH_CODE: {
217 if (value != mg->min_stat || mg->min_error.length == 0)
219 string->value = malloc(mg->min_error.length);
220 string->length = mg->min_error.length;
221 memcpy(string->value, mg->min_error.value,
222 mg->min_error.length);
223 return (GSS_S_COMPLETE);
226 string->value = NULL;
228 return (GSS_S_BAD_STATUS);
232 _gss_mg_error(struct _gss_mech_switch *m, OM_uint32 maj, OM_uint32 min)
234 OM_uint32 major_status, minor_status;
235 OM_uint32 message_content;
236 struct mg_thread_ctx *mg;
238 mg = &last_error_context;
240 gss_release_buffer(&minor_status, &mg->maj_error);
241 gss_release_buffer(&minor_status, &mg->min_error);
243 mg->mech = &m->gm_mech_oid;
247 major_status = m->gm_display_status(&minor_status,
253 if (GSS_ERROR(major_status)) {
254 mg->maj_error.value = NULL;
255 mg->maj_error.length = 0;
257 major_status = m->gm_display_status(&minor_status,
263 if (GSS_ERROR(major_status)) {
264 mg->min_error.value = NULL;
265 mg->min_error.length = 0;
270 gss_display_status(OM_uint32 *minor_status,
271 OM_uint32 status_value,
273 const gss_OID mech_type,
274 OM_uint32 *message_content,
275 gss_buffer_t status_string)
277 OM_uint32 major_status;
279 _gss_buffer_zero(status_string);
280 *message_content = 0;
282 major_status = _gss_mg_get_error(mech_type, status_type,
283 status_value, status_string);
284 if (major_status == GSS_S_COMPLETE) {
286 *message_content = 0;
288 return (GSS_S_COMPLETE);
292 switch (status_type) {
293 case GSS_C_GSS_CODE: {
296 if (GSS_SUPPLEMENTARY_INFO(status_value))
297 asprintf(&buf, "%s", supplementary_error(
298 GSS_SUPPLEMENTARY_INFO(status_value)));
300 asprintf (&buf, "%s %s",
301 calling_error(GSS_CALLING_ERROR(status_value)),
302 routine_error(GSS_ROUTINE_ERROR(status_value)));
307 status_string->length = strlen(buf);
308 status_string->value = buf;
310 return (GSS_S_COMPLETE);
312 case GSS_C_MECH_CODE: {
313 OM_uint32 maj_junk, min_junk;
317 maj_junk = gss_oid_to_str(&min_junk, mech_type, &oid);
318 if (maj_junk != GSS_S_COMPLETE) {
319 oid.value = strdup("unknown");
323 asprintf (&buf, "unknown mech-code %lu for mech %.*s",
324 (unsigned long)status_value,
325 (int)oid.length, (char *)oid.value);
326 if (maj_junk == GSS_S_COMPLETE)
327 gss_release_buffer(&min_junk, &oid);
332 status_string->length = strlen(buf);
333 status_string->value = buf;
335 return (GSS_S_COMPLETE);
338 _gss_buffer_zero(status_string);
339 return (GSS_S_BAD_STATUS);