xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/gssapi/mech/context.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: context.c,v 1.2 2017/01/28 21:31:46 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric #include "mech_locl.h"
4ca1c9b0cSelric #include "heim_threads.h"
5ca1c9b0cSelric 
6ca1c9b0cSelric struct mg_thread_ctx {
7ca1c9b0cSelric     gss_OID mech;
8ca1c9b0cSelric     OM_uint32 maj_stat;
9ca1c9b0cSelric     OM_uint32 min_stat;
10ca1c9b0cSelric     gss_buffer_desc maj_error;
11ca1c9b0cSelric     gss_buffer_desc min_error;
12ca1c9b0cSelric };
13ca1c9b0cSelric 
14ca1c9b0cSelric static HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
15ca1c9b0cSelric static int created_key;
16ca1c9b0cSelric static HEIMDAL_thread_key context_key;
17ca1c9b0cSelric 
18ca1c9b0cSelric 
19ca1c9b0cSelric static void
destroy_context(void * ptr)20ca1c9b0cSelric destroy_context(void *ptr)
21ca1c9b0cSelric {
22ca1c9b0cSelric     struct mg_thread_ctx *mg = ptr;
23ca1c9b0cSelric     OM_uint32 junk;
24ca1c9b0cSelric 
25ca1c9b0cSelric     if (mg == NULL)
26ca1c9b0cSelric 	return;
27ca1c9b0cSelric 
28ca1c9b0cSelric     gss_release_buffer(&junk, &mg->maj_error);
29ca1c9b0cSelric     gss_release_buffer(&junk, &mg->min_error);
30ca1c9b0cSelric     free(mg);
31ca1c9b0cSelric }
32ca1c9b0cSelric 
33ca1c9b0cSelric 
34ca1c9b0cSelric static struct mg_thread_ctx *
_gss_mechglue_thread(void)35ca1c9b0cSelric _gss_mechglue_thread(void)
36ca1c9b0cSelric {
37ca1c9b0cSelric     struct mg_thread_ctx *ctx;
38ca1c9b0cSelric     int ret = 0;
39ca1c9b0cSelric 
40ca1c9b0cSelric     HEIMDAL_MUTEX_lock(&context_mutex);
41ca1c9b0cSelric 
42ca1c9b0cSelric     if (!created_key) {
43ca1c9b0cSelric 	HEIMDAL_key_create(&context_key, destroy_context, ret);
44ca1c9b0cSelric 	if (ret) {
45ca1c9b0cSelric 	    HEIMDAL_MUTEX_unlock(&context_mutex);
46ca1c9b0cSelric 	    return NULL;
47ca1c9b0cSelric 	}
48ca1c9b0cSelric 	created_key = 1;
49ca1c9b0cSelric     }
50ca1c9b0cSelric     HEIMDAL_MUTEX_unlock(&context_mutex);
51ca1c9b0cSelric 
52ca1c9b0cSelric     ctx = HEIMDAL_getspecific(context_key);
53ca1c9b0cSelric     if (ctx == NULL) {
54ca1c9b0cSelric 
55ca1c9b0cSelric 	ctx = calloc(1, sizeof(*ctx));
56ca1c9b0cSelric 	if (ctx == NULL)
57ca1c9b0cSelric 	    return NULL;
58ca1c9b0cSelric 	HEIMDAL_setspecific(context_key, ctx, ret);
59ca1c9b0cSelric 	if (ret) {
60ca1c9b0cSelric 	    free(ctx);
61ca1c9b0cSelric 	    return NULL;
62ca1c9b0cSelric 	}
63ca1c9b0cSelric     }
64ca1c9b0cSelric     return ctx;
65ca1c9b0cSelric }
66ca1c9b0cSelric 
67ca1c9b0cSelric OM_uint32
_gss_mg_get_error(const gss_OID mech,OM_uint32 type,OM_uint32 value,gss_buffer_t string)68ca1c9b0cSelric _gss_mg_get_error(const gss_OID mech, OM_uint32 type,
69ca1c9b0cSelric 		  OM_uint32 value, gss_buffer_t string)
70ca1c9b0cSelric {
71ca1c9b0cSelric     struct mg_thread_ctx *mg;
72ca1c9b0cSelric 
73ca1c9b0cSelric     mg = _gss_mechglue_thread();
74ca1c9b0cSelric     if (mg == NULL)
75ca1c9b0cSelric 	return GSS_S_BAD_STATUS;
76ca1c9b0cSelric 
77ca1c9b0cSelric #if 0
78ca1c9b0cSelric     /*
79ca1c9b0cSelric      * We cant check the mech here since a pseudo-mech might have
80ca1c9b0cSelric      * called an lower layer and then the mech info is all broken
81ca1c9b0cSelric      */
82ca1c9b0cSelric     if (mech != NULL && gss_oid_equal(mg->mech, mech) == 0)
83ca1c9b0cSelric 	return GSS_S_BAD_STATUS;
84ca1c9b0cSelric #endif
85ca1c9b0cSelric 
86ca1c9b0cSelric     switch (type) {
87ca1c9b0cSelric     case GSS_C_GSS_CODE: {
88ca1c9b0cSelric 	if (value != mg->maj_stat || mg->maj_error.length == 0)
89ca1c9b0cSelric 	    break;
90ca1c9b0cSelric 	string->value = malloc(mg->maj_error.length + 1);
91ca1c9b0cSelric 	string->length = mg->maj_error.length;
92ca1c9b0cSelric 	memcpy(string->value, mg->maj_error.value, mg->maj_error.length);
93ca1c9b0cSelric         ((char *) string->value)[string->length] = '\0';
94ca1c9b0cSelric 	return GSS_S_COMPLETE;
95ca1c9b0cSelric     }
96ca1c9b0cSelric     case GSS_C_MECH_CODE: {
97ca1c9b0cSelric 	if (value != mg->min_stat || mg->min_error.length == 0)
98ca1c9b0cSelric 	    break;
99ca1c9b0cSelric 	string->value = malloc(mg->min_error.length + 1);
100ca1c9b0cSelric 	string->length = mg->min_error.length;
101ca1c9b0cSelric 	memcpy(string->value, mg->min_error.value, mg->min_error.length);
102ca1c9b0cSelric         ((char *) string->value)[string->length] = '\0';
103ca1c9b0cSelric 	return GSS_S_COMPLETE;
104ca1c9b0cSelric     }
105ca1c9b0cSelric     }
106ca1c9b0cSelric     string->value = NULL;
107ca1c9b0cSelric     string->length = 0;
108ca1c9b0cSelric     return GSS_S_BAD_STATUS;
109ca1c9b0cSelric }
110ca1c9b0cSelric 
111ca1c9b0cSelric void
_gss_mg_error(gssapi_mech_interface m,OM_uint32 maj,OM_uint32 min)112ca1c9b0cSelric _gss_mg_error(gssapi_mech_interface m, OM_uint32 maj, OM_uint32 min)
113ca1c9b0cSelric {
114ca1c9b0cSelric     OM_uint32 major_status, minor_status;
115ca1c9b0cSelric     OM_uint32 message_content;
116ca1c9b0cSelric     struct mg_thread_ctx *mg;
117ca1c9b0cSelric 
118ca1c9b0cSelric     /*
119ca1c9b0cSelric      * Mechs without gss_display_status() does
120ca1c9b0cSelric      * gss_mg_collect_error() by themself.
121ca1c9b0cSelric      */
122ca1c9b0cSelric     if (m->gm_display_status == NULL)
123ca1c9b0cSelric 	return ;
124ca1c9b0cSelric 
125ca1c9b0cSelric     mg = _gss_mechglue_thread();
126ca1c9b0cSelric     if (mg == NULL)
127ca1c9b0cSelric 	return;
128ca1c9b0cSelric 
129ca1c9b0cSelric     gss_release_buffer(&minor_status, &mg->maj_error);
130ca1c9b0cSelric     gss_release_buffer(&minor_status, &mg->min_error);
131ca1c9b0cSelric 
132ca1c9b0cSelric     mg->mech = &m->gm_mech_oid;
133ca1c9b0cSelric     mg->maj_stat = maj;
134ca1c9b0cSelric     mg->min_stat = min;
135ca1c9b0cSelric 
136ca1c9b0cSelric     major_status = m->gm_display_status(&minor_status,
137ca1c9b0cSelric 					maj,
138ca1c9b0cSelric 					GSS_C_GSS_CODE,
139ca1c9b0cSelric 					&m->gm_mech_oid,
140ca1c9b0cSelric 					&message_content,
141ca1c9b0cSelric 					&mg->maj_error);
142ca1c9b0cSelric     if (GSS_ERROR(major_status)) {
143ca1c9b0cSelric 	mg->maj_error.value = NULL;
144ca1c9b0cSelric 	mg->maj_error.length = 0;
145ca1c9b0cSelric     }
146ca1c9b0cSelric     major_status = m->gm_display_status(&minor_status,
147ca1c9b0cSelric 					min,
148ca1c9b0cSelric 					GSS_C_MECH_CODE,
149ca1c9b0cSelric 					&m->gm_mech_oid,
150ca1c9b0cSelric 					&message_content,
151ca1c9b0cSelric 					&mg->min_error);
152ca1c9b0cSelric     if (GSS_ERROR(major_status)) {
153ca1c9b0cSelric 	mg->min_error.value = NULL;
154ca1c9b0cSelric 	mg->min_error.length = 0;
155ca1c9b0cSelric     }
156ca1c9b0cSelric }
157ca1c9b0cSelric 
158ca1c9b0cSelric void
gss_mg_collect_error(gss_OID mech,OM_uint32 maj,OM_uint32 min)159ca1c9b0cSelric gss_mg_collect_error(gss_OID mech, OM_uint32 maj, OM_uint32 min)
160ca1c9b0cSelric {
161ca1c9b0cSelric     gssapi_mech_interface m = __gss_get_mechanism(mech);
162ca1c9b0cSelric     if (m == NULL)
163ca1c9b0cSelric 	return;
164ca1c9b0cSelric     _gss_mg_error(m, maj, min);
165ca1c9b0cSelric }
166