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 "../libcli/auth/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"
31 #include "param/param.h"
33 enum spnego_state_position {
43 enum spnego_message_type expected_packet;
44 enum spnego_state_position state_position;
45 struct gensec_security *sub_sec_security;
46 bool no_response_expected;
54 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
56 struct spnego_state *spnego_state;
58 spnego_state = talloc(gensec_security, struct spnego_state);
60 return NT_STATUS_NO_MEMORY;
63 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
64 spnego_state->state_position = SPNEGO_CLIENT_START;
65 spnego_state->sub_sec_security = NULL;
66 spnego_state->no_response_expected = false;
67 spnego_state->mech_types = data_blob(NULL, 0);
69 gensec_security->private_data = spnego_state;
73 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
75 struct spnego_state *spnego_state;
77 spnego_state = talloc(gensec_security, struct spnego_state);
79 return NT_STATUS_NO_MEMORY;
82 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
83 spnego_state->state_position = SPNEGO_SERVER_START;
84 spnego_state->sub_sec_security = NULL;
85 spnego_state->no_response_expected = false;
86 spnego_state->mech_types = data_blob(NULL, 0);
88 gensec_security->private_data = spnego_state;
93 wrappers for the spnego_*() functions
95 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security,
97 uint8_t *data, size_t length,
98 const uint8_t *whole_pdu, size_t pdu_length,
101 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
103 if (spnego_state->state_position != SPNEGO_DONE
104 && spnego_state->state_position != SPNEGO_FALLBACK) {
105 return NT_STATUS_INVALID_PARAMETER;
108 return gensec_unseal_packet(spnego_state->sub_sec_security,
111 whole_pdu, pdu_length,
115 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security,
117 const uint8_t *data, size_t length,
118 const uint8_t *whole_pdu, size_t pdu_length,
119 const DATA_BLOB *sig)
121 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
123 if (spnego_state->state_position != SPNEGO_DONE
124 && spnego_state->state_position != SPNEGO_FALLBACK) {
125 return NT_STATUS_INVALID_PARAMETER;
128 return gensec_check_packet(spnego_state->sub_sec_security,
131 whole_pdu, pdu_length,
135 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security,
137 uint8_t *data, size_t length,
138 const uint8_t *whole_pdu, size_t pdu_length,
141 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
143 if (spnego_state->state_position != SPNEGO_DONE
144 && spnego_state->state_position != SPNEGO_FALLBACK) {
145 return NT_STATUS_INVALID_PARAMETER;
148 return gensec_seal_packet(spnego_state->sub_sec_security,
151 whole_pdu, pdu_length,
155 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security,
157 const uint8_t *data, size_t length,
158 const uint8_t *whole_pdu, size_t pdu_length,
161 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
163 if (spnego_state->state_position != SPNEGO_DONE
164 && spnego_state->state_position != SPNEGO_FALLBACK) {
165 return NT_STATUS_INVALID_PARAMETER;
168 return gensec_sign_packet(spnego_state->sub_sec_security,
171 whole_pdu, pdu_length,
175 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security,
180 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
182 if (spnego_state->state_position != SPNEGO_DONE
183 && spnego_state->state_position != SPNEGO_FALLBACK) {
184 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
185 return NT_STATUS_INVALID_PARAMETER;
188 return gensec_wrap(spnego_state->sub_sec_security,
192 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security,
197 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
199 if (spnego_state->state_position != SPNEGO_DONE
200 && spnego_state->state_position != SPNEGO_FALLBACK) {
201 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
202 return NT_STATUS_INVALID_PARAMETER;
205 return gensec_unwrap(spnego_state->sub_sec_security,
209 static NTSTATUS gensec_spnego_wrap_packets(struct gensec_security *gensec_security,
213 size_t *len_processed)
215 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
217 if (spnego_state->state_position != SPNEGO_DONE
218 && spnego_state->state_position != SPNEGO_FALLBACK) {
219 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
220 return NT_STATUS_INVALID_PARAMETER;
223 return gensec_wrap_packets(spnego_state->sub_sec_security,
228 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security,
229 DATA_BLOB blob, size_t *size)
231 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
233 if (spnego_state->state_position != SPNEGO_DONE
234 && spnego_state->state_position != SPNEGO_FALLBACK) {
235 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
236 return NT_STATUS_INVALID_PARAMETER;
239 return gensec_packet_full_request(spnego_state->sub_sec_security,
243 static NTSTATUS gensec_spnego_unwrap_packets(struct gensec_security *gensec_security,
247 size_t *len_processed)
249 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
251 if (spnego_state->state_position != SPNEGO_DONE
252 && spnego_state->state_position != SPNEGO_FALLBACK) {
253 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
254 return NT_STATUS_INVALID_PARAMETER;
257 return gensec_unwrap_packets(spnego_state->sub_sec_security,
262 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size)
264 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
266 if (spnego_state->state_position != SPNEGO_DONE
267 && spnego_state->state_position != SPNEGO_FALLBACK) {
271 return gensec_sig_size(spnego_state->sub_sec_security, data_size);
274 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security)
276 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
278 if (spnego_state->state_position != SPNEGO_DONE
279 && spnego_state->state_position != SPNEGO_FALLBACK) {
283 return gensec_max_input_size(spnego_state->sub_sec_security);
286 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security)
288 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
290 if (spnego_state->state_position != SPNEGO_DONE
291 && spnego_state->state_position != SPNEGO_FALLBACK) {
295 return gensec_max_wrapped_size(spnego_state->sub_sec_security);
298 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security,
299 DATA_BLOB *session_key)
301 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
302 if (!spnego_state->sub_sec_security) {
303 return NT_STATUS_INVALID_PARAMETER;
306 return gensec_session_key(spnego_state->sub_sec_security,
310 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
311 struct auth_session_info **session_info)
313 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
314 if (!spnego_state->sub_sec_security) {
315 return NT_STATUS_INVALID_PARAMETER;
318 return gensec_session_info(spnego_state->sub_sec_security,
322 /** Fallback to another GENSEC mechanism, based on magic strings
324 * This is the 'fallback' case, where we don't get SPNEGO, and have to
325 * try all the other options (and hope they all have a magic string
329 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
330 struct spnego_state *spnego_state,
331 TALLOC_CTX *out_mem_ctx,
332 const DATA_BLOB in, DATA_BLOB *out)
335 struct gensec_security_ops **all_ops
336 = gensec_security_mechs(gensec_security, out_mem_ctx);
337 for (i=0; all_ops[i]; i++) {
341 if (gensec_security != NULL &&
342 !gensec_security_ops_enabled(all_ops[i], gensec_security))
345 if (!all_ops[i]->oid) {
350 for (j=0; all_ops[i]->oid[j]; j++) {
351 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
359 if (!all_ops[i]->magic) {
363 nt_status = all_ops[i]->magic(gensec_security, &in);
364 if (!NT_STATUS_IS_OK(nt_status)) {
368 spnego_state->state_position = SPNEGO_FALLBACK;
370 nt_status = gensec_subcontext_start(spnego_state,
372 &spnego_state->sub_sec_security);
374 if (!NT_STATUS_IS_OK(nt_status)) {
377 /* select the sub context */
378 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
380 if (!NT_STATUS_IS_OK(nt_status)) {
383 nt_status = gensec_update(spnego_state->sub_sec_security,
384 out_mem_ctx, in, out);
387 DEBUG(1, ("Failed to parse SPNEGO request\n"));
388 return NT_STATUS_INVALID_PARAMETER;
393 Parse the netTokenInit, either from the client, to the server, or
394 from the server to the client.
397 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
398 struct spnego_state *spnego_state,
399 TALLOC_CTX *out_mem_ctx,
400 const char **mechType,
401 const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out)
404 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
405 DATA_BLOB null_data_blob = data_blob(NULL,0);
408 const struct gensec_security_ops_wrapper *all_sec
409 = gensec_security_by_oid_list(gensec_security,
414 ok = spnego_write_mech_types(spnego_state,
416 &spnego_state->mech_types);
418 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
419 return NT_STATUS_NO_MEMORY;
422 if (spnego_state->state_position == SPNEGO_SERVER_START) {
423 for (i=0; all_sec && all_sec[i].op; i++) {
424 /* optimistic token */
425 if (strcmp(all_sec[i].oid, mechType[0]) == 0) {
426 nt_status = gensec_subcontext_start(spnego_state,
428 &spnego_state->sub_sec_security);
429 if (!NT_STATUS_IS_OK(nt_status)) {
432 /* select the sub context */
433 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
435 if (!NT_STATUS_IS_OK(nt_status)) {
436 talloc_free(spnego_state->sub_sec_security);
437 spnego_state->sub_sec_security = NULL;
441 nt_status = gensec_update(spnego_state->sub_sec_security,
445 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
446 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
447 /* Pretend we never started it (lets the first run find some incompatible demand) */
449 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
450 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
451 talloc_free(spnego_state->sub_sec_security);
452 spnego_state->sub_sec_security = NULL;
456 spnego_state->neg_oid = all_sec[i].oid;
462 /* Having tried any optimistic token from the client (if we
463 * were the server), if we didn't get anywhere, walk our list
464 * in our preference order */
466 if (!spnego_state->sub_sec_security) {
467 for (i=0; all_sec && all_sec[i].op; i++) {
468 nt_status = gensec_subcontext_start(spnego_state,
470 &spnego_state->sub_sec_security);
471 if (!NT_STATUS_IS_OK(nt_status)) {
474 /* select the sub context */
475 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
477 if (!NT_STATUS_IS_OK(nt_status)) {
478 talloc_free(spnego_state->sub_sec_security);
479 spnego_state->sub_sec_security = NULL;
483 spnego_state->neg_oid = all_sec[i].oid;
485 /* only get the helping start blob for the first OID */
486 nt_status = gensec_update(spnego_state->sub_sec_security,
491 /* it is likely that a NULL input token will
492 * not be liked by most server mechs, but if
493 * we are in the client, we want the first
494 * update packet to be able to abort the use
496 if (spnego_state->state_position != SPNEGO_SERVER_START) {
497 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
498 NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
499 NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
500 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
501 /* Pretend we never started it (lets the first run find some incompatible demand) */
503 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
504 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
505 talloc_free(spnego_state->sub_sec_security);
506 spnego_state->sub_sec_security = NULL;
515 if (spnego_state->sub_sec_security) {
516 /* it is likely that a NULL input token will
517 * not be liked by most server mechs, but this
518 * does the right thing in the CIFS client.
519 * just push us along the merry-go-round
520 * again, and hope for better luck next
523 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
524 *unwrapped_out = data_blob(NULL, 0);
525 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
528 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
529 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
530 && !NT_STATUS_IS_OK(nt_status)) {
531 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
532 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
533 talloc_free(spnego_state->sub_sec_security);
534 spnego_state->sub_sec_security = NULL;
536 /* We started the mech correctly, and the
537 * input from the other side was valid.
538 * Return the error (say bad password, invalid
544 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
547 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
548 /* we could re-negotiate here, but it would only work
549 * if the client or server lied about what it could
550 * support the first time. Lets keep this code to
556 /** create a negTokenInit
558 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
560 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
561 struct spnego_state *spnego_state,
562 TALLOC_CTX *out_mem_ctx,
563 const DATA_BLOB in, DATA_BLOB *out)
566 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
567 DATA_BLOB null_data_blob = data_blob(NULL,0);
568 const char **mechTypes = NULL;
569 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
570 const struct gensec_security_ops_wrapper *all_sec;
571 const char *principal = NULL;
573 mechTypes = gensec_security_oids(gensec_security,
574 out_mem_ctx, GENSEC_OID_SPNEGO);
576 all_sec = gensec_security_by_oid_list(gensec_security,
580 for (i=0; all_sec && all_sec[i].op; i++) {
581 struct spnego_data spnego_out;
582 const char **send_mech_types;
585 nt_status = gensec_subcontext_start(spnego_state,
587 &spnego_state->sub_sec_security);
588 if (!NT_STATUS_IS_OK(nt_status)) {
591 /* select the sub context */
592 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
594 if (!NT_STATUS_IS_OK(nt_status)) {
595 talloc_free(spnego_state->sub_sec_security);
596 spnego_state->sub_sec_security = NULL;
600 /* In the client, try and produce the first (optimistic) packet */
601 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
602 nt_status = gensec_update(spnego_state->sub_sec_security,
607 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
608 && !NT_STATUS_IS_OK(nt_status)) {
609 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
610 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
611 talloc_free(spnego_state->sub_sec_security);
612 spnego_state->sub_sec_security = NULL;
613 /* Pretend we never started it (lets the first run find some incompatible demand) */
619 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
621 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
624 ok = spnego_write_mech_types(spnego_state,
626 &spnego_state->mech_types);
628 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
629 return NT_STATUS_NO_MEMORY;
632 /* List the remaining mechs as options */
633 spnego_out.negTokenInit.mechTypes = send_mech_types;
634 spnego_out.negTokenInit.reqFlags = null_data_blob;
635 spnego_out.negTokenInit.reqFlagsPadding = 0;
637 if (spnego_state->state_position == SPNEGO_SERVER_START) {
638 /* server credentials */
639 struct cli_credentials *creds = gensec_get_credentials(gensec_security);
641 principal = cli_credentials_get_principal(creds, out_mem_ctx);
643 principal = ADS_IGNORE_PRINCIPAL;
647 spnego_out.negTokenInit.mechListMIC
648 = data_blob_string_const(principal);
650 spnego_out.negTokenInit.mechListMIC = null_data_blob;
653 spnego_out.negTokenInit.mechToken = unwrapped_out;
655 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
656 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
657 return NT_STATUS_INVALID_PARAMETER;
661 spnego_state->neg_oid = all_sec[i].oid;
663 if (NT_STATUS_IS_OK(nt_status)) {
664 spnego_state->no_response_expected = true;
667 return NT_STATUS_MORE_PROCESSING_REQUIRED;
669 talloc_free(spnego_state->sub_sec_security);
670 spnego_state->sub_sec_security = NULL;
672 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
673 return NT_STATUS_INVALID_PARAMETER;
677 /** create a server negTokenTarg
679 * This is the case, where the client is the first one who sends data
682 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
683 struct spnego_state *spnego_state,
684 TALLOC_CTX *out_mem_ctx,
686 const DATA_BLOB unwrapped_out,
687 DATA_BLOB mech_list_mic,
690 struct spnego_data spnego_out;
691 DATA_BLOB null_data_blob = data_blob(NULL, 0);
694 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
695 spnego_out.negTokenTarg.responseToken = unwrapped_out;
696 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
697 spnego_out.negTokenTarg.supportedMech = NULL;
699 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
700 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
701 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
702 spnego_state->state_position = SPNEGO_SERVER_TARG;
703 } else if (NT_STATUS_IS_OK(nt_status)) {
704 if (unwrapped_out.data) {
705 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
707 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
708 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
709 spnego_state->state_position = SPNEGO_DONE;
711 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
712 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
713 spnego_state->state_position = SPNEGO_DONE;
716 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
717 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
718 return NT_STATUS_INVALID_PARAMETER;
721 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
727 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
728 const DATA_BLOB in, DATA_BLOB *out)
730 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
731 DATA_BLOB null_data_blob = data_blob(NULL, 0);
732 DATA_BLOB mech_list_mic = data_blob(NULL, 0);
733 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
734 struct spnego_data spnego_out;
735 struct spnego_data spnego;
739 *out = data_blob(NULL, 0);
742 out_mem_ctx = spnego_state;
745 /* and switch into the state machine */
747 switch (spnego_state->state_position) {
748 case SPNEGO_FALLBACK:
749 return gensec_update(spnego_state->sub_sec_security,
750 out_mem_ctx, in, out);
751 case SPNEGO_SERVER_START:
756 len = spnego_read_data(gensec_security, in, &spnego);
758 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
759 out_mem_ctx, in, out);
761 /* client sent NegTargetInit, we send NegTokenTarg */
763 /* OK, so it's real SPNEGO, check the packet's the one we expect */
764 if (spnego.type != spnego_state->expected_packet) {
765 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
766 spnego_state->expected_packet));
767 dump_data(1, in.data, in.length);
768 spnego_free_data(&spnego);
769 return NT_STATUS_INVALID_PARAMETER;
772 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
775 spnego.negTokenInit.mechTypes,
776 spnego.negTokenInit.mechToken,
779 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
787 spnego_free_data(&spnego);
791 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
792 out_mem_ctx, in, out);
793 spnego_state->state_position = SPNEGO_SERVER_START;
794 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
799 case SPNEGO_CLIENT_START:
801 /* The server offers a list of mechanisms */
803 const char *my_mechs[] = {NULL, NULL};
804 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
807 /* client to produce negTokenInit */
808 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
809 out_mem_ctx, in, out);
810 spnego_state->state_position = SPNEGO_CLIENT_TARG;
811 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
815 len = spnego_read_data(gensec_security, in, &spnego);
818 DEBUG(1, ("Invalid SPNEGO request:\n"));
819 dump_data(1, in.data, in.length);
820 return NT_STATUS_INVALID_PARAMETER;
823 /* OK, so it's real SPNEGO, check the packet's the one we expect */
824 if (spnego.type != spnego_state->expected_packet) {
825 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
826 spnego_state->expected_packet));
827 dump_data(1, in.data, in.length);
828 spnego_free_data(&spnego);
829 return NT_STATUS_INVALID_PARAMETER;
832 if (spnego.negTokenInit.targetPrincipal
833 && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
834 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
835 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
836 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
840 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
843 spnego.negTokenInit.mechTypes,
844 spnego.negTokenInit.mechToken,
847 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
848 spnego_free_data(&spnego);
852 my_mechs[0] = spnego_state->neg_oid;
854 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
855 spnego_out.negTokenInit.mechTypes = my_mechs;
856 spnego_out.negTokenInit.reqFlags = null_data_blob;
857 spnego_out.negTokenInit.reqFlagsPadding = 0;
858 spnego_out.negTokenInit.mechListMIC = null_data_blob;
859 spnego_out.negTokenInit.mechToken = unwrapped_out;
861 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
862 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
863 return NT_STATUS_INVALID_PARAMETER;
867 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
868 spnego_state->state_position = SPNEGO_CLIENT_TARG;
870 if (NT_STATUS_IS_OK(nt_status)) {
871 spnego_state->no_response_expected = true;
874 spnego_free_data(&spnego);
875 return NT_STATUS_MORE_PROCESSING_REQUIRED;
877 case SPNEGO_SERVER_TARG:
880 bool new_spnego = false;
883 return NT_STATUS_INVALID_PARAMETER;
886 len = spnego_read_data(gensec_security, in, &spnego);
889 DEBUG(1, ("Invalid SPNEGO request:\n"));
890 dump_data(1, in.data, in.length);
891 return NT_STATUS_INVALID_PARAMETER;
894 /* OK, so it's real SPNEGO, check the packet's the one we expect */
895 if (spnego.type != spnego_state->expected_packet) {
896 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
897 spnego_state->expected_packet));
898 dump_data(1, in.data, in.length);
899 spnego_free_data(&spnego);
900 return NT_STATUS_INVALID_PARAMETER;
903 if (!spnego_state->sub_sec_security) {
904 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
905 spnego_free_data(&spnego);
906 return NT_STATUS_INVALID_PARAMETER;
909 nt_status = gensec_update(spnego_state->sub_sec_security,
911 spnego.negTokenTarg.responseToken,
913 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
915 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
917 spnego_state->mech_types.data,
918 spnego_state->mech_types.length,
919 spnego_state->mech_types.data,
920 spnego_state->mech_types.length,
921 &spnego.negTokenTarg.mechListMIC);
922 if (!NT_STATUS_IS_OK(nt_status)) {
923 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
924 nt_errstr(nt_status)));
927 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
928 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
930 spnego_state->mech_types.data,
931 spnego_state->mech_types.length,
932 spnego_state->mech_types.data,
933 spnego_state->mech_types.length,
935 if (!NT_STATUS_IS_OK(nt_status)) {
936 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
937 nt_errstr(nt_status)));
941 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
949 spnego_free_data(&spnego);
953 case SPNEGO_CLIENT_TARG:
957 return NT_STATUS_INVALID_PARAMETER;
960 len = spnego_read_data(gensec_security, in, &spnego);
963 DEBUG(1, ("Invalid SPNEGO request:\n"));
964 dump_data(1, in.data, in.length);
965 return NT_STATUS_INVALID_PARAMETER;
968 /* OK, so it's real SPNEGO, check the packet's the one we expect */
969 if (spnego.type != spnego_state->expected_packet) {
970 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
971 spnego_state->expected_packet));
972 dump_data(1, in.data, in.length);
973 spnego_free_data(&spnego);
974 return NT_STATUS_INVALID_PARAMETER;
977 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
978 spnego_free_data(&spnego);
979 return NT_STATUS_ACCESS_DENIED;
982 /* Server didn't like our choice of mech, and chose something else */
983 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
984 spnego.negTokenTarg.supportedMech &&
985 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
986 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
987 gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech),
988 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
990 talloc_free(spnego_state->sub_sec_security);
991 nt_status = gensec_subcontext_start(spnego_state,
993 &spnego_state->sub_sec_security);
994 if (!NT_STATUS_IS_OK(nt_status)) {
995 spnego_free_data(&spnego);
998 /* select the sub context */
999 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
1000 spnego.negTokenTarg.supportedMech);
1001 if (!NT_STATUS_IS_OK(nt_status)) {
1002 spnego_free_data(&spnego);
1006 nt_status = gensec_update(spnego_state->sub_sec_security,
1008 spnego.negTokenTarg.responseToken,
1010 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1011 } else if (spnego_state->no_response_expected) {
1012 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1013 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1014 nt_status = NT_STATUS_INVALID_PARAMETER;
1015 } else if (spnego.negTokenTarg.responseToken.length) {
1016 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1017 nt_status = NT_STATUS_INVALID_PARAMETER;
1019 nt_status = NT_STATUS_OK;
1021 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1022 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1024 spnego_state->mech_types.data,
1025 spnego_state->mech_types.length,
1026 spnego_state->mech_types.data,
1027 spnego_state->mech_types.length,
1028 &spnego.negTokenTarg.mechListMIC);
1029 if (!NT_STATUS_IS_OK(nt_status)) {
1030 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1031 nt_errstr(nt_status)));
1035 bool new_spnego = false;
1037 nt_status = gensec_update(spnego_state->sub_sec_security,
1039 spnego.negTokenTarg.responseToken,
1042 if (NT_STATUS_IS_OK(nt_status)
1043 && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1044 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1045 GENSEC_FEATURE_NEW_SPNEGO);
1047 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1048 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1050 spnego_state->mech_types.data,
1051 spnego_state->mech_types.length,
1052 spnego_state->mech_types.data,
1053 spnego_state->mech_types.length,
1055 if (!NT_STATUS_IS_OK(nt_status)) {
1056 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1057 nt_errstr(nt_status)));
1060 if (NT_STATUS_IS_OK(nt_status)) {
1061 spnego_state->no_response_expected = true;
1065 spnego_free_data(&spnego);
1067 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1068 && !NT_STATUS_IS_OK(nt_status)) {
1069 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1070 spnego_state->sub_sec_security->ops->name,
1071 nt_errstr(nt_status)));
1075 if (unwrapped_out.length || mech_list_mic.length) {
1077 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1078 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1079 spnego_out.negTokenTarg.supportedMech = NULL;
1080 spnego_out.negTokenTarg.responseToken = unwrapped_out;
1081 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1083 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1084 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1085 return NT_STATUS_INVALID_PARAMETER;
1088 spnego_state->state_position = SPNEGO_CLIENT_TARG;
1089 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1092 /* all done - server has accepted, and we agree */
1093 *out = null_data_blob;
1095 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1096 /* unless of course it did not accept */
1097 DEBUG(1,("gensec_update ok but not accepted\n"));
1098 nt_status = NT_STATUS_INVALID_PARAMETER;
1101 spnego_state->state_position = SPNEGO_DONE;
1107 /* We should not be called after we are 'done' */
1108 return NT_STATUS_INVALID_PARAMETER;
1110 return NT_STATUS_INVALID_PARAMETER;
1113 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1116 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1118 if (!spnego_state || !spnego_state->sub_sec_security) {
1119 gensec_security->want_features |= feature;
1123 gensec_want_feature(spnego_state->sub_sec_security,
1127 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1130 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1131 if (!spnego_state->sub_sec_security) {
1135 return gensec_have_feature(spnego_state->sub_sec_security,
1139 static const char *gensec_spnego_oids[] = {
1144 static const struct gensec_security_ops gensec_spnego_security_ops = {
1146 .sasl_name = "GSS-SPNEGO",
1147 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1148 .oid = gensec_spnego_oids,
1149 .client_start = gensec_spnego_client_start,
1150 .server_start = gensec_spnego_server_start,
1151 .update = gensec_spnego_update,
1152 .seal_packet = gensec_spnego_seal_packet,
1153 .sign_packet = gensec_spnego_sign_packet,
1154 .sig_size = gensec_spnego_sig_size,
1155 .max_wrapped_size = gensec_spnego_max_wrapped_size,
1156 .max_input_size = gensec_spnego_max_input_size,
1157 .check_packet = gensec_spnego_check_packet,
1158 .unseal_packet = gensec_spnego_unseal_packet,
1159 .packet_full_request = gensec_spnego_packet_full_request,
1160 .wrap = gensec_spnego_wrap,
1161 .unwrap = gensec_spnego_unwrap,
1162 .wrap_packets = gensec_spnego_wrap_packets,
1163 .unwrap_packets = gensec_spnego_unwrap_packets,
1164 .session_key = gensec_spnego_session_key,
1165 .session_info = gensec_spnego_session_info,
1166 .want_feature = gensec_spnego_want_feature,
1167 .have_feature = gensec_spnego_have_feature,
1169 .priority = GENSEC_SPNEGO
1172 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1175 ret = gensec_register(&gensec_spnego_security_ops);
1176 if (!NT_STATUS_IS_OK(ret)) {
1177 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1178 gensec_spnego_security_ops.name));