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);
645 spnego_out.negTokenInit.mechListMIC
646 = data_blob_string_const(principal);
648 spnego_out.negTokenInit.mechListMIC = null_data_blob;
651 spnego_out.negTokenInit.mechToken = unwrapped_out;
653 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
654 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
655 return NT_STATUS_INVALID_PARAMETER;
659 spnego_state->neg_oid = all_sec[i].oid;
661 if (NT_STATUS_IS_OK(nt_status)) {
662 spnego_state->no_response_expected = true;
665 return NT_STATUS_MORE_PROCESSING_REQUIRED;
667 talloc_free(spnego_state->sub_sec_security);
668 spnego_state->sub_sec_security = NULL;
670 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
671 return NT_STATUS_INVALID_PARAMETER;
675 /** create a server negTokenTarg
677 * This is the case, where the client is the first one who sends data
680 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
681 struct spnego_state *spnego_state,
682 TALLOC_CTX *out_mem_ctx,
684 const DATA_BLOB unwrapped_out,
685 DATA_BLOB mech_list_mic,
688 struct spnego_data spnego_out;
689 DATA_BLOB null_data_blob = data_blob(NULL, 0);
692 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
693 spnego_out.negTokenTarg.responseToken = unwrapped_out;
694 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
695 spnego_out.negTokenTarg.supportedMech = NULL;
697 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
698 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
699 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
700 spnego_state->state_position = SPNEGO_SERVER_TARG;
701 } else if (NT_STATUS_IS_OK(nt_status)) {
702 if (unwrapped_out.data) {
703 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
705 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
706 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
707 spnego_state->state_position = SPNEGO_DONE;
709 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
710 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
711 spnego_state->state_position = SPNEGO_DONE;
714 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
715 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
716 return NT_STATUS_INVALID_PARAMETER;
719 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
725 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
726 const DATA_BLOB in, DATA_BLOB *out)
728 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
729 DATA_BLOB null_data_blob = data_blob(NULL, 0);
730 DATA_BLOB mech_list_mic = data_blob(NULL, 0);
731 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
732 struct spnego_data spnego_out;
733 struct spnego_data spnego;
737 *out = data_blob(NULL, 0);
740 out_mem_ctx = spnego_state;
743 /* and switch into the state machine */
745 switch (spnego_state->state_position) {
746 case SPNEGO_FALLBACK:
747 return gensec_update(spnego_state->sub_sec_security,
748 out_mem_ctx, in, out);
749 case SPNEGO_SERVER_START:
754 len = spnego_read_data(gensec_security, in, &spnego);
756 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
757 out_mem_ctx, in, out);
759 /* client sent NegTargetInit, we send NegTokenTarg */
761 /* OK, so it's real SPNEGO, check the packet's the one we expect */
762 if (spnego.type != spnego_state->expected_packet) {
763 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
764 spnego_state->expected_packet));
765 dump_data(1, in.data, in.length);
766 spnego_free_data(&spnego);
767 return NT_STATUS_INVALID_PARAMETER;
770 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
773 spnego.negTokenInit.mechTypes,
774 spnego.negTokenInit.mechToken,
777 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
785 spnego_free_data(&spnego);
789 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
790 out_mem_ctx, in, out);
791 spnego_state->state_position = SPNEGO_SERVER_START;
792 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
797 case SPNEGO_CLIENT_START:
799 /* The server offers a list of mechanisms */
801 const char *my_mechs[] = {NULL, NULL};
802 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
805 /* client to produce negTokenInit */
806 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
807 out_mem_ctx, in, out);
808 spnego_state->state_position = SPNEGO_CLIENT_TARG;
809 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
813 len = spnego_read_data(gensec_security, in, &spnego);
816 DEBUG(1, ("Invalid SPNEGO request:\n"));
817 dump_data(1, in.data, in.length);
818 return NT_STATUS_INVALID_PARAMETER;
821 /* OK, so it's real SPNEGO, check the packet's the one we expect */
822 if (spnego.type != spnego_state->expected_packet) {
823 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
824 spnego_state->expected_packet));
825 dump_data(1, in.data, in.length);
826 spnego_free_data(&spnego);
827 return NT_STATUS_INVALID_PARAMETER;
830 if (spnego.negTokenInit.targetPrincipal
831 && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
832 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
833 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
834 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
838 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
841 spnego.negTokenInit.mechTypes,
842 spnego.negTokenInit.mechToken,
845 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
846 spnego_free_data(&spnego);
850 my_mechs[0] = spnego_state->neg_oid;
852 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
853 spnego_out.negTokenInit.mechTypes = my_mechs;
854 spnego_out.negTokenInit.reqFlags = null_data_blob;
855 spnego_out.negTokenInit.reqFlagsPadding = 0;
856 spnego_out.negTokenInit.mechListMIC = null_data_blob;
857 spnego_out.negTokenInit.mechToken = unwrapped_out;
859 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
860 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
861 return NT_STATUS_INVALID_PARAMETER;
865 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
866 spnego_state->state_position = SPNEGO_CLIENT_TARG;
868 if (NT_STATUS_IS_OK(nt_status)) {
869 spnego_state->no_response_expected = true;
872 spnego_free_data(&spnego);
873 return NT_STATUS_MORE_PROCESSING_REQUIRED;
875 case SPNEGO_SERVER_TARG:
878 bool new_spnego = false;
881 return NT_STATUS_INVALID_PARAMETER;
884 len = spnego_read_data(gensec_security, in, &spnego);
887 DEBUG(1, ("Invalid SPNEGO request:\n"));
888 dump_data(1, in.data, in.length);
889 return NT_STATUS_INVALID_PARAMETER;
892 /* OK, so it's real SPNEGO, check the packet's the one we expect */
893 if (spnego.type != spnego_state->expected_packet) {
894 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
895 spnego_state->expected_packet));
896 dump_data(1, in.data, in.length);
897 spnego_free_data(&spnego);
898 return NT_STATUS_INVALID_PARAMETER;
901 if (!spnego_state->sub_sec_security) {
902 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
903 spnego_free_data(&spnego);
904 return NT_STATUS_INVALID_PARAMETER;
907 nt_status = gensec_update(spnego_state->sub_sec_security,
909 spnego.negTokenTarg.responseToken,
911 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
913 nt_status = gensec_check_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,
919 &spnego.negTokenTarg.mechListMIC);
920 if (!NT_STATUS_IS_OK(nt_status)) {
921 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
922 nt_errstr(nt_status)));
925 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
926 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
928 spnego_state->mech_types.data,
929 spnego_state->mech_types.length,
930 spnego_state->mech_types.data,
931 spnego_state->mech_types.length,
933 if (!NT_STATUS_IS_OK(nt_status)) {
934 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
935 nt_errstr(nt_status)));
939 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
947 spnego_free_data(&spnego);
951 case SPNEGO_CLIENT_TARG:
955 return NT_STATUS_INVALID_PARAMETER;
958 len = spnego_read_data(gensec_security, in, &spnego);
961 DEBUG(1, ("Invalid SPNEGO request:\n"));
962 dump_data(1, in.data, in.length);
963 return NT_STATUS_INVALID_PARAMETER;
966 /* OK, so it's real SPNEGO, check the packet's the one we expect */
967 if (spnego.type != spnego_state->expected_packet) {
968 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
969 spnego_state->expected_packet));
970 dump_data(1, in.data, in.length);
971 spnego_free_data(&spnego);
972 return NT_STATUS_INVALID_PARAMETER;
975 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
976 spnego_free_data(&spnego);
977 return NT_STATUS_ACCESS_DENIED;
980 /* Server didn't like our choice of mech, and chose something else */
981 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
982 spnego.negTokenTarg.supportedMech &&
983 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
984 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
985 gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech),
986 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
988 talloc_free(spnego_state->sub_sec_security);
989 nt_status = gensec_subcontext_start(spnego_state,
991 &spnego_state->sub_sec_security);
992 if (!NT_STATUS_IS_OK(nt_status)) {
993 spnego_free_data(&spnego);
996 /* select the sub context */
997 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
998 spnego.negTokenTarg.supportedMech);
999 if (!NT_STATUS_IS_OK(nt_status)) {
1000 spnego_free_data(&spnego);
1004 nt_status = gensec_update(spnego_state->sub_sec_security,
1006 spnego.negTokenTarg.responseToken,
1008 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1009 } else if (spnego_state->no_response_expected) {
1010 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1011 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1012 nt_status = NT_STATUS_INVALID_PARAMETER;
1013 } else if (spnego.negTokenTarg.responseToken.length) {
1014 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1015 nt_status = NT_STATUS_INVALID_PARAMETER;
1017 nt_status = NT_STATUS_OK;
1019 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1020 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1022 spnego_state->mech_types.data,
1023 spnego_state->mech_types.length,
1024 spnego_state->mech_types.data,
1025 spnego_state->mech_types.length,
1026 &spnego.negTokenTarg.mechListMIC);
1027 if (!NT_STATUS_IS_OK(nt_status)) {
1028 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1029 nt_errstr(nt_status)));
1033 bool new_spnego = false;
1035 nt_status = gensec_update(spnego_state->sub_sec_security,
1037 spnego.negTokenTarg.responseToken,
1040 if (NT_STATUS_IS_OK(nt_status)
1041 && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1042 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1043 GENSEC_FEATURE_NEW_SPNEGO);
1045 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1046 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1048 spnego_state->mech_types.data,
1049 spnego_state->mech_types.length,
1050 spnego_state->mech_types.data,
1051 spnego_state->mech_types.length,
1053 if (!NT_STATUS_IS_OK(nt_status)) {
1054 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1055 nt_errstr(nt_status)));
1058 if (NT_STATUS_IS_OK(nt_status)) {
1059 spnego_state->no_response_expected = true;
1063 spnego_free_data(&spnego);
1065 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1066 && !NT_STATUS_IS_OK(nt_status)) {
1067 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1068 spnego_state->sub_sec_security->ops->name,
1069 nt_errstr(nt_status)));
1073 if (unwrapped_out.length || mech_list_mic.length) {
1075 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1076 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1077 spnego_out.negTokenTarg.supportedMech = NULL;
1078 spnego_out.negTokenTarg.responseToken = unwrapped_out;
1079 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1081 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1082 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1083 return NT_STATUS_INVALID_PARAMETER;
1086 spnego_state->state_position = SPNEGO_CLIENT_TARG;
1087 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1090 /* all done - server has accepted, and we agree */
1091 *out = null_data_blob;
1093 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1094 /* unless of course it did not accept */
1095 DEBUG(1,("gensec_update ok but not accepted\n"));
1096 nt_status = NT_STATUS_INVALID_PARAMETER;
1099 spnego_state->state_position = SPNEGO_DONE;
1105 /* We should not be called after we are 'done' */
1106 return NT_STATUS_INVALID_PARAMETER;
1108 return NT_STATUS_INVALID_PARAMETER;
1111 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1114 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1116 if (!spnego_state || !spnego_state->sub_sec_security) {
1117 gensec_security->want_features |= feature;
1121 gensec_want_feature(spnego_state->sub_sec_security,
1125 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1128 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1129 if (!spnego_state->sub_sec_security) {
1133 return gensec_have_feature(spnego_state->sub_sec_security,
1137 static const char *gensec_spnego_oids[] = {
1142 static const struct gensec_security_ops gensec_spnego_security_ops = {
1144 .sasl_name = "GSS-SPNEGO",
1145 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1146 .oid = gensec_spnego_oids,
1147 .client_start = gensec_spnego_client_start,
1148 .server_start = gensec_spnego_server_start,
1149 .update = gensec_spnego_update,
1150 .seal_packet = gensec_spnego_seal_packet,
1151 .sign_packet = gensec_spnego_sign_packet,
1152 .sig_size = gensec_spnego_sig_size,
1153 .max_wrapped_size = gensec_spnego_max_wrapped_size,
1154 .max_input_size = gensec_spnego_max_input_size,
1155 .check_packet = gensec_spnego_check_packet,
1156 .unseal_packet = gensec_spnego_unseal_packet,
1157 .packet_full_request = gensec_spnego_packet_full_request,
1158 .wrap = gensec_spnego_wrap,
1159 .unwrap = gensec_spnego_unwrap,
1160 .wrap_packets = gensec_spnego_wrap_packets,
1161 .unwrap_packets = gensec_spnego_unwrap_packets,
1162 .session_key = gensec_spnego_session_key,
1163 .session_info = gensec_spnego_session_info,
1164 .want_feature = gensec_spnego_want_feature,
1165 .have_feature = gensec_spnego_have_feature,
1167 .priority = GENSEC_SPNEGO
1170 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1173 ret = gensec_register(&gensec_spnego_security_ops);
1174 if (!NT_STATUS_IS_OK(ret)) {
1175 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1176 gensec_spnego_security_ops.name));