set pointer before freeing
[heimdal.git] / lib / gssapi / spnego / init_sec_context.c
blob6afd524a5b099bcf76d88abcd04c0b3def1cd531
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * Portions Copyright (c) 2004 PADL Software Pty Ltd.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "spnego_locl.h"
36 RCSID("$Id$");
39 * Is target_name an sane target for `mech´.
42 static OM_uint32
43 initiator_approved(gss_name_t target_name, gss_OID mech)
45 OM_uint32 min_stat, maj_stat;
46 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
47 gss_buffer_desc out;
49 maj_stat = gss_init_sec_context(&min_stat,
50 GSS_C_NO_CREDENTIAL,
51 &ctx,
52 target_name,
53 mech,
55 GSS_C_INDEFINITE,
56 GSS_C_NO_CHANNEL_BINDINGS,
57 GSS_C_NO_BUFFER,
58 NULL,
59 &out,
60 NULL,
61 NULL);
62 if (GSS_ERROR(maj_stat)) {
63 gss_mg_collect_error(mech, maj_stat, min_stat);
64 return GSS_S_BAD_MECH;
66 gss_release_buffer(&min_stat, &out);
67 gss_delete_sec_context(&min_stat, &ctx, NULL);
69 return GSS_S_COMPLETE;
73 * Send a reply. Note that we only need to send a reply if we
74 * need to send a MIC or a mechanism token. Otherwise, we can
75 * return an empty buffer.
77 * The return value of this will be returned to the API, so it
78 * must return GSS_S_CONTINUE_NEEDED if a token was generated.
80 static OM_uint32
81 spnego_reply_internal(OM_uint32 *minor_status,
82 gssspnego_ctx context_handle,
83 const gss_buffer_t mech_buf,
84 gss_buffer_t mech_token,
85 gss_buffer_t output_token)
87 NegotiationToken nt;
88 gss_buffer_desc mic_buf;
89 OM_uint32 ret;
90 size_t size;
92 if (mech_buf == GSS_C_NO_BUFFER && mech_token->length == 0) {
93 output_token->length = 0;
94 output_token->value = NULL;
96 return context_handle->open ? GSS_S_COMPLETE : GSS_S_FAILURE;
99 memset(&nt, 0, sizeof(nt));
101 nt.element = choice_NegotiationToken_negTokenResp;
103 ALLOC(nt.u.negTokenResp.negResult, 1);
104 if (nt.u.negTokenResp.negResult == NULL) {
105 *minor_status = ENOMEM;
106 return GSS_S_FAILURE;
109 nt.u.negTokenResp.supportedMech = NULL;
111 output_token->length = 0;
112 output_token->value = NULL;
114 if (mech_token->length == 0) {
115 nt.u.negTokenResp.responseToken = NULL;
116 *(nt.u.negTokenResp.negResult) = accept_completed;
117 } else {
118 ALLOC(nt.u.negTokenResp.responseToken, 1);
119 if (nt.u.negTokenResp.responseToken == NULL) {
120 free_NegotiationToken(&nt);
121 *minor_status = ENOMEM;
122 return GSS_S_FAILURE;
124 nt.u.negTokenResp.responseToken->length = mech_token->length;
125 nt.u.negTokenResp.responseToken->data = mech_token->value;
126 mech_token->length = 0;
127 mech_token->value = NULL;
129 *(nt.u.negTokenResp.negResult) = accept_incomplete;
132 if (mech_buf != GSS_C_NO_BUFFER) {
134 ret = gss_get_mic(minor_status,
135 context_handle->negotiated_ctx_id,
137 mech_buf,
138 &mic_buf);
139 if (ret == GSS_S_COMPLETE) {
140 ALLOC(nt.u.negTokenResp.mechListMIC, 1);
141 if (nt.u.negTokenResp.mechListMIC == NULL) {
142 gss_release_buffer(minor_status, &mic_buf);
143 free_NegotiationToken(&nt);
144 *minor_status = ENOMEM;
145 return GSS_S_FAILURE;
148 nt.u.negTokenResp.mechListMIC->length = mic_buf.length;
149 nt.u.negTokenResp.mechListMIC->data = mic_buf.value;
150 } else if (ret == GSS_S_UNAVAILABLE) {
151 nt.u.negTokenResp.mechListMIC = NULL;
152 } if (ret) {
153 free_NegotiationToken(&nt);
154 *minor_status = ENOMEM;
155 return GSS_S_FAILURE;
157 } else {
158 nt.u.negTokenResp.mechListMIC = NULL;
161 ASN1_MALLOC_ENCODE(NegotiationToken,
162 output_token->value, output_token->length,
163 &nt, &size, ret);
164 if (ret) {
165 free_NegotiationToken(&nt);
166 *minor_status = ret;
167 return GSS_S_FAILURE;
170 if (*(nt.u.negTokenResp.negResult) == accept_completed)
171 ret = GSS_S_COMPLETE;
172 else
173 ret = GSS_S_CONTINUE_NEEDED;
175 free_NegotiationToken(&nt);
176 return ret;
179 static OM_uint32
180 spnego_initial
181 (OM_uint32 * minor_status,
182 gss_cred_id_t cred,
183 gss_ctx_id_t * context_handle,
184 const gss_name_t target_name,
185 const gss_OID mech_type,
186 OM_uint32 req_flags,
187 OM_uint32 time_req,
188 const gss_channel_bindings_t input_chan_bindings,
189 const gss_buffer_t input_token,
190 gss_OID * actual_mech_type,
191 gss_buffer_t output_token,
192 OM_uint32 * ret_flags,
193 OM_uint32 * time_rec
196 NegTokenInit ni;
197 int ret;
198 OM_uint32 sub, minor;
199 gss_buffer_desc mech_token;
200 u_char *buf;
201 size_t buf_size, buf_len;
202 gss_buffer_desc data;
203 size_t ni_len;
204 gss_ctx_id_t context;
205 gssspnego_ctx ctx;
206 spnego_name name = (spnego_name)target_name;
208 *minor_status = 0;
210 memset (&ni, 0, sizeof(ni));
212 *context_handle = GSS_C_NO_CONTEXT;
214 if (target_name == GSS_C_NO_NAME)
215 return GSS_S_BAD_NAME;
217 sub = _gss_spnego_alloc_sec_context(&minor, &context);
218 if (GSS_ERROR(sub)) {
219 *minor_status = minor;
220 return sub;
222 ctx = (gssspnego_ctx)context;
224 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
226 ctx->local = 1;
228 sub = gss_import_name(&minor, &name->value, &name->type, &ctx->target_name);
229 if (GSS_ERROR(sub)) {
230 *minor_status = minor;
231 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
232 return sub;
235 sub = _gss_spnego_indicate_mechtypelist(&minor,
236 ctx->target_name,
237 initiator_approved,
239 cred,
240 &ni.mechTypes,
241 &ctx->preferred_mech_type);
242 if (GSS_ERROR(sub)) {
243 *minor_status = minor;
244 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
245 return sub;
248 ni.reqFlags = NULL;
251 * If we have a credential handle, use it to select the mechanism
252 * that we will use
255 /* generate optimistic token */
256 sub = gss_init_sec_context(&minor,
257 cred,
258 &ctx->negotiated_ctx_id,
259 ctx->target_name,
260 ctx->preferred_mech_type,
261 req_flags,
262 time_req,
263 input_chan_bindings,
264 input_token,
265 &ctx->negotiated_mech_type,
266 &mech_token,
267 &ctx->mech_flags,
268 &ctx->mech_time_rec);
269 if (GSS_ERROR(sub)) {
270 free_NegTokenInit(&ni);
271 *minor_status = minor;
272 gss_mg_collect_error(ctx->preferred_mech_type, sub, minor);
273 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
274 return sub;
276 if (sub == GSS_S_COMPLETE)
277 ctx->maybe_open = 1;
279 if (mech_token.length != 0) {
280 ALLOC(ni.mechToken, 1);
281 if (ni.mechToken == NULL) {
282 free_NegTokenInit(&ni);
283 gss_release_buffer(&minor, &mech_token);
284 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
285 *minor_status = ENOMEM;
286 return GSS_S_FAILURE;
288 ni.mechToken->length = mech_token.length;
289 ni.mechToken->data = malloc(mech_token.length);
290 if (ni.mechToken->data == NULL && mech_token.length != 0) {
291 free_NegTokenInit(&ni);
292 gss_release_buffer(&minor, &mech_token);
293 *minor_status = ENOMEM;
294 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
295 return GSS_S_FAILURE;
297 memcpy(ni.mechToken->data, mech_token.value, mech_token.length);
298 gss_release_buffer(&minor, &mech_token);
299 } else
300 ni.mechToken = NULL;
302 ni.mechListMIC = NULL;
304 ni_len = length_NegTokenInit(&ni);
305 buf_size = 1 + der_length_len(ni_len) + ni_len;
307 buf = malloc(buf_size);
308 if (buf == NULL) {
309 free_NegTokenInit(&ni);
310 *minor_status = ENOMEM;
311 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
312 return GSS_S_FAILURE;
315 ret = encode_NegTokenInit(buf + buf_size - 1,
316 ni_len,
317 &ni, &buf_len);
318 if (ret == 0 && ni_len != buf_len)
319 abort();
321 if (ret == 0) {
322 size_t tmp;
324 ret = der_put_length_and_tag(buf + buf_size - buf_len - 1,
325 buf_size - buf_len,
326 buf_len,
327 ASN1_C_CONTEXT,
328 CONS,
330 &tmp);
331 if (ret == 0 && tmp + buf_len != buf_size)
332 abort();
334 if (ret) {
335 *minor_status = ret;
336 free(buf);
337 free_NegTokenInit(&ni);
338 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
339 return GSS_S_FAILURE;
342 data.value = buf;
343 data.length = buf_size;
345 ctx->initiator_mech_types.len = ni.mechTypes.len;
346 ctx->initiator_mech_types.val = ni.mechTypes.val;
347 ni.mechTypes.len = 0;
348 ni.mechTypes.val = NULL;
350 free_NegTokenInit(&ni);
352 sub = gss_encapsulate_token(&data,
353 GSS_SPNEGO_MECHANISM,
354 output_token);
355 free (buf);
357 if (sub) {
358 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
359 return sub;
362 if (actual_mech_type)
363 *actual_mech_type = ctx->negotiated_mech_type;
364 if (ret_flags)
365 *ret_flags = ctx->mech_flags;
366 if (time_rec)
367 *time_rec = ctx->mech_time_rec;
369 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
371 *context_handle = context;
373 return GSS_S_CONTINUE_NEEDED;
376 static OM_uint32
377 spnego_reply
378 (OM_uint32 * minor_status,
379 const gss_cred_id_t cred,
380 gss_ctx_id_t * context_handle,
381 const gss_name_t target_name,
382 const gss_OID mech_type,
383 OM_uint32 req_flags,
384 OM_uint32 time_req,
385 const gss_channel_bindings_t input_chan_bindings,
386 const gss_buffer_t input_token,
387 gss_OID * actual_mech_type,
388 gss_buffer_t output_token,
389 OM_uint32 * ret_flags,
390 OM_uint32 * time_rec
393 OM_uint32 ret, minor;
394 NegotiationToken resp;
395 gss_OID_desc mech;
396 int require_mic;
397 size_t buf_len;
398 gss_buffer_desc mic_buf, mech_buf;
399 gss_buffer_desc mech_output_token;
400 gssspnego_ctx ctx;
402 *minor_status = 0;
404 ctx = (gssspnego_ctx)*context_handle;
406 output_token->length = 0;
407 output_token->value = NULL;
409 mech_output_token.length = 0;
410 mech_output_token.value = NULL;
412 mech_buf.value = NULL;
413 mech_buf.length = 0;
415 ret = decode_NegotiationToken(input_token->value, input_token->length,
416 &resp, NULL);
417 if (ret)
418 return ret;
420 if (resp.element != choice_NegotiationToken_negTokenResp) {
421 free_NegotiationToken(&resp);
422 *minor_status = 0;
423 return GSS_S_BAD_MECH;
426 if (resp.u.negTokenResp.negResult == NULL
427 || *(resp.u.negTokenResp.negResult) == reject
428 /* || resp.u.negTokenResp.supportedMech == NULL */
431 free_NegotiationToken(&resp);
432 return GSS_S_BAD_MECH;
436 * Pick up the mechanism that the acceptor selected, only allow it
437 * to be sent in packet.
440 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
442 if (resp.u.negTokenResp.supportedMech) {
444 if (ctx->oidlen) {
445 free_NegotiationToken(&resp);
446 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
447 return GSS_S_BAD_MECH;
449 ret = der_put_oid(ctx->oidbuf + sizeof(ctx->oidbuf) - 1,
450 sizeof(ctx->oidbuf),
451 resp.u.negTokenResp.supportedMech,
452 &ctx->oidlen);
453 /* Avoid recursively embedded SPNEGO */
454 if (ret || (ctx->oidlen == GSS_SPNEGO_MECHANISM->length &&
455 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
456 GSS_SPNEGO_MECHANISM->elements,
457 ctx->oidlen) == 0))
459 free_NegotiationToken(&resp);
460 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
461 return GSS_S_BAD_MECH;
464 /* check if the acceptor took our optimistic token */
465 if (ctx->oidlen != ctx->preferred_mech_type->length ||
466 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
467 ctx->preferred_mech_type->elements,
468 ctx->oidlen) != 0)
470 gss_delete_sec_context(&minor, &ctx->negotiated_ctx_id,
471 GSS_C_NO_BUFFER);
472 ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
474 } else if (ctx->oidlen == 0) {
475 free_NegotiationToken(&resp);
476 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
477 return GSS_S_BAD_MECH;
480 /* if a token (of non zero length), or no context, pass to underlaying mech */
481 if ((resp.u.negTokenResp.responseToken != NULL && resp.u.negTokenResp.responseToken->length) ||
482 ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) {
483 gss_buffer_desc mech_input_token;
485 if (resp.u.negTokenResp.responseToken) {
486 mech_input_token.length = resp.u.negTokenResp.responseToken->length;
487 mech_input_token.value = resp.u.negTokenResp.responseToken->data;
488 } else {
489 mech_input_token.length = 0;
490 mech_input_token.value = NULL;
494 mech.length = ctx->oidlen;
495 mech.elements = ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen;
497 /* Fall through as if the negotiated mechanism
498 was requested explicitly */
499 ret = gss_init_sec_context(&minor,
500 cred,
501 &ctx->negotiated_ctx_id,
502 ctx->target_name,
503 &mech,
504 req_flags,
505 time_req,
506 input_chan_bindings,
507 &mech_input_token,
508 &ctx->negotiated_mech_type,
509 &mech_output_token,
510 &ctx->mech_flags,
511 &ctx->mech_time_rec);
512 if (GSS_ERROR(ret)) {
513 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
514 free_NegotiationToken(&resp);
515 gss_mg_collect_error(&mech, ret, minor);
516 *minor_status = minor;
517 return ret;
519 if (ret == GSS_S_COMPLETE) {
520 ctx->open = 1;
522 } else if (*(resp.u.negTokenResp.negResult) == accept_completed) {
523 if (ctx->maybe_open)
524 ctx->open = 1;
527 if (*(resp.u.negTokenResp.negResult) == request_mic) {
528 ctx->require_mic = 1;
531 if (ctx->open) {
533 * Verify the mechListMIC if one was provided or CFX was
534 * used and a non-preferred mechanism was selected
536 if (resp.u.negTokenResp.mechListMIC != NULL) {
537 require_mic = 1;
538 } else {
539 ret = _gss_spnego_require_mechlist_mic(minor_status, ctx,
540 &require_mic);
541 if (ret) {
542 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
543 free_NegotiationToken(&resp);
544 gss_release_buffer(&minor, &mech_output_token);
545 return ret;
548 } else {
549 require_mic = 0;
552 if (require_mic) {
553 ASN1_MALLOC_ENCODE(MechTypeList, mech_buf.value, mech_buf.length,
554 &ctx->initiator_mech_types, &buf_len, ret);
555 if (ret) {
556 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
557 free_NegotiationToken(&resp);
558 gss_release_buffer(&minor, &mech_output_token);
559 *minor_status = ret;
560 return GSS_S_FAILURE;
562 if (mech_buf.length != buf_len)
563 abort();
565 if (resp.u.negTokenResp.mechListMIC == NULL) {
566 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
567 free(mech_buf.value);
568 free_NegotiationToken(&resp);
569 *minor_status = 0;
570 return GSS_S_DEFECTIVE_TOKEN;
572 mic_buf.length = resp.u.negTokenResp.mechListMIC->length;
573 mic_buf.value = resp.u.negTokenResp.mechListMIC->data;
575 if (mech_output_token.length == 0) {
576 ret = gss_verify_mic(minor_status,
577 ctx->negotiated_ctx_id,
578 &mech_buf,
579 &mic_buf,
580 NULL);
581 if (ret) {
582 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
583 free(mech_buf.value);
584 gss_release_buffer(&minor, &mech_output_token);
585 free_NegotiationToken(&resp);
586 return GSS_S_DEFECTIVE_TOKEN;
588 ctx->verified_mic = 1;
592 ret = spnego_reply_internal(minor_status, ctx,
593 require_mic ? &mech_buf : NULL,
594 &mech_output_token,
595 output_token);
597 if (mech_buf.value != NULL)
598 free(mech_buf.value);
600 free_NegotiationToken(&resp);
601 gss_release_buffer(&minor, &mech_output_token);
603 if (actual_mech_type)
604 *actual_mech_type = ctx->negotiated_mech_type;
605 if (ret_flags)
606 *ret_flags = ctx->mech_flags;
607 if (time_rec)
608 *time_rec = ctx->mech_time_rec;
610 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
611 return ret;
614 OM_uint32 _gss_spnego_init_sec_context
615 (OM_uint32 * minor_status,
616 const gss_cred_id_t initiator_cred_handle,
617 gss_ctx_id_t * context_handle,
618 const gss_name_t target_name,
619 const gss_OID mech_type,
620 OM_uint32 req_flags,
621 OM_uint32 time_req,
622 const gss_channel_bindings_t input_chan_bindings,
623 const gss_buffer_t input_token,
624 gss_OID * actual_mech_type,
625 gss_buffer_t output_token,
626 OM_uint32 * ret_flags,
627 OM_uint32 * time_rec
630 if (*context_handle == GSS_C_NO_CONTEXT)
631 return spnego_initial (minor_status,
632 initiator_cred_handle,
633 context_handle,
634 target_name,
635 mech_type,
636 req_flags,
637 time_req,
638 input_chan_bindings,
639 input_token,
640 actual_mech_type,
641 output_token,
642 ret_flags,
643 time_rec);
644 else
645 return spnego_reply (minor_status,
646 initiator_cred_handle,
647 context_handle,
648 target_name,
649 mech_type,
650 req_flags,
651 time_req,
652 input_chan_bindings,
653 input_token,
654 actual_mech_type,
655 output_token,
656 ret_flags,
657 time_rec);