2 Unix SMB/CIFS implementation.
4 RFC2478 Compliant SPNEGO implementation
6 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 Copyright (C) Stefan Metzmacher <metze@samba.org> 2004-2008
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "lib/util/tevent_ntstatus.h"
28 #include "../libcli/auth/spnego.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_internal.h"
33 #include "param/param.h"
34 #include "lib/util/asn1.h"
35 #include "lib/util/base64.h"
39 _PUBLIC_ NTSTATUS
gensec_spnego_init(TALLOC_CTX
*ctx
);
41 enum spnego_state_position
{
51 enum spnego_message_type expected_packet
;
52 enum spnego_state_position state_position
;
53 struct gensec_security
*sub_sec_security
;
64 bool may_skip_mic_check
;
70 * The following is used to implement
71 * the update token fragmentation
75 size_t out_max_length
;
80 static void gensec_spnego_update_sub_abort(struct spnego_state
*spnego_state
)
82 spnego_state
->sub_sec_ready
= false;
83 TALLOC_FREE(spnego_state
->sub_sec_security
);
86 static NTSTATUS
gensec_spnego_client_start(struct gensec_security
*gensec_security
)
88 struct spnego_state
*spnego_state
;
90 spnego_state
= talloc_zero(gensec_security
, struct spnego_state
);
92 return NT_STATUS_NO_MEMORY
;
95 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
96 spnego_state
->state_position
= SPNEGO_CLIENT_START
;
97 spnego_state
->sub_sec_security
= NULL
;
98 spnego_state
->sub_sec_ready
= false;
99 spnego_state
->mech_types
= data_blob_null
;
100 spnego_state
->out_max_length
= gensec_max_update_size(gensec_security
);
101 spnego_state
->out_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
103 spnego_state
->simulate_w2k
= gensec_setting_bool(gensec_security
->settings
,
104 "spnego", "simulate_w2k", false);
106 gensec_security
->private_data
= spnego_state
;
110 static NTSTATUS
gensec_spnego_server_start(struct gensec_security
*gensec_security
)
112 struct spnego_state
*spnego_state
;
114 spnego_state
= talloc_zero(gensec_security
, struct spnego_state
);
116 return NT_STATUS_NO_MEMORY
;
119 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
120 spnego_state
->state_position
= SPNEGO_SERVER_START
;
121 spnego_state
->sub_sec_security
= NULL
;
122 spnego_state
->sub_sec_ready
= false;
123 spnego_state
->mech_types
= data_blob_null
;
124 spnego_state
->out_max_length
= gensec_max_update_size(gensec_security
);
125 spnego_state
->out_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
127 spnego_state
->simulate_w2k
= gensec_setting_bool(gensec_security
->settings
,
128 "spnego", "simulate_w2k", false);
130 gensec_security
->private_data
= spnego_state
;
134 /** Fallback to another GENSEC mechanism, based on magic strings
136 * This is the 'fallback' case, where we don't get SPNEGO, and have to
137 * try all the other options (and hope they all have a magic string
141 static NTSTATUS
gensec_spnego_server_try_fallback(struct gensec_security
*gensec_security
,
142 struct spnego_state
*spnego_state
,
143 struct tevent_context
*ev
,
144 TALLOC_CTX
*out_mem_ctx
,
145 const DATA_BLOB in
, DATA_BLOB
*out
)
148 const struct gensec_security_ops
**all_ops
;
150 all_ops
= gensec_security_mechs(gensec_security
, out_mem_ctx
);
152 for (i
=0; all_ops
&& all_ops
[i
]; i
++) {
156 if (gensec_security
!= NULL
&&
157 !gensec_security_ops_enabled(all_ops
[i
], gensec_security
))
160 if (!all_ops
[i
]->oid
) {
165 for (j
=0; all_ops
[i
]->oid
[j
]; j
++) {
166 if (strcasecmp(GENSEC_OID_SPNEGO
,all_ops
[i
]->oid
[j
]) == 0) {
174 if (!all_ops
[i
]->magic
) {
178 nt_status
= all_ops
[i
]->magic(gensec_security
, &in
);
179 if (!NT_STATUS_IS_OK(nt_status
)) {
183 spnego_state
->state_position
= SPNEGO_FALLBACK
;
185 nt_status
= gensec_subcontext_start(spnego_state
,
187 &spnego_state
->sub_sec_security
);
189 if (!NT_STATUS_IS_OK(nt_status
)) {
192 /* select the sub context */
193 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
195 if (!NT_STATUS_IS_OK(nt_status
)) {
198 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
199 out_mem_ctx
, ev
, in
, out
);
202 DEBUG(1, ("Failed to parse SPNEGO request\n"));
203 return NT_STATUS_INVALID_PARAMETER
;
207 Parse the netTokenInit, either from the client, to the server, or
208 from the server to the client.
211 static NTSTATUS
gensec_spnego_parse_negTokenInit(struct gensec_security
*gensec_security
,
212 struct spnego_state
*spnego_state
,
213 TALLOC_CTX
*out_mem_ctx
,
214 struct tevent_context
*ev
,
215 const char * const *mechType
,
216 const DATA_BLOB unwrapped_in
, DATA_BLOB
*unwrapped_out
)
219 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
222 const struct gensec_security_ops_wrapper
*all_sec
223 = gensec_security_by_oid_list(gensec_security
,
228 ok
= spnego_write_mech_types(spnego_state
,
230 &spnego_state
->mech_types
);
232 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
233 return NT_STATUS_NO_MEMORY
;
236 if (spnego_state
->state_position
== SPNEGO_SERVER_START
) {
238 for (j
=0; mechType
&& mechType
[j
]; j
++) {
239 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
240 if (strcmp(mechType
[j
], all_sec
[i
].oid
) != 0) {
244 nt_status
= gensec_subcontext_start(spnego_state
,
246 &spnego_state
->sub_sec_security
);
247 if (!NT_STATUS_IS_OK(nt_status
)) {
250 /* select the sub context */
251 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
253 if (!NT_STATUS_IS_OK(nt_status
)) {
255 * Pretend we never started it
257 gensec_spnego_update_sub_abort(spnego_state
);
262 /* no optimistic token */
263 spnego_state
->neg_oid
= all_sec
[i
].oid
;
264 *unwrapped_out
= data_blob_null
;
265 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
267 * Indicate the downgrade and request a
270 spnego_state
->downgraded
= true;
271 spnego_state
->mic_requested
= true;
275 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
280 if (NT_STATUS_IS_OK(nt_status
)) {
281 spnego_state
->sub_sec_ready
= true;
283 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
) ||
284 NT_STATUS_EQUAL(nt_status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
286 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
287 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
290 * Pretend we never started it
292 gensec_spnego_update_sub_abort(spnego_state
);
296 spnego_state
->neg_oid
= all_sec
[i
].oid
;
299 if (spnego_state
->sub_sec_security
) {
304 if (!spnego_state
->sub_sec_security
) {
305 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
306 return NT_STATUS_INVALID_PARAMETER
;
310 /* Having tried any optimistic token from the client (if we
311 * were the server), if we didn't get anywhere, walk our list
312 * in our preference order */
314 if (!spnego_state
->sub_sec_security
) {
315 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
316 nt_status
= gensec_subcontext_start(spnego_state
,
318 &spnego_state
->sub_sec_security
);
319 if (!NT_STATUS_IS_OK(nt_status
)) {
322 /* select the sub context */
323 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
325 if (!NT_STATUS_IS_OK(nt_status
)) {
327 * Pretend we never started it.
329 gensec_spnego_update_sub_abort(spnego_state
);
333 spnego_state
->neg_oid
= all_sec
[i
].oid
;
335 /* only get the helping start blob for the first OID */
336 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
341 if (NT_STATUS_IS_OK(nt_status
)) {
342 spnego_state
->sub_sec_ready
= true;
345 /* it is likely that a NULL input token will
346 * not be liked by most server mechs, but if
347 * we are in the client, we want the first
348 * update packet to be able to abort the use
350 if (spnego_state
->state_position
!= SPNEGO_SERVER_START
) {
351 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
) ||
352 NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_LOGON_SERVERS
) ||
353 NT_STATUS_EQUAL(nt_status
, NT_STATUS_TIME_DIFFERENCE_AT_DC
) ||
354 NT_STATUS_EQUAL(nt_status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
355 const char *next
= NULL
;
356 const char *principal
= NULL
;
357 int dbg_level
= DBGLVL_WARNING
;
359 if (all_sec
[i
+1].op
!= NULL
) {
360 next
= all_sec
[i
+1].op
->name
;
361 dbg_level
= DBGLVL_NOTICE
;
364 if (gensec_security
->target
.principal
!= NULL
) {
365 principal
= gensec_security
->target
.principal
;
366 } else if (gensec_security
->target
.service
!= NULL
&&
367 gensec_security
->target
.hostname
!= NULL
)
369 principal
= talloc_asprintf(spnego_state
->sub_sec_security
,
371 gensec_security
->target
.service
,
372 gensec_security
->target
.hostname
);
374 principal
= gensec_security
->target
.hostname
;
377 DEBUG(dbg_level
, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
378 spnego_state
->sub_sec_security
->ops
->name
,
380 next
, nt_errstr(nt_status
)));
383 * Pretend we never started it.
385 gensec_spnego_update_sub_abort(spnego_state
);
394 if (spnego_state
->sub_sec_security
) {
395 /* it is likely that a NULL input token will
396 * not be liked by most server mechs, but this
397 * does the right thing in the CIFS client.
398 * just push us along the merry-go-round
399 * again, and hope for better luck next
402 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
)) {
403 *unwrapped_out
= data_blob_null
;
404 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
407 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
)
408 && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
409 && !NT_STATUS_IS_OK(nt_status
)) {
410 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
411 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
413 /* We started the mech correctly, and the
414 * input from the other side was valid.
415 * Return the error (say bad password, invalid
417 gensec_spnego_update_sub_abort(spnego_state
);
421 return nt_status
; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
424 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
425 /* we could re-negotiate here, but it would only work
426 * if the client or server lied about what it could
427 * support the first time. Lets keep this code to
433 /** create a negTokenInit
435 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
437 static NTSTATUS
gensec_spnego_create_negTokenInit(struct gensec_security
*gensec_security
,
438 struct spnego_state
*spnego_state
,
439 TALLOC_CTX
*out_mem_ctx
,
440 struct tevent_context
*ev
,
441 const DATA_BLOB in
, DATA_BLOB
*out
)
444 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
445 const char **mechTypes
= NULL
;
446 DATA_BLOB unwrapped_out
= data_blob_null
;
447 const struct gensec_security_ops_wrapper
*all_sec
;
449 mechTypes
= gensec_security_oids(gensec_security
,
450 out_mem_ctx
, GENSEC_OID_SPNEGO
);
452 all_sec
= gensec_security_by_oid_list(gensec_security
,
456 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
457 struct spnego_data spnego_out
;
458 const char **send_mech_types
;
461 nt_status
= gensec_subcontext_start(spnego_state
,
463 &spnego_state
->sub_sec_security
);
464 if (!NT_STATUS_IS_OK(nt_status
)) {
467 /* select the sub context */
468 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
470 if (!NT_STATUS_IS_OK(nt_status
)) {
471 gensec_spnego_update_sub_abort(spnego_state
);
475 /* In the client, try and produce the first (optimistic) packet */
476 if (spnego_state
->state_position
== SPNEGO_CLIENT_START
) {
477 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
482 if (NT_STATUS_IS_OK(nt_status
)) {
483 spnego_state
->sub_sec_ready
= true;
486 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
487 && !NT_STATUS_IS_OK(nt_status
)) {
488 const char *next
= NULL
;
489 const char *principal
= NULL
;
490 int dbg_level
= DBGLVL_WARNING
;
492 if (all_sec
[i
+1].op
!= NULL
) {
493 next
= all_sec
[i
+1].op
->name
;
494 dbg_level
= DBGLVL_NOTICE
;
497 if (gensec_security
->target
.principal
!= NULL
) {
498 principal
= gensec_security
->target
.principal
;
499 } else if (gensec_security
->target
.service
!= NULL
&&
500 gensec_security
->target
.hostname
!= NULL
)
502 principal
= talloc_asprintf(spnego_state
->sub_sec_security
,
504 gensec_security
->target
.service
,
505 gensec_security
->target
.hostname
);
507 principal
= gensec_security
->target
.hostname
;
510 DEBUG(dbg_level
, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
511 spnego_state
->sub_sec_security
->ops
->name
,
513 next
, nt_errstr(nt_status
)));
516 * Pretend we never started it
518 gensec_spnego_update_sub_abort(spnego_state
);
523 spnego_out
.type
= SPNEGO_NEG_TOKEN_INIT
;
525 send_mech_types
= gensec_security_oids_from_ops_wrapped(out_mem_ctx
,
528 ok
= spnego_write_mech_types(spnego_state
,
530 &spnego_state
->mech_types
);
532 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
533 return NT_STATUS_NO_MEMORY
;
536 /* List the remaining mechs as options */
537 spnego_out
.negTokenInit
.mechTypes
= send_mech_types
;
538 spnego_out
.negTokenInit
.reqFlags
= data_blob_null
;
539 spnego_out
.negTokenInit
.reqFlagsPadding
= 0;
541 if (spnego_state
->state_position
== SPNEGO_SERVER_START
) {
542 spnego_out
.negTokenInit
.mechListMIC
543 = data_blob_string_const(ADS_IGNORE_PRINCIPAL
);
545 spnego_out
.negTokenInit
.mechListMIC
= data_blob_null
;
548 spnego_out
.negTokenInit
.mechToken
= unwrapped_out
;
550 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
551 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
552 return NT_STATUS_INVALID_PARAMETER
;
556 spnego_state
->neg_oid
= all_sec
[i
].oid
;
558 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
560 gensec_spnego_update_sub_abort(spnego_state
);
562 DEBUG(10, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status
)));
567 /** create a server negTokenTarg
569 * This is the case, where the client is the first one who sends data
572 static NTSTATUS
gensec_spnego_server_negTokenTarg(struct spnego_state
*spnego_state
,
573 TALLOC_CTX
*out_mem_ctx
,
575 const DATA_BLOB unwrapped_out
,
576 DATA_BLOB mech_list_mic
,
579 struct spnego_data spnego_out
;
582 spnego_out
.type
= SPNEGO_NEG_TOKEN_TARG
;
583 spnego_out
.negTokenTarg
.responseToken
= unwrapped_out
;
584 spnego_out
.negTokenTarg
.mechListMIC
= mech_list_mic
;
585 spnego_out
.negTokenTarg
.supportedMech
= NULL
;
587 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
588 spnego_out
.negTokenTarg
.supportedMech
= spnego_state
->neg_oid
;
589 if (spnego_state
->mic_requested
) {
590 spnego_out
.negTokenTarg
.negResult
= SPNEGO_REQUEST_MIC
;
591 spnego_state
->mic_requested
= false;
593 spnego_out
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
595 spnego_state
->state_position
= SPNEGO_SERVER_TARG
;
596 } else if (NT_STATUS_IS_OK(nt_status
)) {
597 if (unwrapped_out
.data
) {
598 spnego_out
.negTokenTarg
.supportedMech
= spnego_state
->neg_oid
;
600 spnego_out
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
601 spnego_state
->state_position
= SPNEGO_DONE
;
603 spnego_out
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
604 spnego_out
.negTokenTarg
.mechListMIC
= data_blob_null
;
605 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status
)));
606 spnego_state
->state_position
= SPNEGO_DONE
;
609 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
610 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
611 return NT_STATUS_INVALID_PARAMETER
;
614 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
615 spnego_state
->num_targs
++;
620 static NTSTATUS
gensec_spnego_update_client(struct gensec_security
*gensec_security
,
621 TALLOC_CTX
*out_mem_ctx
,
622 struct tevent_context
*ev
,
623 const DATA_BLOB in
, DATA_BLOB
*out
)
625 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
626 DATA_BLOB mech_list_mic
= data_blob_null
;
627 DATA_BLOB unwrapped_out
= data_blob_null
;
628 struct spnego_data spnego_out
;
629 struct spnego_data spnego
;
632 *out
= data_blob_null
;
634 /* and switch into the state machine */
636 switch (spnego_state
->state_position
) {
637 case SPNEGO_CLIENT_START
:
639 /* The server offers a list of mechanisms */
641 const char *my_mechs
[] = {NULL
, NULL
};
642 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
646 /* client to produce negTokenInit */
647 nt_status
= gensec_spnego_create_negTokenInit(gensec_security
, spnego_state
,
648 out_mem_ctx
, ev
, in
, out
);
649 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
650 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
654 len
= spnego_read_data(gensec_security
, in
, &spnego
);
657 DEBUG(1, ("Invalid SPNEGO request:\n"));
658 dump_data(1, in
.data
, in
.length
);
659 return NT_STATUS_INVALID_PARAMETER
;
662 /* OK, so it's real SPNEGO, check the packet's the one we expect */
663 if (spnego
.type
!= spnego_state
->expected_packet
) {
664 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
665 spnego_state
->expected_packet
));
666 dump_data(1, in
.data
, in
.length
);
667 spnego_free_data(&spnego
);
668 return NT_STATUS_INVALID_PARAMETER
;
671 if (spnego
.negTokenInit
.targetPrincipal
672 && strcmp(spnego
.negTokenInit
.targetPrincipal
, ADS_IGNORE_PRINCIPAL
) != 0) {
673 DEBUG(5, ("Server claims it's principal name is %s\n", spnego
.negTokenInit
.targetPrincipal
));
674 if (lpcfg_client_use_spnego_principal(gensec_security
->settings
->lp_ctx
)) {
675 gensec_set_target_principal(gensec_security
, spnego
.negTokenInit
.targetPrincipal
);
679 nt_status
= gensec_spnego_parse_negTokenInit(gensec_security
,
683 spnego
.negTokenInit
.mechTypes
,
684 spnego
.negTokenInit
.mechToken
,
687 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(nt_status
)) {
688 spnego_free_data(&spnego
);
692 my_mechs
[0] = spnego_state
->neg_oid
;
694 spnego_out
.type
= SPNEGO_NEG_TOKEN_INIT
;
695 spnego_out
.negTokenInit
.mechTypes
= my_mechs
;
696 spnego_out
.negTokenInit
.reqFlags
= data_blob_null
;
697 spnego_out
.negTokenInit
.reqFlagsPadding
= 0;
698 spnego_out
.negTokenInit
.mechListMIC
= data_blob_null
;
699 spnego_out
.negTokenInit
.mechToken
= unwrapped_out
;
701 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
702 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
703 return NT_STATUS_INVALID_PARAMETER
;
706 ok
= spnego_write_mech_types(spnego_state
,
708 &spnego_state
->mech_types
);
710 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
711 return NT_STATUS_NO_MEMORY
;
715 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
716 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
718 spnego_free_data(&spnego
);
719 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
722 case SPNEGO_CLIENT_TARG
:
724 NTSTATUS nt_status
= NT_STATUS_INTERNAL_ERROR
;
727 return NT_STATUS_INVALID_PARAMETER
;
730 len
= spnego_read_data(gensec_security
, in
, &spnego
);
733 DEBUG(1, ("Invalid SPNEGO request:\n"));
734 dump_data(1, in
.data
, in
.length
);
735 return NT_STATUS_INVALID_PARAMETER
;
738 /* OK, so it's real SPNEGO, check the packet's the one we expect */
739 if (spnego
.type
!= spnego_state
->expected_packet
) {
740 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
741 spnego_state
->expected_packet
));
742 dump_data(1, in
.data
, in
.length
);
743 spnego_free_data(&spnego
);
744 return NT_STATUS_INVALID_PARAMETER
;
747 spnego_state
->num_targs
++;
749 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
750 spnego_free_data(&spnego
);
751 return NT_STATUS_LOGON_FAILURE
;
754 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REQUEST_MIC
) {
755 spnego_state
->mic_requested
= true;
758 /* Server didn't like our choice of mech, and chose something else */
759 if (((spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_INCOMPLETE
) ||
760 (spnego
.negTokenTarg
.negResult
== SPNEGO_REQUEST_MIC
)) &&
761 spnego
.negTokenTarg
.supportedMech
&&
762 strcmp(spnego
.negTokenTarg
.supportedMech
, spnego_state
->neg_oid
) != 0) {
763 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
764 gensec_get_name_by_oid(gensec_security
, spnego_state
->neg_oid
),
765 gensec_get_name_by_oid(gensec_security
, spnego
.negTokenTarg
.supportedMech
)));
766 spnego_state
->downgraded
= true;
767 gensec_spnego_update_sub_abort(spnego_state
);
768 nt_status
= gensec_subcontext_start(spnego_state
,
770 &spnego_state
->sub_sec_security
);
771 if (!NT_STATUS_IS_OK(nt_status
)) {
772 spnego_free_data(&spnego
);
775 /* select the sub context */
776 nt_status
= gensec_start_mech_by_oid(spnego_state
->sub_sec_security
,
777 spnego
.negTokenTarg
.supportedMech
);
778 if (!NT_STATUS_IS_OK(nt_status
)) {
779 spnego_free_data(&spnego
);
783 spnego_state
->neg_oid
= talloc_strdup(spnego_state
,
784 spnego
.negTokenTarg
.supportedMech
);
785 if (spnego_state
->neg_oid
== NULL
) {
786 spnego_free_data(&spnego
);
787 return NT_STATUS_NO_MEMORY
;
791 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
792 DATA_BLOB
*m
= &spnego
.negTokenTarg
.mechListMIC
;
793 const DATA_BLOB
*r
= &spnego
.negTokenTarg
.responseToken
;
796 * Windows 2000 has a bug, it repeats the
797 * responseToken in the mechListMIC field.
799 if (m
->length
== r
->length
) {
802 cmp
= memcmp(m
->data
, r
->data
, m
->length
);
809 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
810 if (spnego_state
->sub_sec_ready
) {
811 spnego_state
->needs_mic_check
= true;
815 if (spnego_state
->needs_mic_check
) {
816 if (spnego
.negTokenTarg
.responseToken
.length
!= 0) {
817 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
818 spnego_free_data(&spnego
);
819 return NT_STATUS_INVALID_PARAMETER
;
822 if (spnego
.negTokenTarg
.mechListMIC
.length
== 0
823 && spnego_state
->may_skip_mic_check
) {
825 * In this case we don't require
826 * a mechListMIC from the server.
828 * This works around bugs in the Azure
829 * and Apple spnego implementations.
832 * https://bugzilla.samba.org/show_bug.cgi?id=11994
834 spnego_state
->needs_mic_check
= false;
835 nt_status
= NT_STATUS_OK
;
836 goto client_response
;
839 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
840 spnego_state
->mech_types
.data
,
841 spnego_state
->mech_types
.length
,
842 spnego_state
->mech_types
.data
,
843 spnego_state
->mech_types
.length
,
844 &spnego
.negTokenTarg
.mechListMIC
);
845 if (!NT_STATUS_IS_OK(nt_status
)) {
846 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
847 nt_errstr(nt_status
)));
848 spnego_free_data(&spnego
);
851 spnego_state
->needs_mic_check
= false;
852 spnego_state
->done_mic_check
= true;
853 goto client_response
;
856 if (!spnego_state
->sub_sec_ready
) {
857 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
859 spnego
.negTokenTarg
.responseToken
,
861 if (NT_STATUS_IS_OK(nt_status
)) {
862 spnego_state
->sub_sec_ready
= true;
864 if (!NT_STATUS_IS_OK(nt_status
)) {
865 goto client_response
;
868 nt_status
= NT_STATUS_OK
;
871 if (!spnego_state
->done_mic_check
) {
872 bool have_sign
= true;
873 bool new_spnego
= false;
875 have_sign
= gensec_have_feature(spnego_state
->sub_sec_security
,
876 GENSEC_FEATURE_SIGN
);
877 if (spnego_state
->simulate_w2k
) {
880 new_spnego
= gensec_have_feature(spnego_state
->sub_sec_security
,
881 GENSEC_FEATURE_NEW_SPNEGO
);
883 switch (spnego
.negTokenTarg
.negResult
) {
884 case SPNEGO_ACCEPT_COMPLETED
:
885 case SPNEGO_NONE_RESULT
:
886 if (spnego_state
->num_targs
== 1) {
888 * the first exchange doesn't require
896 case SPNEGO_ACCEPT_INCOMPLETE
:
897 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
902 if (spnego_state
->downgraded
) {
904 * A downgrade should be protected if
911 * The caller may just asked for
912 * GENSEC_FEATURE_SESSION_KEY, this
913 * is only reflected in the want_features.
916 * gensec_have_features(GENSEC_FEATURE_SIGN)
919 if (gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
922 if (gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
926 * Here we're sure our preferred mech was
927 * selected by the server and our caller doesn't
928 * need GENSEC_FEATURE_SIGN nor
929 * GENSEC_FEATURE_SEAL support.
931 * In this case we don't require
932 * a mechListMIC from the server.
934 * This works around bugs in the Azure
935 * and Apple spnego implementations.
938 * https://bugzilla.samba.org/show_bug.cgi?id=11994
940 spnego_state
->may_skip_mic_check
= true;
943 case SPNEGO_REQUEST_MIC
:
944 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
952 if (spnego_state
->mic_requested
) {
958 if (have_sign
&& new_spnego
) {
959 spnego_state
->needs_mic_check
= true;
960 spnego_state
->needs_mic_sign
= true;
964 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
965 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
966 spnego_state
->mech_types
.data
,
967 spnego_state
->mech_types
.length
,
968 spnego_state
->mech_types
.data
,
969 spnego_state
->mech_types
.length
,
970 &spnego
.negTokenTarg
.mechListMIC
);
971 if (!NT_STATUS_IS_OK(nt_status
)) {
972 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
973 nt_errstr(nt_status
)));
974 spnego_free_data(&spnego
);
977 spnego_state
->needs_mic_check
= false;
978 spnego_state
->done_mic_check
= true;
981 if (spnego_state
->needs_mic_sign
) {
982 nt_status
= gensec_sign_packet(spnego_state
->sub_sec_security
,
984 spnego_state
->mech_types
.data
,
985 spnego_state
->mech_types
.length
,
986 spnego_state
->mech_types
.data
,
987 spnego_state
->mech_types
.length
,
989 if (!NT_STATUS_IS_OK(nt_status
)) {
990 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
991 nt_errstr(nt_status
)));
992 spnego_free_data(&spnego
);
995 spnego_state
->needs_mic_sign
= false;
998 if (spnego_state
->needs_mic_check
) {
999 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1003 spnego_free_data(&spnego
);
1005 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
1006 && !NT_STATUS_IS_OK(nt_status
)) {
1007 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1008 spnego_state
->sub_sec_security
->ops
->name
,
1009 nt_errstr(nt_status
)));
1013 if (unwrapped_out
.length
|| mech_list_mic
.length
) {
1015 spnego_out
.type
= SPNEGO_NEG_TOKEN_TARG
;
1016 spnego_out
.negTokenTarg
.negResult
= SPNEGO_NONE_RESULT
;
1017 spnego_out
.negTokenTarg
.supportedMech
= NULL
;
1018 spnego_out
.negTokenTarg
.responseToken
= unwrapped_out
;
1019 spnego_out
.negTokenTarg
.mechListMIC
= mech_list_mic
;
1021 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
1022 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1023 return NT_STATUS_INVALID_PARAMETER
;
1026 spnego_state
->num_targs
++;
1027 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
1028 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1031 /* all done - server has accepted, and we agree */
1032 *out
= data_blob_null
;
1034 if (spnego
.negTokenTarg
.negResult
!= SPNEGO_ACCEPT_COMPLETED
) {
1035 /* unless of course it did not accept */
1036 DEBUG(1,("gensec_update ok but not accepted\n"));
1037 nt_status
= NT_STATUS_INVALID_PARAMETER
;
1040 spnego_state
->state_position
= SPNEGO_DONE
;
1050 smb_panic(__location__
);
1051 return NT_STATUS_INTERNAL_ERROR
;
1054 static NTSTATUS
gensec_spnego_update_server(struct gensec_security
*gensec_security
,
1055 TALLOC_CTX
*out_mem_ctx
,
1056 struct tevent_context
*ev
,
1057 const DATA_BLOB in
, DATA_BLOB
*out
)
1059 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1060 DATA_BLOB mech_list_mic
= data_blob_null
;
1061 DATA_BLOB unwrapped_out
= data_blob_null
;
1062 struct spnego_data spnego
;
1065 /* and switch into the state machine */
1067 switch (spnego_state
->state_position
) {
1068 case SPNEGO_SERVER_START
:
1073 len
= spnego_read_data(gensec_security
, in
, &spnego
);
1075 return gensec_spnego_server_try_fallback(gensec_security
, spnego_state
,
1076 ev
, out_mem_ctx
, in
, out
);
1078 /* client sent NegTargetInit, we send NegTokenTarg */
1080 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1081 if (spnego
.type
!= spnego_state
->expected_packet
) {
1082 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
1083 spnego_state
->expected_packet
));
1084 dump_data(1, in
.data
, in
.length
);
1085 spnego_free_data(&spnego
);
1086 return NT_STATUS_INVALID_PARAMETER
;
1089 nt_status
= gensec_spnego_parse_negTokenInit(gensec_security
,
1093 spnego
.negTokenInit
.mechTypes
,
1094 spnego
.negTokenInit
.mechToken
,
1097 if (spnego_state
->simulate_w2k
) {
1099 * Windows 2000 returns the unwrapped token
1100 * also in the mech_list_mic field.
1102 * In order to verify our client code,
1103 * we need a way to have a server with this
1106 mech_list_mic
= unwrapped_out
;
1109 nt_status
= gensec_spnego_server_negTokenTarg(spnego_state
,
1116 spnego_free_data(&spnego
);
1120 nt_status
= gensec_spnego_create_negTokenInit(gensec_security
, spnego_state
,
1121 out_mem_ctx
, ev
, in
, out
);
1122 spnego_state
->state_position
= SPNEGO_SERVER_START
;
1123 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
1128 case SPNEGO_SERVER_TARG
:
1131 bool have_sign
= true;
1132 bool new_spnego
= false;
1135 return NT_STATUS_INVALID_PARAMETER
;
1138 len
= spnego_read_data(gensec_security
, in
, &spnego
);
1141 DEBUG(1, ("Invalid SPNEGO request:\n"));
1142 dump_data(1, in
.data
, in
.length
);
1143 return NT_STATUS_INVALID_PARAMETER
;
1146 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1147 if (spnego
.type
!= spnego_state
->expected_packet
) {
1148 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
1149 spnego_state
->expected_packet
));
1150 dump_data(1, in
.data
, in
.length
);
1151 spnego_free_data(&spnego
);
1152 return NT_STATUS_INVALID_PARAMETER
;
1155 spnego_state
->num_targs
++;
1157 if (!spnego_state
->sub_sec_security
) {
1158 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1159 spnego_free_data(&spnego
);
1160 return NT_STATUS_INVALID_PARAMETER
;
1163 if (spnego_state
->needs_mic_check
) {
1164 if (spnego
.negTokenTarg
.responseToken
.length
!= 0) {
1165 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1166 spnego_free_data(&spnego
);
1167 return NT_STATUS_INVALID_PARAMETER
;
1170 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
1171 spnego_state
->mech_types
.data
,
1172 spnego_state
->mech_types
.length
,
1173 spnego_state
->mech_types
.data
,
1174 spnego_state
->mech_types
.length
,
1175 &spnego
.negTokenTarg
.mechListMIC
);
1176 if (NT_STATUS_IS_OK(nt_status
)) {
1177 spnego_state
->needs_mic_check
= false;
1178 spnego_state
->done_mic_check
= true;
1180 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1181 nt_errstr(nt_status
)));
1183 goto server_response
;
1186 nt_status
= gensec_update_ev(spnego_state
->sub_sec_security
,
1188 spnego
.negTokenTarg
.responseToken
,
1190 if (NT_STATUS_IS_OK(nt_status
)) {
1191 spnego_state
->sub_sec_ready
= true;
1193 if (!NT_STATUS_IS_OK(nt_status
)) {
1194 goto server_response
;
1197 have_sign
= gensec_have_feature(spnego_state
->sub_sec_security
,
1198 GENSEC_FEATURE_SIGN
);
1199 if (spnego_state
->simulate_w2k
) {
1202 new_spnego
= gensec_have_feature(spnego_state
->sub_sec_security
,
1203 GENSEC_FEATURE_NEW_SPNEGO
);
1204 if (spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
1208 if (have_sign
&& new_spnego
) {
1209 spnego_state
->needs_mic_check
= true;
1210 spnego_state
->needs_mic_sign
= true;
1213 if (have_sign
&& spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
1214 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
1215 spnego_state
->mech_types
.data
,
1216 spnego_state
->mech_types
.length
,
1217 spnego_state
->mech_types
.data
,
1218 spnego_state
->mech_types
.length
,
1219 &spnego
.negTokenTarg
.mechListMIC
);
1220 if (!NT_STATUS_IS_OK(nt_status
)) {
1221 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1222 nt_errstr(nt_status
)));
1223 goto server_response
;
1226 spnego_state
->needs_mic_check
= false;
1227 spnego_state
->done_mic_check
= true;
1230 if (spnego_state
->needs_mic_sign
) {
1231 nt_status
= gensec_sign_packet(spnego_state
->sub_sec_security
,
1233 spnego_state
->mech_types
.data
,
1234 spnego_state
->mech_types
.length
,
1235 spnego_state
->mech_types
.data
,
1236 spnego_state
->mech_types
.length
,
1238 if (!NT_STATUS_IS_OK(nt_status
)) {
1239 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1240 nt_errstr(nt_status
)));
1241 goto server_response
;
1243 spnego_state
->needs_mic_sign
= false;
1246 if (spnego_state
->needs_mic_check
) {
1247 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1251 nt_status
= gensec_spnego_server_negTokenTarg(spnego_state
,
1258 spnego_free_data(&spnego
);
1267 smb_panic(__location__
);
1268 return NT_STATUS_INTERNAL_ERROR
;
1272 static NTSTATUS
gensec_spnego_update(struct gensec_security
*gensec_security
, TALLOC_CTX
*out_mem_ctx
,
1273 struct tevent_context
*ev
,
1274 const DATA_BLOB in
, DATA_BLOB
*out
)
1276 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1278 *out
= data_blob_null
;
1280 /* and switch into the state machine */
1282 switch (spnego_state
->state_position
) {
1283 case SPNEGO_FALLBACK
:
1284 return gensec_update_ev(spnego_state
->sub_sec_security
,
1285 out_mem_ctx
, ev
, in
, out
);
1287 case SPNEGO_CLIENT_START
:
1288 case SPNEGO_CLIENT_TARG
:
1289 return gensec_spnego_update_client(gensec_security
, out_mem_ctx
,
1292 case SPNEGO_SERVER_START
:
1293 case SPNEGO_SERVER_TARG
:
1294 return gensec_spnego_update_server(gensec_security
, out_mem_ctx
,
1298 /* We should not be called after we are 'done' */
1299 return NT_STATUS_INVALID_PARAMETER
;
1301 return NT_STATUS_INVALID_PARAMETER
;
1304 struct gensec_spnego_update_state
{
1305 struct gensec_security
*gensec
;
1306 struct spnego_state
*spnego
;
1312 static void gensec_spnego_update_cleanup(struct tevent_req
*req
,
1313 enum tevent_req_state req_state
)
1315 struct gensec_spnego_update_state
*state
=
1316 tevent_req_data(req
,
1317 struct gensec_spnego_update_state
);
1319 switch (req_state
) {
1320 case TEVENT_REQ_USER_ERROR
:
1321 case TEVENT_REQ_TIMED_OUT
:
1322 case TEVENT_REQ_NO_MEMORY
:
1324 * A fatal error, further updates are not allowed.
1326 state
->spnego
->state_position
= SPNEGO_DONE
;
1333 static NTSTATUS
gensec_spnego_update_in(struct gensec_security
*gensec_security
,
1334 const DATA_BLOB in
, TALLOC_CTX
*mem_ctx
,
1335 DATA_BLOB
*full_in
);
1336 static NTSTATUS
gensec_spnego_update_out(struct gensec_security
*gensec_security
,
1337 TALLOC_CTX
*out_mem_ctx
,
1340 static struct tevent_req
*gensec_spnego_update_send(TALLOC_CTX
*mem_ctx
,
1341 struct tevent_context
*ev
,
1342 struct gensec_security
*gensec_security
,
1345 struct spnego_state
*spnego_state
=
1346 talloc_get_type_abort(gensec_security
->private_data
,
1347 struct spnego_state
);
1348 struct tevent_req
*req
= NULL
;
1349 struct gensec_spnego_update_state
*state
= NULL
;
1352 req
= tevent_req_create(mem_ctx
, &state
,
1353 struct gensec_spnego_update_state
);
1357 state
->gensec
= gensec_security
;
1358 state
->spnego
= spnego_state
;
1359 tevent_req_set_cleanup_fn(req
, gensec_spnego_update_cleanup
);
1361 if (spnego_state
->out_frag
.length
> 0) {
1362 if (in
.length
> 0) {
1363 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1364 return tevent_req_post(req
, ev
);
1367 status
= gensec_spnego_update_out(gensec_security
,
1368 state
, &state
->out
);
1369 state
->status
= status
;
1370 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1371 tevent_req_done(req
);
1372 return tevent_req_post(req
, ev
);
1374 if (tevent_req_nterror(req
, status
)) {
1375 return tevent_req_post(req
, ev
);
1378 tevent_req_done(req
);
1379 return tevent_req_post(req
, ev
);
1382 status
= gensec_spnego_update_in(gensec_security
, in
,
1383 state
, &state
->full_in
);
1384 state
->status
= status
;
1385 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1386 tevent_req_done(req
);
1387 return tevent_req_post(req
, ev
);
1389 if (tevent_req_nterror(req
, status
)) {
1390 return tevent_req_post(req
, ev
);
1393 status
= gensec_spnego_update(gensec_security
,
1396 &spnego_state
->out_frag
);
1397 if (NT_STATUS_IS_OK(status
)) {
1398 bool reset_full
= true;
1400 reset_full
= !spnego_state
->done_mic_check
;
1402 status
= gensec_may_reset_crypto(spnego_state
->sub_sec_security
,
1405 if (!NT_STATUS_IS_OK(status
) &&
1406 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1407 tevent_req_nterror(req
, status
);
1408 return tevent_req_post(req
, ev
);
1411 spnego_state
->out_status
= status
;
1413 status
= gensec_spnego_update_out(gensec_security
,
1414 state
, &state
->out
);
1415 state
->status
= status
;
1416 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1417 tevent_req_done(req
);
1418 return tevent_req_post(req
, ev
);
1420 if (tevent_req_nterror(req
, status
)) {
1421 return tevent_req_post(req
, ev
);
1424 tevent_req_done(req
);
1425 return tevent_req_post(req
, ev
);
1428 static NTSTATUS
gensec_spnego_update_in(struct gensec_security
*gensec_security
,
1429 const DATA_BLOB in
, TALLOC_CTX
*mem_ctx
,
1432 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1436 *full_in
= data_blob_null
;
1438 if (spnego_state
->in_needed
== 0) {
1443 * try to work out the size of the full
1444 * input token, it might be fragmented
1446 ret
= asn1_peek_full_tag(in
, ASN1_APPLICATION(0), &size
);
1447 if ((ret
!= 0) && (ret
!= EAGAIN
)) {
1448 ret
= asn1_peek_full_tag(in
, ASN1_CONTEXT(1), &size
);
1451 if ((ret
== 0) || (ret
== EAGAIN
)) {
1452 spnego_state
->in_needed
= size
;
1455 * If it is not an asn1 message
1456 * just call the next layer.
1458 spnego_state
->in_needed
= in
.length
;
1462 if (spnego_state
->in_needed
> UINT16_MAX
) {
1464 * limit the incoming message to 0xFFFF
1465 * to avoid DoS attacks.
1467 return NT_STATUS_INVALID_BUFFER_SIZE
;
1470 if ((spnego_state
->in_needed
> 0) && (in
.length
== 0)) {
1472 * If we reach this, we know we got at least
1473 * part of an asn1 message, getting 0 means
1474 * the remote peer wants us to spin.
1476 return NT_STATUS_INVALID_PARAMETER
;
1479 expected
= spnego_state
->in_needed
- spnego_state
->in_frag
.length
;
1480 if (in
.length
> expected
) {
1482 * we got more than expected
1484 return NT_STATUS_INVALID_PARAMETER
;
1487 if (in
.length
== spnego_state
->in_needed
) {
1489 * if the in.length contains the full blob
1492 * Note: this implies spnego_state->in_frag.length == 0,
1493 * but we do not need to check this explicitly
1494 * because we already know that we did not get
1495 * more than expected.
1498 spnego_state
->in_needed
= 0;
1499 return NT_STATUS_OK
;
1502 ok
= data_blob_append(spnego_state
, &spnego_state
->in_frag
,
1503 in
.data
, in
.length
);
1505 return NT_STATUS_NO_MEMORY
;
1508 if (spnego_state
->in_needed
> spnego_state
->in_frag
.length
) {
1509 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
1512 *full_in
= spnego_state
->in_frag
;
1513 talloc_steal(mem_ctx
, full_in
->data
);
1514 spnego_state
->in_frag
= data_blob_null
;
1515 spnego_state
->in_needed
= 0;
1516 return NT_STATUS_OK
;
1519 static NTSTATUS
gensec_spnego_update_out(struct gensec_security
*gensec_security
,
1520 TALLOC_CTX
*out_mem_ctx
,
1523 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1524 DATA_BLOB out
= data_blob_null
;
1527 *_out
= data_blob_null
;
1529 if (spnego_state
->out_frag
.length
<= spnego_state
->out_max_length
) {
1531 * Fast path, we can deliver everything
1534 *_out
= spnego_state
->out_frag
;
1535 if (spnego_state
->out_frag
.length
> 0) {
1536 talloc_steal(out_mem_ctx
, _out
->data
);
1537 spnego_state
->out_frag
= data_blob_null
;
1540 if (!NT_STATUS_IS_OK(spnego_state
->out_status
)) {
1541 return spnego_state
->out_status
;
1545 * We're completely done, further updates are not allowed.
1547 spnego_state
->state_position
= SPNEGO_DONE
;
1548 return gensec_child_ready(gensec_security
,
1549 spnego_state
->sub_sec_security
);
1552 out
= spnego_state
->out_frag
;
1555 * copy the remaining bytes
1557 spnego_state
->out_frag
= data_blob_talloc(spnego_state
,
1558 out
.data
+ spnego_state
->out_max_length
,
1559 out
.length
- spnego_state
->out_max_length
);
1560 if (spnego_state
->out_frag
.data
== NULL
) {
1561 return NT_STATUS_NO_MEMORY
;
1565 * truncate the buffer
1567 ok
= data_blob_realloc(spnego_state
, &out
,
1568 spnego_state
->out_max_length
);
1570 return NT_STATUS_NO_MEMORY
;
1573 talloc_steal(out_mem_ctx
, out
.data
);
1575 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
1578 static NTSTATUS
gensec_spnego_update_recv(struct tevent_req
*req
,
1579 TALLOC_CTX
*out_mem_ctx
,
1582 struct gensec_spnego_update_state
*state
=
1583 tevent_req_data(req
,
1584 struct gensec_spnego_update_state
);
1587 *out
= data_blob_null
;
1589 if (tevent_req_is_nterror(req
, &status
)) {
1590 tevent_req_received(req
);
1595 talloc_steal(out_mem_ctx
, state
->out
.data
);
1596 status
= state
->status
;
1597 tevent_req_received(req
);
1601 static const char *gensec_spnego_oids
[] = {
1606 static const struct gensec_security_ops gensec_spnego_security_ops
= {
1608 .sasl_name
= "GSS-SPNEGO",
1609 .auth_type
= DCERPC_AUTH_TYPE_SPNEGO
,
1610 .oid
= gensec_spnego_oids
,
1611 .client_start
= gensec_spnego_client_start
,
1612 .server_start
= gensec_spnego_server_start
,
1613 .update_send
= gensec_spnego_update_send
,
1614 .update_recv
= gensec_spnego_update_recv
,
1615 .seal_packet
= gensec_child_seal_packet
,
1616 .sign_packet
= gensec_child_sign_packet
,
1617 .sig_size
= gensec_child_sig_size
,
1618 .max_wrapped_size
= gensec_child_max_wrapped_size
,
1619 .max_input_size
= gensec_child_max_input_size
,
1620 .check_packet
= gensec_child_check_packet
,
1621 .unseal_packet
= gensec_child_unseal_packet
,
1622 .wrap
= gensec_child_wrap
,
1623 .unwrap
= gensec_child_unwrap
,
1624 .session_key
= gensec_child_session_key
,
1625 .session_info
= gensec_child_session_info
,
1626 .want_feature
= gensec_child_want_feature
,
1627 .have_feature
= gensec_child_have_feature
,
1628 .expire_time
= gensec_child_expire_time
,
1629 .final_auth_type
= gensec_child_final_auth_type
,
1631 .priority
= GENSEC_SPNEGO
1634 _PUBLIC_ NTSTATUS
gensec_spnego_init(TALLOC_CTX
*ctx
)
1637 ret
= gensec_register(ctx
, &gensec_spnego_security_ops
);
1638 if (!NT_STATUS_IS_OK(ret
)) {
1639 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1640 gensec_spnego_security_ops
.name
));