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
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
34 #include "spnego_locl.h"
39 * Is target_name an sane target for `mech´.
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;
49 maj_stat = gss_init_sec_context(&min_stat,
56 GSS_C_NO_CHANNEL_BINDINGS,
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.
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)
88 gss_buffer_desc mic_buf;
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;
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,
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;
153 free_NegotiationToken(&nt);
154 *minor_status = ENOMEM;
155 return GSS_S_FAILURE;
158 nt.u.negTokenResp.mechListMIC = NULL;
161 ASN1_MALLOC_ENCODE(NegotiationToken,
162 output_token->value, output_token->length,
165 free_NegotiationToken(&nt);
167 return GSS_S_FAILURE;
170 if (*(nt.u.negTokenResp.negResult) == accept_completed)
171 ret = GSS_S_COMPLETE;
173 ret = GSS_S_CONTINUE_NEEDED;
175 free_NegotiationToken(&nt);
181 (OM_uint32 * minor_status,
183 gss_ctx_id_t * context_handle,
184 const gss_name_t target_name,
185 const gss_OID mech_type,
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,
198 OM_uint32 sub, minor;
199 gss_buffer_desc mech_token;
201 size_t buf_size, buf_len;
202 gss_buffer_desc data;
204 gss_ctx_id_t context;
206 spnego_name name = (spnego_name)target_name;
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;
222 ctx = (gssspnego_ctx)context;
224 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
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);
235 sub = _gss_spnego_indicate_mechtypelist(&minor,
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);
251 * If we have a credential handle, use it to select the mechanism
255 /* generate optimistic token */
256 sub = gss_init_sec_context(&minor,
257 (cred != NULL) ? cred->negotiated_cred_id :
259 &ctx->negotiated_ctx_id,
261 ctx->preferred_mech_type,
266 &ctx->negotiated_mech_type,
269 &ctx->mech_time_rec);
270 if (GSS_ERROR(sub)) {
271 free_NegTokenInit(&ni);
272 *minor_status = minor;
273 gss_mg_collect_error(ctx->preferred_mech_type, sub, minor);
274 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
277 if (sub == GSS_S_COMPLETE)
280 if (mech_token.length != 0) {
281 ALLOC(ni.mechToken, 1);
282 if (ni.mechToken == NULL) {
283 free_NegTokenInit(&ni);
284 gss_release_buffer(&minor, &mech_token);
285 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
286 *minor_status = ENOMEM;
287 return GSS_S_FAILURE;
289 ni.mechToken->length = mech_token.length;
290 ni.mechToken->data = malloc(mech_token.length);
291 if (ni.mechToken->data == NULL && mech_token.length != 0) {
292 free_NegTokenInit(&ni);
293 gss_release_buffer(&minor, &mech_token);
294 *minor_status = ENOMEM;
295 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
296 return GSS_S_FAILURE;
298 memcpy(ni.mechToken->data, mech_token.value, mech_token.length);
299 gss_release_buffer(&minor, &mech_token);
303 ni.mechListMIC = NULL;
305 ni_len = length_NegTokenInit(&ni);
306 buf_size = 1 + der_length_len(ni_len) + ni_len;
308 buf = malloc(buf_size);
310 free_NegTokenInit(&ni);
311 *minor_status = ENOMEM;
312 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
313 return GSS_S_FAILURE;
316 ret = encode_NegTokenInit(buf + buf_size - 1,
319 if (ret == 0 && ni_len != buf_len)
325 ret = der_put_length_and_tag(buf + buf_size - buf_len - 1,
332 if (ret == 0 && tmp + buf_len != buf_size)
338 free_NegTokenInit(&ni);
339 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
340 return GSS_S_FAILURE;
344 data.length = buf_size;
346 ctx->initiator_mech_types.len = ni.mechTypes.len;
347 ctx->initiator_mech_types.val = ni.mechTypes.val;
348 ni.mechTypes.len = 0;
349 ni.mechTypes.val = NULL;
351 free_NegTokenInit(&ni);
353 sub = gss_encapsulate_token(&data,
354 GSS_SPNEGO_MECHANISM,
359 _gss_spnego_internal_delete_sec_context(&minor, &context, GSS_C_NO_BUFFER);
363 if (actual_mech_type)
364 *actual_mech_type = ctx->negotiated_mech_type;
366 *ret_flags = ctx->mech_flags;
368 *time_rec = ctx->mech_time_rec;
370 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
372 *context_handle = context;
374 return GSS_S_CONTINUE_NEEDED;
379 (OM_uint32 * minor_status,
380 const gssspnego_cred cred,
381 gss_ctx_id_t * context_handle,
382 const gss_name_t target_name,
383 const gss_OID mech_type,
386 const gss_channel_bindings_t input_chan_bindings,
387 const gss_buffer_t input_token,
388 gss_OID * actual_mech_type,
389 gss_buffer_t output_token,
390 OM_uint32 * ret_flags,
394 OM_uint32 ret, minor;
395 NegotiationToken resp;
399 gss_buffer_desc mic_buf, mech_buf;
400 gss_buffer_desc mech_output_token;
405 ctx = (gssspnego_ctx)*context_handle;
407 output_token->length = 0;
408 output_token->value = NULL;
410 mech_output_token.length = 0;
411 mech_output_token.value = NULL;
413 mech_buf.value = NULL;
416 ret = decode_NegotiationToken(input_token->value, input_token->length,
421 if (resp.element != choice_NegotiationToken_negTokenResp) {
422 free_NegotiationToken(&resp);
424 return GSS_S_BAD_MECH;
427 if (resp.u.negTokenResp.negResult == NULL
428 || *(resp.u.negTokenResp.negResult) == reject
429 /* || resp.u.negTokenResp.supportedMech == NULL */
432 free_NegotiationToken(&resp);
433 return GSS_S_BAD_MECH;
437 * Pick up the mechanism that the acceptor selected, only allow it
438 * to be sent in packet.
441 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
443 if (resp.u.negTokenResp.supportedMech) {
446 free_NegotiationToken(&resp);
447 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
448 return GSS_S_BAD_MECH;
450 ret = der_put_oid(ctx->oidbuf + sizeof(ctx->oidbuf) - 1,
452 resp.u.negTokenResp.supportedMech,
454 /* Avoid recursively embedded SPNEGO */
455 if (ret || (ctx->oidlen == GSS_SPNEGO_MECHANISM->length &&
456 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
457 GSS_SPNEGO_MECHANISM->elements,
460 free_NegotiationToken(&resp);
461 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
462 return GSS_S_BAD_MECH;
465 /* check if the acceptor took our optimistic token */
466 if (ctx->oidlen != ctx->preferred_mech_type->length ||
467 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
468 ctx->preferred_mech_type->elements,
471 gss_delete_sec_context(&minor, &ctx->negotiated_ctx_id,
473 ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
475 } else if (ctx->oidlen == 0) {
476 free_NegotiationToken(&resp);
477 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
478 return GSS_S_BAD_MECH;
481 /* if a token (of non zero length), or no context, pass to underlaying mech */
482 if ((resp.u.negTokenResp.responseToken != NULL && resp.u.negTokenResp.responseToken->length) ||
483 ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) {
484 gss_buffer_desc mech_input_token;
486 if (resp.u.negTokenResp.responseToken) {
487 mech_input_token.length = resp.u.negTokenResp.responseToken->length;
488 mech_input_token.value = resp.u.negTokenResp.responseToken->data;
490 mech_input_token.length = 0;
491 mech_input_token.value = NULL;
495 mech.length = ctx->oidlen;
496 mech.elements = ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen;
498 /* Fall through as if the negotiated mechanism
499 was requested explicitly */
500 ret = gss_init_sec_context(&minor,
501 (cred != NULL) ? cred->negotiated_cred_id :
503 &ctx->negotiated_ctx_id,
510 &ctx->negotiated_mech_type,
513 &ctx->mech_time_rec);
514 if (GSS_ERROR(ret)) {
515 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
516 free_NegotiationToken(&resp);
517 gss_mg_collect_error(&mech, ret, minor);
518 *minor_status = minor;
521 if (ret == GSS_S_COMPLETE) {
524 } else if (*(resp.u.negTokenResp.negResult) == accept_completed) {
529 if (*(resp.u.negTokenResp.negResult) == request_mic) {
530 ctx->require_mic = 1;
535 * Verify the mechListMIC if one was provided or CFX was
536 * used and a non-preferred mechanism was selected
538 if (resp.u.negTokenResp.mechListMIC != NULL) {
541 ret = _gss_spnego_require_mechlist_mic(minor_status, ctx,
544 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
545 free_NegotiationToken(&resp);
546 gss_release_buffer(&minor, &mech_output_token);
555 ASN1_MALLOC_ENCODE(MechTypeList, mech_buf.value, mech_buf.length,
556 &ctx->initiator_mech_types, &buf_len, ret);
558 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
559 free_NegotiationToken(&resp);
560 gss_release_buffer(&minor, &mech_output_token);
562 return GSS_S_FAILURE;
564 if (mech_buf.length != buf_len)
567 if (resp.u.negTokenResp.mechListMIC == NULL) {
568 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
569 free(mech_buf.value);
570 free_NegotiationToken(&resp);
572 return GSS_S_DEFECTIVE_TOKEN;
574 mic_buf.length = resp.u.negTokenResp.mechListMIC->length;
575 mic_buf.value = resp.u.negTokenResp.mechListMIC->data;
577 if (mech_output_token.length == 0) {
578 ret = gss_verify_mic(minor_status,
579 ctx->negotiated_ctx_id,
584 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
585 free(mech_buf.value);
586 gss_release_buffer(&minor, &mech_output_token);
587 free_NegotiationToken(&resp);
588 return GSS_S_DEFECTIVE_TOKEN;
590 ctx->verified_mic = 1;
594 ret = spnego_reply_internal(minor_status, ctx,
595 require_mic ? &mech_buf : NULL,
599 if (mech_buf.value != NULL)
600 free(mech_buf.value);
602 free_NegotiationToken(&resp);
603 gss_release_buffer(&minor, &mech_output_token);
605 if (actual_mech_type)
606 *actual_mech_type = ctx->negotiated_mech_type;
608 *ret_flags = ctx->mech_flags;
610 *time_rec = ctx->mech_time_rec;
612 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
616 OM_uint32 _gss_spnego_init_sec_context
617 (OM_uint32 * minor_status,
618 const gss_cred_id_t initiator_cred_handle,
619 gss_ctx_id_t * context_handle,
620 const gss_name_t target_name,
621 const gss_OID mech_type,
624 const gss_channel_bindings_t input_chan_bindings,
625 const gss_buffer_t input_token,
626 gss_OID * actual_mech_type,
627 gss_buffer_t output_token,
628 OM_uint32 * ret_flags,
632 gssspnego_cred cred = (gssspnego_cred)initiator_cred_handle;
634 if (*context_handle == GSS_C_NO_CONTEXT)
635 return spnego_initial (minor_status,
649 return spnego_reply (minor_status,