Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / gssapi / mech / context.c
blob696c8ab63cd4f5d19eeb59ebf4bb3f52e46c3af9
1 /* $NetBSD: context.c,v 1.1.1.1 2011/04/13 18:14:46 elric Exp $ */
3 #include "mech_locl.h"
4 #include "heim_threads.h"
6 struct mg_thread_ctx {
7 gss_OID mech;
8 OM_uint32 maj_stat;
9 OM_uint32 min_stat;
10 gss_buffer_desc maj_error;
11 gss_buffer_desc min_error;
14 static HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
15 static int created_key;
16 static HEIMDAL_thread_key context_key;
19 static void
20 destroy_context(void *ptr)
22 struct mg_thread_ctx *mg = ptr;
23 OM_uint32 junk;
25 if (mg == NULL)
26 return;
28 gss_release_buffer(&junk, &mg->maj_error);
29 gss_release_buffer(&junk, &mg->min_error);
30 free(mg);
34 static struct mg_thread_ctx *
35 _gss_mechglue_thread(void)
37 struct mg_thread_ctx *ctx;
38 int ret = 0;
40 HEIMDAL_MUTEX_lock(&context_mutex);
42 if (!created_key) {
43 HEIMDAL_key_create(&context_key, destroy_context, ret);
44 if (ret) {
45 HEIMDAL_MUTEX_unlock(&context_mutex);
46 return NULL;
48 created_key = 1;
50 HEIMDAL_MUTEX_unlock(&context_mutex);
52 ctx = HEIMDAL_getspecific(context_key);
53 if (ctx == NULL) {
55 ctx = calloc(1, sizeof(*ctx));
56 if (ctx == NULL)
57 return NULL;
58 HEIMDAL_setspecific(context_key, ctx, ret);
59 if (ret) {
60 free(ctx);
61 return NULL;
64 return ctx;
67 OM_uint32
68 _gss_mg_get_error(const gss_OID mech, OM_uint32 type,
69 OM_uint32 value, gss_buffer_t string)
71 struct mg_thread_ctx *mg;
73 mg = _gss_mechglue_thread();
74 if (mg == NULL)
75 return GSS_S_BAD_STATUS;
77 #if 0
79 * We cant check the mech here since a pseudo-mech might have
80 * called an lower layer and then the mech info is all broken
82 if (mech != NULL && gss_oid_equal(mg->mech, mech) == 0)
83 return GSS_S_BAD_STATUS;
84 #endif
86 switch (type) {
87 case GSS_C_GSS_CODE: {
88 if (value != mg->maj_stat || mg->maj_error.length == 0)
89 break;
90 string->value = malloc(mg->maj_error.length + 1);
91 string->length = mg->maj_error.length;
92 memcpy(string->value, mg->maj_error.value, mg->maj_error.length);
93 ((char *) string->value)[string->length] = '\0';
94 return GSS_S_COMPLETE;
96 case GSS_C_MECH_CODE: {
97 if (value != mg->min_stat || mg->min_error.length == 0)
98 break;
99 string->value = malloc(mg->min_error.length + 1);
100 string->length = mg->min_error.length;
101 memcpy(string->value, mg->min_error.value, mg->min_error.length);
102 ((char *) string->value)[string->length] = '\0';
103 return GSS_S_COMPLETE;
106 string->value = NULL;
107 string->length = 0;
108 return GSS_S_BAD_STATUS;
111 void
112 _gss_mg_error(gssapi_mech_interface m, OM_uint32 maj, OM_uint32 min)
114 OM_uint32 major_status, minor_status;
115 OM_uint32 message_content;
116 struct mg_thread_ctx *mg;
119 * Mechs without gss_display_status() does
120 * gss_mg_collect_error() by themself.
122 if (m->gm_display_status == NULL)
123 return ;
125 mg = _gss_mechglue_thread();
126 if (mg == NULL)
127 return;
129 gss_release_buffer(&minor_status, &mg->maj_error);
130 gss_release_buffer(&minor_status, &mg->min_error);
132 mg->mech = &m->gm_mech_oid;
133 mg->maj_stat = maj;
134 mg->min_stat = min;
136 major_status = m->gm_display_status(&minor_status,
137 maj,
138 GSS_C_GSS_CODE,
139 &m->gm_mech_oid,
140 &message_content,
141 &mg->maj_error);
142 if (GSS_ERROR(major_status)) {
143 mg->maj_error.value = NULL;
144 mg->maj_error.length = 0;
146 major_status = m->gm_display_status(&minor_status,
147 min,
148 GSS_C_MECH_CODE,
149 &m->gm_mech_oid,
150 &message_content,
151 &mg->min_error);
152 if (GSS_ERROR(major_status)) {
153 mg->min_error.value = NULL;
154 mg->min_error.length = 0;
158 void
159 gss_mg_collect_error(gss_OID mech, OM_uint32 maj, OM_uint32 min)
161 gssapi_mech_interface m = __gss_get_mechanism(mech);
162 if (m == NULL)
163 return;
164 _gss_mg_error(m, maj, min);