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/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;
400 gss_buffer_desc mic_buf, mech_buf;
401 gss_buffer_desc mech_output_token;
406 ctx = (gssspnego_ctx)*context_handle;
408 output_token->length = 0;
409 output_token->value = NULL;
411 mech_output_token.length = 0;
412 mech_output_token.value = NULL;
414 mech_buf.value = NULL;
417 ret = der_match_tag_and_length(input_token->value, input_token->length,
418 ASN1_C_CONTEXT, CONS, 1, &len, &taglen);
422 if (len > input_token->length - taglen)
425 ret = decode_NegTokenResp((const unsigned char *)input_token->value+taglen,
428 *minor_status = ENOMEM;
429 return GSS_S_FAILURE;
432 if (resp.negResult == NULL
433 || *(resp.negResult) == reject
434 /* || resp.supportedMech == NULL */
437 free_NegTokenResp(&resp);
438 return GSS_S_BAD_MECH;
442 * Pick up the mechanism that the acceptor selected, only allow it
443 * to be sent in packet.
446 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
448 if (resp.supportedMech) {
451 free_NegTokenResp(&resp);
452 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
453 return GSS_S_BAD_MECH;
455 ret = der_put_oid(ctx->oidbuf + sizeof(ctx->oidbuf) - 1,
459 /* Avoid recursively embedded SPNEGO */
460 if (ret || (ctx->oidlen == GSS_SPNEGO_MECHANISM->length &&
461 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
462 GSS_SPNEGO_MECHANISM->elements,
465 free_NegTokenResp(&resp);
466 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
467 return GSS_S_BAD_MECH;
470 /* check if the acceptor took our optimistic token */
471 if (ctx->oidlen != ctx->preferred_mech_type->length ||
472 memcmp(ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen,
473 ctx->preferred_mech_type->elements,
476 gss_delete_sec_context(&minor, &ctx->negotiated_ctx_id,
478 ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;
480 } else if (ctx->oidlen == 0) {
481 free_NegTokenResp(&resp);
482 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
483 return GSS_S_BAD_MECH;
486 /* if a token (of non zero length), or no context, pass to underlaying mech */
487 if ((resp.responseToken != NULL && resp.responseToken->length) ||
488 ctx->negotiated_ctx_id == GSS_C_NO_CONTEXT) {
489 gss_buffer_desc mech_input_token;
491 if (resp.responseToken) {
492 mech_input_token.length = resp.responseToken->length;
493 mech_input_token.value = resp.responseToken->data;
495 mech_input_token.length = 0;
496 mech_input_token.value = NULL;
500 mech.length = ctx->oidlen;
501 mech.elements = ctx->oidbuf + sizeof(ctx->oidbuf) - ctx->oidlen;
503 /* Fall through as if the negotiated mechanism
504 was requested explicitly */
505 ret = gss_init_sec_context(&minor,
506 (cred != NULL) ? cred->negotiated_cred_id :
508 &ctx->negotiated_ctx_id,
515 &ctx->negotiated_mech_type,
518 &ctx->mech_time_rec);
519 if (GSS_ERROR(ret)) {
520 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
521 free_NegTokenResp(&resp);
522 gss_mg_collect_error(&mech, ret, minor);
523 *minor_status = minor;
526 if (ret == GSS_S_COMPLETE) {
529 } else if (*(resp.negResult) == accept_completed) {
534 if (*(resp.negResult) == request_mic) {
535 ctx->require_mic = 1;
540 * Verify the mechListMIC if one was provided or CFX was
541 * used and a non-preferred mechanism was selected
543 if (resp.mechListMIC != NULL) {
546 ret = _gss_spnego_require_mechlist_mic(minor_status, ctx,
549 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
550 free_NegTokenResp(&resp);
551 gss_release_buffer(&minor, &mech_output_token);
560 ASN1_MALLOC_ENCODE(MechTypeList, mech_buf.value, mech_buf.length,
561 &ctx->initiator_mech_types, &buf_len, ret);
563 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
564 free_NegTokenResp(&resp);
565 gss_release_buffer(&minor, &mech_output_token);
567 return GSS_S_FAILURE;
569 if (mech_buf.length != buf_len)
572 if (resp.mechListMIC == NULL) {
573 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
574 free(mech_buf.value);
575 free_NegTokenResp(&resp);
577 return GSS_S_DEFECTIVE_TOKEN;
579 mic_buf.length = resp.mechListMIC->length;
580 mic_buf.value = resp.mechListMIC->data;
582 if (mech_output_token.length == 0) {
583 ret = gss_verify_mic(minor_status,
584 ctx->negotiated_ctx_id,
589 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
590 free(mech_buf.value);
591 gss_release_buffer(&minor, &mech_output_token);
592 free_NegTokenResp(&resp);
593 return GSS_S_DEFECTIVE_TOKEN;
595 ctx->verified_mic = 1;
599 ret = spnego_reply_internal(minor_status, ctx,
600 require_mic ? &mech_buf : NULL,
604 if (mech_buf.value != NULL)
605 free(mech_buf.value);
607 free_NegTokenResp(&resp);
608 gss_release_buffer(&minor, &mech_output_token);
610 if (actual_mech_type)
611 *actual_mech_type = ctx->negotiated_mech_type;
613 *ret_flags = ctx->mech_flags;
615 *time_rec = ctx->mech_time_rec;
617 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
621 OM_uint32 _gss_spnego_init_sec_context
622 (OM_uint32 * minor_status,
623 const gss_cred_id_t initiator_cred_handle,
624 gss_ctx_id_t * context_handle,
625 const gss_name_t target_name,
626 const gss_OID mech_type,
629 const gss_channel_bindings_t input_chan_bindings,
630 const gss_buffer_t input_token,
631 gss_OID * actual_mech_type,
632 gss_buffer_t output_token,
633 OM_uint32 * ret_flags,
637 gssspnego_cred cred = (gssspnego_cred)initiator_cred_handle;
639 if (*context_handle == GSS_C_NO_CONTEXT)
640 return spnego_initial (minor_status,
654 return spnego_reply (minor_status,