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"
27 #include "auth/gensec/spnego_proto.h"
29 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)
250 struct gensec_security_ops **all_ops
251 = gensec_security_mechs(gensec_security, out_mem_ctx);
252 for (i=0; all_ops[i]; 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(gensec_security,
322 if (spnego_state->state_position == SPNEGO_SERVER_START) {
323 for (i=0; all_sec && all_sec[i].op; i++) {
324 /* optomisitic token */
325 if (strcmp(all_sec[i].oid, mechType[0]) == 0) {
326 nt_status = gensec_subcontext_start(spnego_state,
328 &spnego_state->sub_sec_security);
329 if (!NT_STATUS_IS_OK(nt_status)) {
332 /* select the sub context */
333 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
335 if (!NT_STATUS_IS_OK(nt_status)) {
336 talloc_free(spnego_state->sub_sec_security);
337 spnego_state->sub_sec_security = NULL;
341 nt_status = gensec_update(spnego_state->sub_sec_security,
345 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
346 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
347 /* Pretend we never started it (lets the first run find some incompatible demand) */
349 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
350 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
351 talloc_free(spnego_state->sub_sec_security);
352 spnego_state->sub_sec_security = NULL;
356 spnego_state->neg_oid = all_sec[i].oid;
362 /* Having tried any optomisitc token from the client (if we
363 * were the server), if we didn't get anywhere, walk our list
364 * in our preference order */
366 if (!spnego_state->sub_sec_security) {
367 for (i=0; all_sec && all_sec[i].op; i++) {
368 nt_status = gensec_subcontext_start(spnego_state,
370 &spnego_state->sub_sec_security);
371 if (!NT_STATUS_IS_OK(nt_status)) {
374 /* select the sub context */
375 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
377 if (!NT_STATUS_IS_OK(nt_status)) {
378 talloc_free(spnego_state->sub_sec_security);
379 spnego_state->sub_sec_security = NULL;
383 spnego_state->neg_oid = all_sec[i].oid;
385 /* only get the helping start blob for the first OID */
386 nt_status = gensec_update(spnego_state->sub_sec_security,
391 /* it is likely that a NULL input token will
392 * not be liked by most server mechs, but if
393 * we are in the client, we want the first
394 * update packet to be able to abort the use
396 if (spnego_state->state_position != SPNEGO_SERVER_START) {
397 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
398 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
399 /* Pretend we never started it (lets the first run find some incompatible demand) */
401 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
402 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
403 talloc_free(spnego_state->sub_sec_security);
404 spnego_state->sub_sec_security = NULL;
413 if (spnego_state->sub_sec_security) {
414 /* it is likely that a NULL input token will
415 * not be liked by most server mechs, but this
416 * does the right thing in the CIFS client.
417 * just push us along the merry-go-round
418 * again, and hope for better luck next
421 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
422 *unwrapped_out = data_blob(NULL, 0);
423 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
426 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
427 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
428 && !NT_STATUS_IS_OK(nt_status)) {
429 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
430 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
431 talloc_free(spnego_state->sub_sec_security);
432 spnego_state->sub_sec_security = NULL;
434 /* We started the mech correctly, and the
435 * input from the other side was valid.
436 * Return the error (say bad password, invalid
442 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
445 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
446 /* we could re-negotiate here, but it would only work
447 * if the client or server lied about what it could
448 * support the first time. Lets keep this code to
451 return NT_STATUS_INVALID_PARAMETER;
454 /** create a negTokenInit
456 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
458 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
459 struct spnego_state *spnego_state,
460 TALLOC_CTX *out_mem_ctx,
461 const DATA_BLOB in, DATA_BLOB *out)
464 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
465 DATA_BLOB null_data_blob = data_blob(NULL,0);
466 const char **mechTypes = NULL;
467 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
468 const struct gensec_security_ops_wrapper *all_sec;
469 const char *principal = NULL;
471 mechTypes = gensec_security_oids(gensec_security,
472 out_mem_ctx, GENSEC_OID_SPNEGO);
474 all_sec = gensec_security_by_oid_list(gensec_security,
478 for (i=0; all_sec && all_sec[i].op; i++) {
479 struct spnego_data spnego_out;
480 nt_status = gensec_subcontext_start(spnego_state,
482 &spnego_state->sub_sec_security);
483 if (!NT_STATUS_IS_OK(nt_status)) {
486 /* select the sub context */
487 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
489 if (!NT_STATUS_IS_OK(nt_status)) {
490 talloc_free(spnego_state->sub_sec_security);
491 spnego_state->sub_sec_security = NULL;
495 /* In the client, try and produce the first (optimistic) packet */
496 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
497 nt_status = gensec_update(spnego_state->sub_sec_security,
502 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
503 && !NT_STATUS_IS_OK(nt_status)) {
504 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
505 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
506 talloc_free(spnego_state->sub_sec_security);
507 spnego_state->sub_sec_security = NULL;
508 /* Pretend we never started it (lets the first run find some incompatible demand) */
514 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
516 /* List the remaining mechs as options */
517 spnego_out.negTokenInit.mechTypes = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
519 spnego_out.negTokenInit.reqFlags = 0;
521 if (spnego_state->state_position == SPNEGO_SERVER_START) {
522 /* server credentails */
523 struct cli_credentials *creds = gensec_get_credentials(gensec_security);
525 principal = cli_credentials_get_principal(creds, out_mem_ctx);
529 spnego_out.negTokenInit.mechListMIC
530 = data_blob_string_const(principal);
532 spnego_out.negTokenInit.mechListMIC = null_data_blob;
535 spnego_out.negTokenInit.mechToken = unwrapped_out;
537 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
538 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
539 return NT_STATUS_INVALID_PARAMETER;
543 spnego_state->neg_oid = all_sec[i].oid;
545 if (NT_STATUS_IS_OK(nt_status)) {
546 spnego_state->no_response_expected = True;
549 return NT_STATUS_MORE_PROCESSING_REQUIRED;
551 talloc_free(spnego_state->sub_sec_security);
552 spnego_state->sub_sec_security = NULL;
554 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
555 return NT_STATUS_INVALID_PARAMETER;
559 /** create a server negTokenTarg
561 * This is the case, where the client is the first one who sends data
564 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
565 struct spnego_state *spnego_state,
566 TALLOC_CTX *out_mem_ctx,
568 const DATA_BLOB unwrapped_out, DATA_BLOB *out)
570 struct spnego_data spnego_out;
571 DATA_BLOB null_data_blob = data_blob(NULL, 0);
574 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
575 spnego_out.negTokenTarg.responseToken = unwrapped_out;
576 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
577 spnego_out.negTokenTarg.supportedMech = NULL;
579 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
580 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
581 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
582 spnego_state->state_position = SPNEGO_SERVER_TARG;
583 } else if (NT_STATUS_IS_OK(nt_status)) {
584 if (unwrapped_out.data) {
585 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
587 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
588 spnego_state->state_position = SPNEGO_DONE;
590 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
591 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
592 spnego_state->state_position = SPNEGO_DONE;
595 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
596 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
597 return NT_STATUS_INVALID_PARAMETER;
600 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
606 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
607 const DATA_BLOB in, DATA_BLOB *out)
609 struct spnego_state *spnego_state = gensec_security->private_data;
610 DATA_BLOB null_data_blob = data_blob(NULL, 0);
611 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
612 struct spnego_data spnego_out;
613 struct spnego_data spnego;
617 *out = data_blob(NULL, 0);
620 out_mem_ctx = spnego_state;
623 /* and switch into the state machine */
625 switch (spnego_state->state_position) {
626 case SPNEGO_FALLBACK:
627 return gensec_update(spnego_state->sub_sec_security,
628 out_mem_ctx, in, out);
629 case SPNEGO_SERVER_START:
634 len = spnego_read_data(in, &spnego);
636 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
637 out_mem_ctx, in, out);
639 /* client sent NegTargetInit, we send NegTokenTarg */
641 /* OK, so it's real SPNEGO, check the packet's the one we expect */
642 if (spnego.type != spnego_state->expected_packet) {
643 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
644 spnego_state->expected_packet));
645 dump_data(1, in.data, in.length);
646 spnego_free_data(&spnego);
647 return NT_STATUS_INVALID_PARAMETER;
650 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
653 spnego.negTokenInit.mechTypes,
654 spnego.negTokenInit.mechToken,
657 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
664 spnego_free_data(&spnego);
668 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
669 out_mem_ctx, in, out);
670 spnego_state->state_position = SPNEGO_SERVER_START;
671 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
676 case SPNEGO_CLIENT_START:
678 /* The server offers a list of mechanisms */
680 const char *my_mechs[] = {NULL, NULL};
681 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
684 /* client to produce negTokenInit */
685 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
686 out_mem_ctx, in, out);
687 spnego_state->state_position = SPNEGO_CLIENT_TARG;
688 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
692 len = spnego_read_data(in, &spnego);
695 DEBUG(1, ("Invalid SPNEGO request:\n"));
696 dump_data(1, in.data, in.length);
697 return NT_STATUS_INVALID_PARAMETER;
700 /* OK, so it's real SPNEGO, check the packet's the one we expect */
701 if (spnego.type != spnego_state->expected_packet) {
702 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
703 spnego_state->expected_packet));
704 dump_data(1, in.data, in.length);
705 spnego_free_data(&spnego);
706 return NT_STATUS_INVALID_PARAMETER;
709 if (spnego.negTokenInit.targetPrincipal) {
710 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
711 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
714 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
717 spnego.negTokenInit.mechTypes,
718 spnego.negTokenInit.mechToken,
721 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
722 spnego_free_data(&spnego);
726 my_mechs[0] = spnego_state->neg_oid;
728 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
729 spnego_out.negTokenInit.mechTypes = my_mechs;
730 spnego_out.negTokenInit.reqFlags = 0;
731 spnego_out.negTokenInit.mechListMIC = null_data_blob;
732 spnego_out.negTokenInit.mechToken = unwrapped_out;
734 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
735 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
736 return NT_STATUS_INVALID_PARAMETER;
740 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
741 spnego_state->state_position = SPNEGO_CLIENT_TARG;
743 if (NT_STATUS_IS_OK(nt_status)) {
744 spnego_state->no_response_expected = True;
747 spnego_free_data(&spnego);
748 return NT_STATUS_MORE_PROCESSING_REQUIRED;
750 case SPNEGO_SERVER_TARG:
754 return NT_STATUS_INVALID_PARAMETER;
757 len = spnego_read_data(in, &spnego);
760 DEBUG(1, ("Invalid SPNEGO request:\n"));
761 dump_data(1, in.data, in.length);
762 return NT_STATUS_INVALID_PARAMETER;
765 /* OK, so it's real SPNEGO, check the packet's the one we expect */
766 if (spnego.type != spnego_state->expected_packet) {
767 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
768 spnego_state->expected_packet));
769 dump_data(1, in.data, in.length);
770 spnego_free_data(&spnego);
771 return NT_STATUS_INVALID_PARAMETER;
774 if (!spnego_state->sub_sec_security) {
775 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
776 spnego_free_data(&spnego);
777 return NT_STATUS_INVALID_PARAMETER;
780 nt_status = gensec_update(spnego_state->sub_sec_security,
782 spnego.negTokenTarg.responseToken,
785 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
792 spnego_free_data(&spnego);
796 case SPNEGO_CLIENT_TARG:
800 return NT_STATUS_INVALID_PARAMETER;
803 len = spnego_read_data(in, &spnego);
806 DEBUG(1, ("Invalid SPNEGO request:\n"));
807 dump_data(1, in.data, in.length);
808 return NT_STATUS_INVALID_PARAMETER;
811 /* OK, so it's real SPNEGO, check the packet's the one we expect */
812 if (spnego.type != spnego_state->expected_packet) {
813 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
814 spnego_state->expected_packet));
815 dump_data(1, in.data, in.length);
816 spnego_free_data(&spnego);
817 return NT_STATUS_INVALID_PARAMETER;
820 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
821 spnego_free_data(&spnego);
822 return NT_STATUS_ACCESS_DENIED;
825 /* Server didn't like our choice of mech, and chose something else */
826 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
827 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
828 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
829 gensec_get_name_by_oid(spnego.negTokenTarg.supportedMech),
830 gensec_get_name_by_oid(spnego_state->neg_oid)));
832 talloc_free(spnego_state->sub_sec_security);
833 nt_status = gensec_subcontext_start(spnego_state,
835 &spnego_state->sub_sec_security);
836 if (!NT_STATUS_IS_OK(nt_status)) {
837 spnego_free_data(&spnego);
840 /* select the sub context */
841 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
842 spnego.negTokenTarg.supportedMech);
843 if (!NT_STATUS_IS_OK(nt_status)) {
844 spnego_free_data(&spnego);
848 nt_status = gensec_update(spnego_state->sub_sec_security,
850 spnego.negTokenTarg.responseToken,
852 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
853 } else if (spnego_state->no_response_expected) {
854 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
855 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
856 nt_status = NT_STATUS_INVALID_PARAMETER;
857 } else if (spnego.negTokenTarg.responseToken.length) {
858 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
859 nt_status = NT_STATUS_INVALID_PARAMETER;
861 nt_status = NT_STATUS_OK;
864 nt_status = gensec_update(spnego_state->sub_sec_security,
866 spnego.negTokenTarg.responseToken,
869 if (NT_STATUS_IS_OK(nt_status)) {
870 spnego_state->no_response_expected = True;
874 spnego_free_data(&spnego);
876 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
877 && !NT_STATUS_IS_OK(nt_status)) {
878 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
879 spnego_state->sub_sec_security->ops->name,
880 nt_errstr(nt_status)));
884 if (unwrapped_out.length) {
886 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
887 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
888 spnego_out.negTokenTarg.supportedMech = NULL;
889 spnego_out.negTokenTarg.responseToken = unwrapped_out;
890 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
892 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
893 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
894 return NT_STATUS_INVALID_PARAMETER;
897 spnego_state->state_position = SPNEGO_CLIENT_TARG;
898 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
901 /* all done - server has accepted, and we agree */
902 *out = null_data_blob;
904 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
905 /* unless of course it did not accept */
906 DEBUG(1,("gensec_update ok but not accepted\n"));
907 nt_status = NT_STATUS_INVALID_PARAMETER;
910 spnego_state->state_position = SPNEGO_DONE;
916 /* We should not be called after we are 'done' */
917 return NT_STATUS_INVALID_PARAMETER;
919 return NT_STATUS_INVALID_PARAMETER;
922 static BOOL gensec_spnego_have_feature(struct gensec_security *gensec_security,
925 struct spnego_state *spnego_state = gensec_security->private_data;
926 if (!spnego_state->sub_sec_security) {
930 return gensec_have_feature(spnego_state->sub_sec_security,
934 static const char *gensec_spnego_oids[] = {
939 static const struct gensec_security_ops gensec_spnego_security_ops = {
941 .sasl_name = "GSS-SPNEGO",
942 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
943 .oid = gensec_spnego_oids,
944 .client_start = gensec_spnego_client_start,
945 .server_start = gensec_spnego_server_start,
946 .update = gensec_spnego_update,
947 .seal_packet = gensec_spnego_seal_packet,
948 .sign_packet = gensec_spnego_sign_packet,
949 .sig_size = gensec_spnego_sig_size,
950 .check_packet = gensec_spnego_check_packet,
951 .unseal_packet = gensec_spnego_unseal_packet,
952 .wrap = gensec_spnego_wrap,
953 .unwrap = gensec_spnego_unwrap,
954 .session_key = gensec_spnego_session_key,
955 .session_info = gensec_spnego_session_info,
956 .have_feature = gensec_spnego_have_feature,
960 NTSTATUS gensec_spnego_init(void)
963 ret = gensec_register(&gensec_spnego_security_ops);
964 if (!NT_STATUS_IS_OK(ret)) {
965 DEBUG(0,("Failed to register '%s' gensec backend!\n",
966 gensec_spnego_security_ops.name));