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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "auth/auth.h"
28 enum spnego_state_position {
39 enum spnego_message_type expected_packet;
40 enum spnego_state_position state_position;
41 struct gensec_security *sub_sec_security;
42 BOOL no_response_expected;
48 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
50 struct spnego_state *spnego_state;
52 spnego_state = talloc(gensec_security, struct spnego_state);
54 return NT_STATUS_NO_MEMORY;
57 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
58 spnego_state->state_position = SPNEGO_CLIENT_START;
59 spnego_state->sub_sec_security = NULL;
60 spnego_state->no_response_expected = False;
62 gensec_security->private_data = spnego_state;
66 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
68 struct spnego_state *spnego_state;
70 spnego_state = talloc(gensec_security, struct spnego_state);
72 return NT_STATUS_NO_MEMORY;
75 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
76 spnego_state->state_position = SPNEGO_SERVER_START;
77 spnego_state->sub_sec_security = NULL;
78 spnego_state->no_response_expected = False;
80 gensec_security->private_data = spnego_state;
85 wrappers for the spnego_*() functions
87 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security,
89 uint8_t *data, size_t length,
90 const uint8_t *whole_pdu, size_t pdu_length,
93 struct spnego_state *spnego_state = gensec_security->private_data;
95 if (spnego_state->state_position != SPNEGO_DONE
96 && spnego_state->state_position != SPNEGO_FALLBACK) {
97 return NT_STATUS_INVALID_PARAMETER;
100 return gensec_unseal_packet(spnego_state->sub_sec_security,
103 whole_pdu, pdu_length,
107 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security,
109 const uint8_t *data, size_t length,
110 const uint8_t *whole_pdu, size_t pdu_length,
111 const DATA_BLOB *sig)
113 struct spnego_state *spnego_state = gensec_security->private_data;
115 if (spnego_state->state_position != SPNEGO_DONE
116 && spnego_state->state_position != SPNEGO_FALLBACK) {
117 return NT_STATUS_INVALID_PARAMETER;
120 return gensec_check_packet(spnego_state->sub_sec_security,
123 whole_pdu, pdu_length,
127 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security,
129 uint8_t *data, size_t length,
130 const uint8_t *whole_pdu, size_t pdu_length,
133 struct spnego_state *spnego_state = gensec_security->private_data;
135 if (spnego_state->state_position != SPNEGO_DONE
136 && spnego_state->state_position != SPNEGO_FALLBACK) {
137 return NT_STATUS_INVALID_PARAMETER;
140 return gensec_seal_packet(spnego_state->sub_sec_security,
143 whole_pdu, pdu_length,
147 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security,
149 const uint8_t *data, size_t length,
150 const uint8_t *whole_pdu, size_t pdu_length,
153 struct spnego_state *spnego_state = gensec_security->private_data;
155 if (spnego_state->state_position != SPNEGO_DONE
156 && spnego_state->state_position != SPNEGO_FALLBACK) {
157 return NT_STATUS_INVALID_PARAMETER;
160 return gensec_sign_packet(spnego_state->sub_sec_security,
163 whole_pdu, pdu_length,
167 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security,
172 struct spnego_state *spnego_state = gensec_security->private_data;
174 if (spnego_state->state_position != SPNEGO_DONE
175 && spnego_state->state_position != SPNEGO_FALLBACK) {
176 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
177 return NT_STATUS_INVALID_PARAMETER;
180 return gensec_wrap(spnego_state->sub_sec_security,
184 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security,
189 struct spnego_state *spnego_state = gensec_security->private_data;
191 if (spnego_state->state_position != SPNEGO_DONE
192 && spnego_state->state_position != SPNEGO_FALLBACK) {
193 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
194 return NT_STATUS_INVALID_PARAMETER;
197 return gensec_unwrap(spnego_state->sub_sec_security,
201 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size)
203 struct spnego_state *spnego_state = gensec_security->private_data;
205 if (spnego_state->state_position != SPNEGO_DONE
206 && spnego_state->state_position != SPNEGO_FALLBACK) {
210 return gensec_sig_size(spnego_state->sub_sec_security, data_size);
213 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security,
214 DATA_BLOB *session_key)
216 struct spnego_state *spnego_state = gensec_security->private_data;
217 if (!spnego_state->sub_sec_security) {
218 return NT_STATUS_INVALID_PARAMETER;
221 return gensec_session_key(spnego_state->sub_sec_security,
225 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
226 struct auth_session_info **session_info)
228 struct spnego_state *spnego_state = gensec_security->private_data;
229 if (!spnego_state->sub_sec_security) {
230 return NT_STATUS_INVALID_PARAMETER;
233 return gensec_session_info(spnego_state->sub_sec_security,
237 /** Fallback to another GENSEC mechanism, based on magic strings
239 * This is the 'fallback' case, where we don't get SPNEGO, and have to
240 * try all the other options (and hope they all have a magic string
244 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
245 struct spnego_state *spnego_state,
246 TALLOC_CTX *out_mem_ctx,
247 const DATA_BLOB in, DATA_BLOB *out)
251 const struct gensec_security_ops **all_ops = gensec_security_all(&num_ops);
252 for (i=0; i < num_ops; i++) {
255 if (!all_ops[i]->oid) {
260 for (j=0; all_ops[i]->oid[j]; j++) {
261 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
269 if (!all_ops[i]->magic) {
273 nt_status = all_ops[i]->magic(gensec_security, &in);
274 if (!NT_STATUS_IS_OK(nt_status)) {
278 spnego_state->state_position = SPNEGO_FALLBACK;
280 nt_status = gensec_subcontext_start(spnego_state,
282 &spnego_state->sub_sec_security);
284 if (!NT_STATUS_IS_OK(nt_status)) {
287 /* select the sub context */
288 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
290 if (!NT_STATUS_IS_OK(nt_status)) {
293 nt_status = gensec_update(spnego_state->sub_sec_security,
294 out_mem_ctx, in, out);
297 DEBUG(1, ("Failed to parse SPNEGO request\n"));
298 return NT_STATUS_INVALID_PARAMETER;
303 Parse the netTokenInit, either from the client, to the server, or
304 from the server to the client.
307 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
308 struct spnego_state *spnego_state,
309 TALLOC_CTX *out_mem_ctx,
310 const char **mechType,
311 const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out)
314 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
315 DATA_BLOB null_data_blob = data_blob(NULL,0);
317 const struct gensec_security_ops_wrapper *all_sec
318 = gensec_security_by_oid_list(out_mem_ctx,
321 for (i=0; all_sec && all_sec[i].op; i++) {
322 nt_status = gensec_subcontext_start(spnego_state,
324 &spnego_state->sub_sec_security);
325 if (!NT_STATUS_IS_OK(nt_status)) {
328 /* select the sub context */
329 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
331 if (!NT_STATUS_IS_OK(nt_status)) {
332 talloc_free(spnego_state->sub_sec_security);
333 spnego_state->sub_sec_security = NULL;
337 if ((i == 0) && (strcmp(all_sec[0].oid, mechType[0]) == 0)) {
338 nt_status = gensec_update(spnego_state->sub_sec_security,
343 /* only get the helping start blob for the first OID */
344 nt_status = gensec_update(spnego_state->sub_sec_security,
348 /* it is likely that a NULL input token will
349 * not be liked by most server mechs, but this
350 * does the right thing in the CIFS client.
351 * just push us along the merry-go-round
352 * again, and hope for better luck next
355 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
356 *unwrapped_out = data_blob(NULL, 0);
357 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
361 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
362 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
363 && !NT_STATUS_IS_OK(nt_status)) {
364 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
365 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
366 talloc_free(spnego_state->sub_sec_security);
367 spnego_state->sub_sec_security = NULL;
369 /* We started the mech correctly, and the
370 * input from the other side was valid.
371 * Return the error (say bad password, invalid
375 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
376 /* Pretend we never started it (lets the first run find some incompatible demand) */
378 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
379 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
380 talloc_free(spnego_state->sub_sec_security);
381 spnego_state->sub_sec_security = NULL;
385 spnego_state->neg_oid = all_sec[i].oid;
387 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
390 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
391 /* we could re-negotiate here, but it would only work
392 * if the client or server lied about what it could
393 * support the first time. Lets keep this code to
396 return NT_STATUS_INVALID_PARAMETER;
399 /** create a negTokenInit
401 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
403 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
404 struct spnego_state *spnego_state,
405 TALLOC_CTX *out_mem_ctx,
406 const DATA_BLOB in, DATA_BLOB *out)
409 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
410 DATA_BLOB null_data_blob = data_blob(NULL,0);
411 const char **mechTypes = NULL;
412 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
413 const struct gensec_security_ops_wrapper *all_sec;
414 const char *principal = NULL;
416 mechTypes = gensec_security_oids(out_mem_ctx, GENSEC_OID_SPNEGO);
418 all_sec = gensec_security_by_oid_list(out_mem_ctx,
421 for (i=0; all_sec && all_sec[i].op; i++) {
422 struct spnego_data spnego_out;
423 nt_status = gensec_subcontext_start(spnego_state,
425 &spnego_state->sub_sec_security);
426 if (!NT_STATUS_IS_OK(nt_status)) {
429 /* select the sub context */
430 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
432 if (!NT_STATUS_IS_OK(nt_status)) {
433 talloc_free(spnego_state->sub_sec_security);
434 spnego_state->sub_sec_security = NULL;
438 /* In the client, try and produce the first (optimistic) packet */
439 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
440 nt_status = gensec_update(spnego_state->sub_sec_security,
445 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
446 && !NT_STATUS_IS_OK(nt_status)) {
447 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
448 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
449 talloc_free(spnego_state->sub_sec_security);
450 spnego_state->sub_sec_security = NULL;
451 /* Pretend we never started it (lets the first run find some incompatible demand) */
457 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
459 /* List the remaining mechs as options */
460 spnego_out.negTokenInit.mechTypes = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
462 spnego_out.negTokenInit.reqFlags = 0;
464 if (spnego_state->state_position == SPNEGO_SERVER_START) {
465 /* server credentails */
466 struct cli_credentials *creds = gensec_get_credentials(gensec_security);
468 principal = cli_credentials_get_principal(creds, out_mem_ctx);
472 spnego_out.negTokenInit.mechListMIC
473 = data_blob_string_const(principal);
475 spnego_out.negTokenInit.mechListMIC = null_data_blob;
478 spnego_out.negTokenInit.mechToken = unwrapped_out;
480 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
481 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
482 return NT_STATUS_INVALID_PARAMETER;
486 spnego_state->neg_oid = all_sec[i].oid;
488 if (NT_STATUS_IS_OK(nt_status)) {
489 spnego_state->no_response_expected = True;
492 return NT_STATUS_MORE_PROCESSING_REQUIRED;
494 talloc_free(spnego_state->sub_sec_security);
495 spnego_state->sub_sec_security = NULL;
497 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
498 return NT_STATUS_INVALID_PARAMETER;
502 /** create a server negTokenTarg
504 * This is the case, where the client is the first one who sends data
507 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
508 struct spnego_state *spnego_state,
509 TALLOC_CTX *out_mem_ctx,
511 const DATA_BLOB unwrapped_out, DATA_BLOB *out)
513 struct spnego_data spnego_out;
514 DATA_BLOB null_data_blob = data_blob(NULL, 0);
517 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
518 spnego_out.negTokenTarg.responseToken = unwrapped_out;
519 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
520 spnego_out.negTokenTarg.supportedMech = NULL;
522 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
523 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
524 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
525 spnego_state->state_position = SPNEGO_SERVER_TARG;
526 } else if (NT_STATUS_IS_OK(nt_status)) {
527 if (unwrapped_out.data) {
528 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
530 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
531 spnego_state->state_position = SPNEGO_DONE;
533 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
534 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
535 spnego_state->state_position = SPNEGO_DONE;
538 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
539 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
540 return NT_STATUS_INVALID_PARAMETER;
543 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
549 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
550 const DATA_BLOB in, DATA_BLOB *out)
552 struct spnego_state *spnego_state = gensec_security->private_data;
553 DATA_BLOB null_data_blob = data_blob(NULL, 0);
554 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
555 struct spnego_data spnego_out;
556 struct spnego_data spnego;
560 *out = data_blob(NULL, 0);
563 out_mem_ctx = spnego_state;
566 /* and switch into the state machine */
568 switch (spnego_state->state_position) {
569 case SPNEGO_FALLBACK:
570 return gensec_update(spnego_state->sub_sec_security,
571 out_mem_ctx, in, out);
572 case SPNEGO_SERVER_START:
577 len = spnego_read_data(in, &spnego);
579 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
580 out_mem_ctx, in, out);
582 /* client sent NegTargetInit, we send NegTokenTarg */
584 /* OK, so it's real SPNEGO, check the packet's the one we expect */
585 if (spnego.type != spnego_state->expected_packet) {
586 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
587 spnego_state->expected_packet));
588 dump_data(1, in.data, in.length);
589 spnego_free_data(&spnego);
590 return NT_STATUS_INVALID_PARAMETER;
593 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
596 spnego.negTokenInit.mechTypes,
597 spnego.negTokenInit.mechToken,
600 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
607 spnego_free_data(&spnego);
611 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
612 out_mem_ctx, in, out);
613 spnego_state->state_position = SPNEGO_SERVER_START;
614 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
619 case SPNEGO_CLIENT_START:
621 /* The server offers a list of mechanisms */
623 const char *my_mechs[] = {NULL, NULL};
624 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
627 /* client to produce negTokenInit */
628 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
629 out_mem_ctx, in, out);
630 spnego_state->state_position = SPNEGO_CLIENT_TARG;
631 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
635 len = spnego_read_data(in, &spnego);
638 DEBUG(1, ("Invalid SPNEGO request:\n"));
639 dump_data(1, in.data, in.length);
640 return NT_STATUS_INVALID_PARAMETER;
643 /* OK, so it's real SPNEGO, check the packet's the one we expect */
644 if (spnego.type != spnego_state->expected_packet) {
645 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
646 spnego_state->expected_packet));
647 dump_data(1, in.data, in.length);
648 spnego_free_data(&spnego);
649 return NT_STATUS_INVALID_PARAMETER;
652 if (spnego.negTokenInit.targetPrincipal) {
653 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
654 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
657 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
660 spnego.negTokenInit.mechTypes,
661 spnego.negTokenInit.mechToken,
664 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
665 spnego_free_data(&spnego);
669 my_mechs[0] = spnego_state->neg_oid;
671 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
672 spnego_out.negTokenInit.mechTypes = my_mechs;
673 spnego_out.negTokenInit.reqFlags = 0;
674 spnego_out.negTokenInit.mechListMIC = null_data_blob;
675 spnego_out.negTokenInit.mechToken = unwrapped_out;
677 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
678 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
679 return NT_STATUS_INVALID_PARAMETER;
683 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
684 spnego_state->state_position = SPNEGO_CLIENT_TARG;
686 if (NT_STATUS_IS_OK(nt_status)) {
687 spnego_state->no_response_expected = True;
690 spnego_free_data(&spnego);
691 return NT_STATUS_MORE_PROCESSING_REQUIRED;
693 case SPNEGO_SERVER_TARG:
697 return NT_STATUS_INVALID_PARAMETER;
700 len = spnego_read_data(in, &spnego);
703 DEBUG(1, ("Invalid SPNEGO request:\n"));
704 dump_data(1, in.data, in.length);
705 return NT_STATUS_INVALID_PARAMETER;
708 /* OK, so it's real SPNEGO, check the packet's the one we expect */
709 if (spnego.type != spnego_state->expected_packet) {
710 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
711 spnego_state->expected_packet));
712 dump_data(1, in.data, in.length);
713 spnego_free_data(&spnego);
714 return NT_STATUS_INVALID_PARAMETER;
717 if (!spnego_state->sub_sec_security) {
718 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
719 spnego_free_data(&spnego);
720 return NT_STATUS_INVALID_PARAMETER;
723 nt_status = gensec_update(spnego_state->sub_sec_security,
725 spnego.negTokenTarg.responseToken,
728 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
735 spnego_free_data(&spnego);
739 case SPNEGO_CLIENT_TARG:
743 return NT_STATUS_INVALID_PARAMETER;
746 len = spnego_read_data(in, &spnego);
749 DEBUG(1, ("Invalid SPNEGO request:\n"));
750 dump_data(1, in.data, in.length);
751 return NT_STATUS_INVALID_PARAMETER;
754 /* OK, so it's real SPNEGO, check the packet's the one we expect */
755 if (spnego.type != spnego_state->expected_packet) {
756 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
757 spnego_state->expected_packet));
758 dump_data(1, in.data, in.length);
759 spnego_free_data(&spnego);
760 return NT_STATUS_INVALID_PARAMETER;
763 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
764 spnego_free_data(&spnego);
765 return NT_STATUS_ACCESS_DENIED;
768 /* Server didn't like our choice of mech, and chose something else */
769 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
770 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
771 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
772 gensec_get_name_by_oid(spnego.negTokenTarg.supportedMech),
773 gensec_get_name_by_oid(spnego_state->neg_oid)));
775 talloc_free(spnego_state->sub_sec_security);
776 nt_status = gensec_subcontext_start(spnego_state,
778 &spnego_state->sub_sec_security);
779 if (!NT_STATUS_IS_OK(nt_status)) {
780 spnego_free_data(&spnego);
783 /* select the sub context */
784 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
785 spnego.negTokenTarg.supportedMech);
786 if (!NT_STATUS_IS_OK(nt_status)) {
787 spnego_free_data(&spnego);
791 nt_status = gensec_update(spnego_state->sub_sec_security,
793 spnego.negTokenTarg.responseToken,
795 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
796 } else if (spnego_state->no_response_expected) {
797 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
798 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
799 nt_status = NT_STATUS_INVALID_PARAMETER;
800 } else if (spnego.negTokenTarg.responseToken.length) {
801 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
802 nt_status = NT_STATUS_INVALID_PARAMETER;
804 nt_status = NT_STATUS_OK;
807 nt_status = gensec_update(spnego_state->sub_sec_security,
809 spnego.negTokenTarg.responseToken,
812 if (NT_STATUS_IS_OK(nt_status)) {
813 spnego_state->no_response_expected = True;
817 spnego_free_data(&spnego);
819 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
820 && !NT_STATUS_IS_OK(nt_status)) {
821 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
822 spnego_state->sub_sec_security->ops->name,
823 nt_errstr(nt_status)));
827 if (unwrapped_out.length) {
829 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
830 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
831 spnego_out.negTokenTarg.supportedMech = NULL;
832 spnego_out.negTokenTarg.responseToken = unwrapped_out;
833 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
835 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
836 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
837 return NT_STATUS_INVALID_PARAMETER;
840 spnego_state->state_position = SPNEGO_CLIENT_TARG;
841 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
844 /* all done - server has accepted, and we agree */
845 *out = null_data_blob;
847 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
848 /* unless of course it did not accept */
849 DEBUG(1,("gensec_update ok but not accepted\n"));
850 nt_status = NT_STATUS_INVALID_PARAMETER;
853 spnego_state->state_position = SPNEGO_DONE;
859 /* We should not be called after we are 'done' */
860 return NT_STATUS_INVALID_PARAMETER;
862 return NT_STATUS_INVALID_PARAMETER;
865 static BOOL gensec_spnego_have_feature(struct gensec_security *gensec_security,
868 struct spnego_state *spnego_state = gensec_security->private_data;
869 if (!spnego_state->sub_sec_security) {
873 return gensec_have_feature(spnego_state->sub_sec_security,
877 static const char *gensec_spnego_oids[] = {
882 static const struct gensec_security_ops gensec_spnego_security_ops = {
884 .sasl_name = "GSS-SPNEGO",
885 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
886 .oid = gensec_spnego_oids,
887 .client_start = gensec_spnego_client_start,
888 .server_start = gensec_spnego_server_start,
889 .update = gensec_spnego_update,
890 .seal_packet = gensec_spnego_seal_packet,
891 .sign_packet = gensec_spnego_sign_packet,
892 .sig_size = gensec_spnego_sig_size,
893 .check_packet = gensec_spnego_check_packet,
894 .unseal_packet = gensec_spnego_unseal_packet,
895 .wrap = gensec_spnego_wrap,
896 .unwrap = gensec_spnego_unwrap,
897 .session_key = gensec_spnego_session_key,
898 .session_info = gensec_spnego_session_info,
899 .have_feature = gensec_spnego_have_feature,
903 NTSTATUS gensec_spnego_init(void)
906 ret = gensec_register(&gensec_spnego_security_ops);
907 if (!NT_STATUS_IS_OK(ret)) {
908 DEBUG(0,("Failed to register '%s' gensec backend!\n",
909 gensec_spnego_security_ops.name));