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/>.
26 #include "auth/gensec/spnego.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_proto.h"
32 enum spnego_state_position {
42 enum spnego_message_type expected_packet;
43 enum spnego_state_position state_position;
44 struct gensec_security *sub_sec_security;
45 bool no_response_expected;
53 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
55 struct spnego_state *spnego_state;
57 spnego_state = talloc(gensec_security, struct spnego_state);
59 return NT_STATUS_NO_MEMORY;
62 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
63 spnego_state->state_position = SPNEGO_CLIENT_START;
64 spnego_state->sub_sec_security = NULL;
65 spnego_state->no_response_expected = false;
66 spnego_state->mech_types = data_blob(NULL, 0);
68 gensec_security->private_data = spnego_state;
72 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
74 struct spnego_state *spnego_state;
76 spnego_state = talloc(gensec_security, struct spnego_state);
78 return NT_STATUS_NO_MEMORY;
81 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
82 spnego_state->state_position = SPNEGO_SERVER_START;
83 spnego_state->sub_sec_security = NULL;
84 spnego_state->no_response_expected = false;
85 spnego_state->mech_types = data_blob(NULL, 0);
87 gensec_security->private_data = spnego_state;
92 wrappers for the spnego_*() functions
94 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security,
96 uint8_t *data, size_t length,
97 const uint8_t *whole_pdu, size_t pdu_length,
100 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
102 if (spnego_state->state_position != SPNEGO_DONE
103 && spnego_state->state_position != SPNEGO_FALLBACK) {
104 return NT_STATUS_INVALID_PARAMETER;
107 return gensec_unseal_packet(spnego_state->sub_sec_security,
110 whole_pdu, pdu_length,
114 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security,
116 const uint8_t *data, size_t length,
117 const uint8_t *whole_pdu, size_t pdu_length,
118 const DATA_BLOB *sig)
120 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
122 if (spnego_state->state_position != SPNEGO_DONE
123 && spnego_state->state_position != SPNEGO_FALLBACK) {
124 return NT_STATUS_INVALID_PARAMETER;
127 return gensec_check_packet(spnego_state->sub_sec_security,
130 whole_pdu, pdu_length,
134 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security,
136 uint8_t *data, size_t length,
137 const uint8_t *whole_pdu, size_t pdu_length,
140 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
142 if (spnego_state->state_position != SPNEGO_DONE
143 && spnego_state->state_position != SPNEGO_FALLBACK) {
144 return NT_STATUS_INVALID_PARAMETER;
147 return gensec_seal_packet(spnego_state->sub_sec_security,
150 whole_pdu, pdu_length,
154 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security,
156 const uint8_t *data, size_t length,
157 const uint8_t *whole_pdu, size_t pdu_length,
160 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
162 if (spnego_state->state_position != SPNEGO_DONE
163 && spnego_state->state_position != SPNEGO_FALLBACK) {
164 return NT_STATUS_INVALID_PARAMETER;
167 return gensec_sign_packet(spnego_state->sub_sec_security,
170 whole_pdu, pdu_length,
174 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security,
179 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
181 if (spnego_state->state_position != SPNEGO_DONE
182 && spnego_state->state_position != SPNEGO_FALLBACK) {
183 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
184 return NT_STATUS_INVALID_PARAMETER;
187 return gensec_wrap(spnego_state->sub_sec_security,
191 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security,
196 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
198 if (spnego_state->state_position != SPNEGO_DONE
199 && spnego_state->state_position != SPNEGO_FALLBACK) {
200 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
201 return NT_STATUS_INVALID_PARAMETER;
204 return gensec_unwrap(spnego_state->sub_sec_security,
208 static NTSTATUS gensec_spnego_wrap_packets(struct gensec_security *gensec_security,
212 size_t *len_processed)
214 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
216 if (spnego_state->state_position != SPNEGO_DONE
217 && spnego_state->state_position != SPNEGO_FALLBACK) {
218 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
219 return NT_STATUS_INVALID_PARAMETER;
222 return gensec_wrap_packets(spnego_state->sub_sec_security,
227 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security,
228 DATA_BLOB blob, size_t *size)
230 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
232 if (spnego_state->state_position != SPNEGO_DONE
233 && spnego_state->state_position != SPNEGO_FALLBACK) {
234 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
235 return NT_STATUS_INVALID_PARAMETER;
238 return gensec_packet_full_request(spnego_state->sub_sec_security,
242 static NTSTATUS gensec_spnego_unwrap_packets(struct gensec_security *gensec_security,
246 size_t *len_processed)
248 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
250 if (spnego_state->state_position != SPNEGO_DONE
251 && spnego_state->state_position != SPNEGO_FALLBACK) {
252 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
253 return NT_STATUS_INVALID_PARAMETER;
256 return gensec_unwrap_packets(spnego_state->sub_sec_security,
261 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size)
263 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
265 if (spnego_state->state_position != SPNEGO_DONE
266 && spnego_state->state_position != SPNEGO_FALLBACK) {
270 return gensec_sig_size(spnego_state->sub_sec_security, data_size);
273 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security)
275 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
277 if (spnego_state->state_position != SPNEGO_DONE
278 && spnego_state->state_position != SPNEGO_FALLBACK) {
282 return gensec_max_input_size(spnego_state->sub_sec_security);
285 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security)
287 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
289 if (spnego_state->state_position != SPNEGO_DONE
290 && spnego_state->state_position != SPNEGO_FALLBACK) {
294 return gensec_max_wrapped_size(spnego_state->sub_sec_security);
297 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security,
298 DATA_BLOB *session_key)
300 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
301 if (!spnego_state->sub_sec_security) {
302 return NT_STATUS_INVALID_PARAMETER;
305 return gensec_session_key(spnego_state->sub_sec_security,
309 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
310 struct auth_session_info **session_info)
312 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
313 if (!spnego_state->sub_sec_security) {
314 return NT_STATUS_INVALID_PARAMETER;
317 return gensec_session_info(spnego_state->sub_sec_security,
321 /** Fallback to another GENSEC mechanism, based on magic strings
323 * This is the 'fallback' case, where we don't get SPNEGO, and have to
324 * try all the other options (and hope they all have a magic string
328 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
329 struct spnego_state *spnego_state,
330 TALLOC_CTX *out_mem_ctx,
331 const DATA_BLOB in, DATA_BLOB *out)
334 struct gensec_security_ops **all_ops
335 = gensec_security_mechs(gensec_security, out_mem_ctx);
336 for (i=0; all_ops[i]; i++) {
339 if (!all_ops[i]->oid) {
344 for (j=0; all_ops[i]->oid[j]; j++) {
345 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
353 if (!all_ops[i]->magic) {
357 nt_status = all_ops[i]->magic(gensec_security, &in);
358 if (!NT_STATUS_IS_OK(nt_status)) {
362 spnego_state->state_position = SPNEGO_FALLBACK;
364 nt_status = gensec_subcontext_start(spnego_state,
366 &spnego_state->sub_sec_security);
368 if (!NT_STATUS_IS_OK(nt_status)) {
371 /* select the sub context */
372 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
374 if (!NT_STATUS_IS_OK(nt_status)) {
377 nt_status = gensec_update(spnego_state->sub_sec_security,
378 out_mem_ctx, in, out);
381 DEBUG(1, ("Failed to parse SPNEGO request\n"));
382 return NT_STATUS_INVALID_PARAMETER;
387 Parse the netTokenInit, either from the client, to the server, or
388 from the server to the client.
391 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
392 struct spnego_state *spnego_state,
393 TALLOC_CTX *out_mem_ctx,
394 const char **mechType,
395 const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out)
398 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
399 DATA_BLOB null_data_blob = data_blob(NULL,0);
402 const struct gensec_security_ops_wrapper *all_sec
403 = gensec_security_by_oid_list(gensec_security,
408 ok = spnego_write_mech_types(spnego_state,
410 &spnego_state->mech_types);
412 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
413 return NT_STATUS_NO_MEMORY;
416 if (spnego_state->state_position == SPNEGO_SERVER_START) {
417 for (i=0; all_sec && all_sec[i].op; i++) {
418 /* optomisitic token */
419 if (strcmp(all_sec[i].oid, mechType[0]) == 0) {
420 nt_status = gensec_subcontext_start(spnego_state,
422 &spnego_state->sub_sec_security);
423 if (!NT_STATUS_IS_OK(nt_status)) {
426 /* select the sub context */
427 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
429 if (!NT_STATUS_IS_OK(nt_status)) {
430 talloc_free(spnego_state->sub_sec_security);
431 spnego_state->sub_sec_security = NULL;
435 nt_status = gensec_update(spnego_state->sub_sec_security,
439 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
440 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
441 /* Pretend we never started it (lets the first run find some incompatible demand) */
443 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
444 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
445 talloc_free(spnego_state->sub_sec_security);
446 spnego_state->sub_sec_security = NULL;
450 spnego_state->neg_oid = all_sec[i].oid;
456 /* Having tried any optomisitc token from the client (if we
457 * were the server), if we didn't get anywhere, walk our list
458 * in our preference order */
460 if (!spnego_state->sub_sec_security) {
461 for (i=0; all_sec && all_sec[i].op; i++) {
462 nt_status = gensec_subcontext_start(spnego_state,
464 &spnego_state->sub_sec_security);
465 if (!NT_STATUS_IS_OK(nt_status)) {
468 /* select the sub context */
469 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
471 if (!NT_STATUS_IS_OK(nt_status)) {
472 talloc_free(spnego_state->sub_sec_security);
473 spnego_state->sub_sec_security = NULL;
477 spnego_state->neg_oid = all_sec[i].oid;
479 /* only get the helping start blob for the first OID */
480 nt_status = gensec_update(spnego_state->sub_sec_security,
485 /* it is likely that a NULL input token will
486 * not be liked by most server mechs, but if
487 * we are in the client, we want the first
488 * update packet to be able to abort the use
490 if (spnego_state->state_position != SPNEGO_SERVER_START) {
491 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
492 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
493 /* Pretend we never started it (lets the first run find some incompatible demand) */
495 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
496 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
497 talloc_free(spnego_state->sub_sec_security);
498 spnego_state->sub_sec_security = NULL;
507 if (spnego_state->sub_sec_security) {
508 /* it is likely that a NULL input token will
509 * not be liked by most server mechs, but this
510 * does the right thing in the CIFS client.
511 * just push us along the merry-go-round
512 * again, and hope for better luck next
515 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
516 *unwrapped_out = data_blob(NULL, 0);
517 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
520 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
521 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
522 && !NT_STATUS_IS_OK(nt_status)) {
523 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
524 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
525 talloc_free(spnego_state->sub_sec_security);
526 spnego_state->sub_sec_security = NULL;
528 /* We started the mech correctly, and the
529 * input from the other side was valid.
530 * Return the error (say bad password, invalid
536 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
539 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
540 /* we could re-negotiate here, but it would only work
541 * if the client or server lied about what it could
542 * support the first time. Lets keep this code to
548 /** create a negTokenInit
550 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
552 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
553 struct spnego_state *spnego_state,
554 TALLOC_CTX *out_mem_ctx,
555 const DATA_BLOB in, DATA_BLOB *out)
558 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
559 DATA_BLOB null_data_blob = data_blob(NULL,0);
560 const char **mechTypes = NULL;
561 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
562 const struct gensec_security_ops_wrapper *all_sec;
563 const char *principal = NULL;
565 mechTypes = gensec_security_oids(gensec_security,
566 out_mem_ctx, GENSEC_OID_SPNEGO);
568 all_sec = gensec_security_by_oid_list(gensec_security,
572 for (i=0; all_sec && all_sec[i].op; i++) {
573 struct spnego_data spnego_out;
574 const char **send_mech_types;
577 nt_status = gensec_subcontext_start(spnego_state,
579 &spnego_state->sub_sec_security);
580 if (!NT_STATUS_IS_OK(nt_status)) {
583 /* select the sub context */
584 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
586 if (!NT_STATUS_IS_OK(nt_status)) {
587 talloc_free(spnego_state->sub_sec_security);
588 spnego_state->sub_sec_security = NULL;
592 /* In the client, try and produce the first (optimistic) packet */
593 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
594 nt_status = gensec_update(spnego_state->sub_sec_security,
599 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
600 && !NT_STATUS_IS_OK(nt_status)) {
601 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
602 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
603 talloc_free(spnego_state->sub_sec_security);
604 spnego_state->sub_sec_security = NULL;
605 /* Pretend we never started it (lets the first run find some incompatible demand) */
611 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
613 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
616 ok = spnego_write_mech_types(spnego_state,
618 &spnego_state->mech_types);
620 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
621 return NT_STATUS_NO_MEMORY;
624 /* List the remaining mechs as options */
625 spnego_out.negTokenInit.mechTypes = send_mech_types;
626 spnego_out.negTokenInit.reqFlags = 0;
628 if (spnego_state->state_position == SPNEGO_SERVER_START) {
629 /* server credentials */
630 struct cli_credentials *creds = gensec_get_credentials(gensec_security);
632 principal = cli_credentials_get_principal(creds, out_mem_ctx);
636 spnego_out.negTokenInit.mechListMIC
637 = data_blob_string_const(principal);
639 spnego_out.negTokenInit.mechListMIC = null_data_blob;
642 spnego_out.negTokenInit.mechToken = unwrapped_out;
644 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
645 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
646 return NT_STATUS_INVALID_PARAMETER;
650 spnego_state->neg_oid = all_sec[i].oid;
652 if (NT_STATUS_IS_OK(nt_status)) {
653 spnego_state->no_response_expected = true;
656 return NT_STATUS_MORE_PROCESSING_REQUIRED;
658 talloc_free(spnego_state->sub_sec_security);
659 spnego_state->sub_sec_security = NULL;
661 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
662 return NT_STATUS_INVALID_PARAMETER;
666 /** create a server negTokenTarg
668 * This is the case, where the client is the first one who sends data
671 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
672 struct spnego_state *spnego_state,
673 TALLOC_CTX *out_mem_ctx,
675 const DATA_BLOB unwrapped_out,
676 DATA_BLOB mech_list_mic,
679 struct spnego_data spnego_out;
680 DATA_BLOB null_data_blob = data_blob(NULL, 0);
683 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
684 spnego_out.negTokenTarg.responseToken = unwrapped_out;
685 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
686 spnego_out.negTokenTarg.supportedMech = NULL;
688 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
689 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
690 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
691 spnego_state->state_position = SPNEGO_SERVER_TARG;
692 } else if (NT_STATUS_IS_OK(nt_status)) {
693 if (unwrapped_out.data) {
694 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
696 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
697 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
698 spnego_state->state_position = SPNEGO_DONE;
700 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
701 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
702 spnego_state->state_position = SPNEGO_DONE;
705 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
706 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
707 return NT_STATUS_INVALID_PARAMETER;
710 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
716 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
717 const DATA_BLOB in, DATA_BLOB *out)
719 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
720 DATA_BLOB null_data_blob = data_blob(NULL, 0);
721 DATA_BLOB mech_list_mic = data_blob(NULL, 0);
722 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
723 struct spnego_data spnego_out;
724 struct spnego_data spnego;
728 *out = data_blob(NULL, 0);
731 out_mem_ctx = spnego_state;
734 /* and switch into the state machine */
736 switch (spnego_state->state_position) {
737 case SPNEGO_FALLBACK:
738 return gensec_update(spnego_state->sub_sec_security,
739 out_mem_ctx, in, out);
740 case SPNEGO_SERVER_START:
745 len = spnego_read_data(gensec_security, in, &spnego);
747 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
748 out_mem_ctx, in, out);
750 /* client sent NegTargetInit, we send NegTokenTarg */
752 /* OK, so it's real SPNEGO, check the packet's the one we expect */
753 if (spnego.type != spnego_state->expected_packet) {
754 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
755 spnego_state->expected_packet));
756 dump_data(1, in.data, in.length);
757 spnego_free_data(&spnego);
758 return NT_STATUS_INVALID_PARAMETER;
761 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
764 spnego.negTokenInit.mechTypes,
765 spnego.negTokenInit.mechToken,
768 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
776 spnego_free_data(&spnego);
780 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
781 out_mem_ctx, in, out);
782 spnego_state->state_position = SPNEGO_SERVER_START;
783 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
788 case SPNEGO_CLIENT_START:
790 /* The server offers a list of mechanisms */
792 const char *my_mechs[] = {NULL, NULL};
793 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
796 /* client to produce negTokenInit */
797 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
798 out_mem_ctx, in, out);
799 spnego_state->state_position = SPNEGO_CLIENT_TARG;
800 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
804 len = spnego_read_data(gensec_security, in, &spnego);
807 DEBUG(1, ("Invalid SPNEGO request:\n"));
808 dump_data(1, in.data, in.length);
809 return NT_STATUS_INVALID_PARAMETER;
812 /* OK, so it's real SPNEGO, check the packet's the one we expect */
813 if (spnego.type != spnego_state->expected_packet) {
814 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
815 spnego_state->expected_packet));
816 dump_data(1, in.data, in.length);
817 spnego_free_data(&spnego);
818 return NT_STATUS_INVALID_PARAMETER;
821 if (spnego.negTokenInit.targetPrincipal) {
822 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
823 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
826 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
829 spnego.negTokenInit.mechTypes,
830 spnego.negTokenInit.mechToken,
833 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
834 spnego_free_data(&spnego);
838 my_mechs[0] = spnego_state->neg_oid;
840 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
841 spnego_out.negTokenInit.mechTypes = my_mechs;
842 spnego_out.negTokenInit.reqFlags = 0;
843 spnego_out.negTokenInit.mechListMIC = null_data_blob;
844 spnego_out.negTokenInit.mechToken = unwrapped_out;
846 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
847 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
848 return NT_STATUS_INVALID_PARAMETER;
852 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
853 spnego_state->state_position = SPNEGO_CLIENT_TARG;
855 if (NT_STATUS_IS_OK(nt_status)) {
856 spnego_state->no_response_expected = true;
859 spnego_free_data(&spnego);
860 return NT_STATUS_MORE_PROCESSING_REQUIRED;
862 case SPNEGO_SERVER_TARG:
865 bool new_spnego = false;
868 return NT_STATUS_INVALID_PARAMETER;
871 len = spnego_read_data(gensec_security, in, &spnego);
874 DEBUG(1, ("Invalid SPNEGO request:\n"));
875 dump_data(1, in.data, in.length);
876 return NT_STATUS_INVALID_PARAMETER;
879 /* OK, so it's real SPNEGO, check the packet's the one we expect */
880 if (spnego.type != spnego_state->expected_packet) {
881 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
882 spnego_state->expected_packet));
883 dump_data(1, in.data, in.length);
884 spnego_free_data(&spnego);
885 return NT_STATUS_INVALID_PARAMETER;
888 if (!spnego_state->sub_sec_security) {
889 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
890 spnego_free_data(&spnego);
891 return NT_STATUS_INVALID_PARAMETER;
894 nt_status = gensec_update(spnego_state->sub_sec_security,
896 spnego.negTokenTarg.responseToken,
898 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
900 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
902 spnego_state->mech_types.data,
903 spnego_state->mech_types.length,
904 spnego_state->mech_types.data,
905 spnego_state->mech_types.length,
906 &spnego.negTokenTarg.mechListMIC);
907 if (!NT_STATUS_IS_OK(nt_status)) {
908 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
909 nt_errstr(nt_status)));
912 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
913 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
915 spnego_state->mech_types.data,
916 spnego_state->mech_types.length,
917 spnego_state->mech_types.data,
918 spnego_state->mech_types.length,
920 if (!NT_STATUS_IS_OK(nt_status)) {
921 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
922 nt_errstr(nt_status)));
926 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
934 spnego_free_data(&spnego);
938 case SPNEGO_CLIENT_TARG:
942 return NT_STATUS_INVALID_PARAMETER;
945 len = spnego_read_data(gensec_security, in, &spnego);
948 DEBUG(1, ("Invalid SPNEGO request:\n"));
949 dump_data(1, in.data, in.length);
950 return NT_STATUS_INVALID_PARAMETER;
953 /* OK, so it's real SPNEGO, check the packet's the one we expect */
954 if (spnego.type != spnego_state->expected_packet) {
955 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
956 spnego_state->expected_packet));
957 dump_data(1, in.data, in.length);
958 spnego_free_data(&spnego);
959 return NT_STATUS_INVALID_PARAMETER;
962 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
963 spnego_free_data(&spnego);
964 return NT_STATUS_ACCESS_DENIED;
967 /* Server didn't like our choice of mech, and chose something else */
968 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
969 spnego.negTokenTarg.supportedMech &&
970 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
971 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
972 gensec_get_name_by_oid(spnego.negTokenTarg.supportedMech),
973 gensec_get_name_by_oid(spnego_state->neg_oid)));
975 talloc_free(spnego_state->sub_sec_security);
976 nt_status = gensec_subcontext_start(spnego_state,
978 &spnego_state->sub_sec_security);
979 if (!NT_STATUS_IS_OK(nt_status)) {
980 spnego_free_data(&spnego);
983 /* select the sub context */
984 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
985 spnego.negTokenTarg.supportedMech);
986 if (!NT_STATUS_IS_OK(nt_status)) {
987 spnego_free_data(&spnego);
991 nt_status = gensec_update(spnego_state->sub_sec_security,
993 spnego.negTokenTarg.responseToken,
995 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
996 } else if (spnego_state->no_response_expected) {
997 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
998 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
999 nt_status = NT_STATUS_INVALID_PARAMETER;
1000 } else if (spnego.negTokenTarg.responseToken.length) {
1001 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1002 nt_status = NT_STATUS_INVALID_PARAMETER;
1004 nt_status = NT_STATUS_OK;
1006 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1007 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1009 spnego_state->mech_types.data,
1010 spnego_state->mech_types.length,
1011 spnego_state->mech_types.data,
1012 spnego_state->mech_types.length,
1013 &spnego.negTokenTarg.mechListMIC);
1014 if (!NT_STATUS_IS_OK(nt_status)) {
1015 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1016 nt_errstr(nt_status)));
1020 bool new_spnego = false;
1022 nt_status = gensec_update(spnego_state->sub_sec_security,
1024 spnego.negTokenTarg.responseToken,
1027 if (NT_STATUS_IS_OK(nt_status)) {
1028 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1029 GENSEC_FEATURE_NEW_SPNEGO);
1031 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1032 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1034 spnego_state->mech_types.data,
1035 spnego_state->mech_types.length,
1036 spnego_state->mech_types.data,
1037 spnego_state->mech_types.length,
1039 if (!NT_STATUS_IS_OK(nt_status)) {
1040 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1041 nt_errstr(nt_status)));
1044 if (NT_STATUS_IS_OK(nt_status)) {
1045 spnego_state->no_response_expected = true;
1049 spnego_free_data(&spnego);
1051 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1052 && !NT_STATUS_IS_OK(nt_status)) {
1053 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1054 spnego_state->sub_sec_security->ops->name,
1055 nt_errstr(nt_status)));
1059 if (unwrapped_out.length) {
1061 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1062 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1063 spnego_out.negTokenTarg.supportedMech = NULL;
1064 spnego_out.negTokenTarg.responseToken = unwrapped_out;
1065 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1067 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1068 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1069 return NT_STATUS_INVALID_PARAMETER;
1072 spnego_state->state_position = SPNEGO_CLIENT_TARG;
1073 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1076 /* all done - server has accepted, and we agree */
1077 *out = null_data_blob;
1079 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1080 /* unless of course it did not accept */
1081 DEBUG(1,("gensec_update ok but not accepted\n"));
1082 nt_status = NT_STATUS_INVALID_PARAMETER;
1085 spnego_state->state_position = SPNEGO_DONE;
1091 /* We should not be called after we are 'done' */
1092 return NT_STATUS_INVALID_PARAMETER;
1094 return NT_STATUS_INVALID_PARAMETER;
1097 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1100 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1101 if (!spnego_state->sub_sec_security) {
1105 return gensec_have_feature(spnego_state->sub_sec_security,
1109 static const char *gensec_spnego_oids[] = {
1114 static const struct gensec_security_ops gensec_spnego_security_ops = {
1116 .sasl_name = "GSS-SPNEGO",
1117 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1118 .oid = gensec_spnego_oids,
1119 .client_start = gensec_spnego_client_start,
1120 .server_start = gensec_spnego_server_start,
1121 .update = gensec_spnego_update,
1122 .seal_packet = gensec_spnego_seal_packet,
1123 .sign_packet = gensec_spnego_sign_packet,
1124 .sig_size = gensec_spnego_sig_size,
1125 .max_wrapped_size = gensec_spnego_max_wrapped_size,
1126 .max_input_size = gensec_spnego_max_input_size,
1127 .check_packet = gensec_spnego_check_packet,
1128 .unseal_packet = gensec_spnego_unseal_packet,
1129 .packet_full_request = gensec_spnego_packet_full_request,
1130 .wrap = gensec_spnego_wrap,
1131 .unwrap = gensec_spnego_unwrap,
1132 .wrap_packets = gensec_spnego_wrap_packets,
1133 .unwrap_packets = gensec_spnego_unwrap_packets,
1134 .session_key = gensec_spnego_session_key,
1135 .session_info = gensec_spnego_session_info,
1136 .have_feature = gensec_spnego_have_feature,
1138 .priority = GENSEC_SPNEGO
1141 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1144 ret = gensec_register(&gensec_spnego_security_ops);
1145 if (!NT_STATUS_IS_OK(ret)) {
1146 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1147 gensec_spnego_security_ops.name));