1 /* $NetBSD: init_sec_context.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2004 PADL Software Pty Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #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
,
258 &ctx
->negotiated_ctx_id
,
260 ctx
->preferred_mech_type
,
265 &ctx
->negotiated_mech_type
,
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
);
276 if (sub
== GSS_S_COMPLETE
)
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
);
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
);
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,
318 if (ret
== 0 && ni_len
!= buf_len
)
324 ret
= der_put_length_and_tag(buf
+ buf_size
- buf_len
- 1,
331 if (ret
== 0 && tmp
+ buf_len
!= buf_size
)
337 free_NegTokenInit(&ni
);
338 _gss_spnego_internal_delete_sec_context(&minor
, &context
, GSS_C_NO_BUFFER
);
339 return GSS_S_FAILURE
;
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
,
358 _gss_spnego_internal_delete_sec_context(&minor
, &context
, GSS_C_NO_BUFFER
);
362 if (actual_mech_type
)
363 *actual_mech_type
= ctx
->negotiated_mech_type
;
365 *ret_flags
= ctx
->mech_flags
;
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
;
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
,
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
,
393 OM_uint32 ret
, minor
;
394 NegotiationToken resp
;
398 gss_buffer_desc mic_buf
, mech_buf
;
399 gss_buffer_desc mech_output_token
;
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
;
415 ret
= decode_NegotiationToken(input_token
->value
, input_token
->length
,
420 if (resp
.element
!= choice_NegotiationToken_negTokenResp
) {
421 free_NegotiationToken(&resp
);
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
) {
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,
451 resp
.u
.negTokenResp
.supportedMech
,
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
,
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
,
470 gss_delete_sec_context(&minor
, &ctx
->negotiated_ctx_id
,
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
;
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
,
501 &ctx
->negotiated_ctx_id
,
508 &ctx
->negotiated_mech_type
,
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
;
519 if (ret
== GSS_S_COMPLETE
) {
522 } else if (*(resp
.u
.negTokenResp
.negResult
) == accept_completed
) {
527 if (*(resp
.u
.negTokenResp
.negResult
) == request_mic
) {
528 ctx
->require_mic
= 1;
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
) {
539 ret
= _gss_spnego_require_mechlist_mic(minor_status
, ctx
,
542 HEIMDAL_MUTEX_unlock(&ctx
->ctx_id_mutex
);
543 free_NegotiationToken(&resp
);
544 gss_release_buffer(&minor
, &mech_output_token
);
553 ASN1_MALLOC_ENCODE(MechTypeList
, mech_buf
.value
, mech_buf
.length
,
554 &ctx
->initiator_mech_types
, &buf_len
, ret
);
556 HEIMDAL_MUTEX_unlock(&ctx
->ctx_id_mutex
);
557 free_NegotiationToken(&resp
);
558 gss_release_buffer(&minor
, &mech_output_token
);
560 return GSS_S_FAILURE
;
562 if (mech_buf
.length
!= buf_len
) {
564 UNREACHABLE(return GSS_S_FAILURE
);
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 GSSAPI_CALLCONV
617 _gss_spnego_init_sec_context
618 (OM_uint32
* minor_status
,
619 const gss_cred_id_t initiator_cred_handle
,
620 gss_ctx_id_t
* context_handle
,
621 const gss_name_t target_name
,
622 const gss_OID mech_type
,
625 const gss_channel_bindings_t input_chan_bindings
,
626 const gss_buffer_t input_token
,
627 gss_OID
* actual_mech_type
,
628 gss_buffer_t output_token
,
629 OM_uint32
* ret_flags
,
633 if (*context_handle
== GSS_C_NO_CONTEXT
)
634 return spnego_initial (minor_status
,
635 initiator_cred_handle
,
648 return spnego_reply (minor_status
,
649 initiator_cred_handle
,