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/>.
27 #include "lib/util/tevent_ntstatus.h"
28 #include "../libcli/auth/spnego.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_internal.h"
33 #include "param/param.h"
34 #include "lib/util/asn1.h"
35 #include "lib/util/base64.h"
39 _PUBLIC_ NTSTATUS gensec_spnego_init(TALLOC_CTX *ctx);
41 enum spnego_state_position {
51 enum spnego_message_type expected_packet;
52 enum spnego_state_position state_position;
53 struct gensec_security *sub_sec_security;
64 bool may_skip_mic_check;
70 * The following is used to implement
71 * the update token fragmentation
75 size_t out_max_length;
80 static void gensec_spnego_update_sub_abort(struct spnego_state *spnego_state)
82 spnego_state->sub_sec_ready = false;
83 TALLOC_FREE(spnego_state->sub_sec_security);
86 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
88 struct spnego_state *spnego_state;
90 spnego_state = talloc_zero(gensec_security, struct spnego_state);
92 return NT_STATUS_NO_MEMORY;
95 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
96 spnego_state->state_position = SPNEGO_CLIENT_START;
97 spnego_state->sub_sec_security = NULL;
98 spnego_state->sub_sec_ready = false;
99 spnego_state->mech_types = data_blob_null;
100 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
101 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
103 spnego_state->simulate_w2k = gensec_setting_bool(gensec_security->settings,
104 "spnego", "simulate_w2k", false);
106 gensec_security->private_data = spnego_state;
110 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
112 struct spnego_state *spnego_state;
114 spnego_state = talloc_zero(gensec_security, struct spnego_state);
116 return NT_STATUS_NO_MEMORY;
119 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
120 spnego_state->state_position = SPNEGO_SERVER_START;
121 spnego_state->sub_sec_security = NULL;
122 spnego_state->sub_sec_ready = false;
123 spnego_state->mech_types = data_blob_null;
124 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
125 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
127 spnego_state->simulate_w2k = gensec_setting_bool(gensec_security->settings,
128 "spnego", "simulate_w2k", false);
130 gensec_security->private_data = spnego_state;
134 /** Fallback to another GENSEC mechanism, based on magic strings
136 * This is the 'fallback' case, where we don't get SPNEGO, and have to
137 * try all the other options (and hope they all have a magic string
141 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
142 struct spnego_state *spnego_state,
147 const struct gensec_security_ops **all_ops;
149 all_ops = gensec_security_mechs(gensec_security, mem_ctx);
151 for (i=0; all_ops && all_ops[i]; i++) {
155 if (gensec_security != NULL &&
156 !gensec_security_ops_enabled(all_ops[i], gensec_security))
161 if (!all_ops[i]->oid) {
166 for (j=0; all_ops[i]->oid[j]; j++) {
167 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
175 if (!all_ops[i]->magic) {
179 nt_status = all_ops[i]->magic(gensec_security, &in);
180 if (!NT_STATUS_IS_OK(nt_status)) {
184 spnego_state->state_position = SPNEGO_FALLBACK;
186 nt_status = gensec_subcontext_start(spnego_state,
188 &spnego_state->sub_sec_security);
190 if (!NT_STATUS_IS_OK(nt_status)) {
193 /* select the sub context */
194 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
196 if (!NT_STATUS_IS_OK(nt_status)) {
202 DEBUG(1, ("Failed to parse SPNEGO request\n"));
203 return NT_STATUS_INVALID_PARAMETER;
207 Parse the netTokenInit, either from the client, to the server, or
208 from the server to the client.
211 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
212 struct spnego_state *spnego_state,
213 TALLOC_CTX *out_mem_ctx,
214 struct tevent_context *ev,
215 struct spnego_data *spnego_in,
216 DATA_BLOB *unwrapped_out)
219 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
220 const char * const *mechType = NULL;
221 DATA_BLOB unwrapped_in = data_blob_null;
223 const struct gensec_security_ops_wrapper *all_sec = NULL;
225 if (spnego_in->type != SPNEGO_NEG_TOKEN_INIT) {
226 return NT_STATUS_INTERNAL_ERROR;
229 mechType = spnego_in->negTokenInit.mechTypes;
230 unwrapped_in = spnego_in->negTokenInit.mechToken;
232 all_sec = gensec_security_by_oid_list(gensec_security,
237 ok = spnego_write_mech_types(spnego_state,
239 &spnego_state->mech_types);
241 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
242 return NT_STATUS_NO_MEMORY;
245 if (spnego_state->state_position == SPNEGO_SERVER_START) {
247 for (j=0; mechType && mechType[j]; j++) {
248 for (i=0; all_sec && all_sec[i].op; i++) {
249 if (strcmp(mechType[j], all_sec[i].oid) != 0) {
253 nt_status = gensec_subcontext_start(spnego_state,
255 &spnego_state->sub_sec_security);
256 if (!NT_STATUS_IS_OK(nt_status)) {
259 /* select the sub context */
260 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
262 if (!NT_STATUS_IS_OK(nt_status)) {
264 * Pretend we never started it
266 gensec_spnego_update_sub_abort(spnego_state);
271 /* no optimistic token */
272 spnego_state->neg_oid = all_sec[i].oid;
273 *unwrapped_out = data_blob_null;
274 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
276 * Indicate the downgrade and request a
279 spnego_state->downgraded = true;
280 spnego_state->mic_requested = true;
284 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
289 if (NT_STATUS_IS_OK(nt_status)) {
290 spnego_state->sub_sec_ready = true;
292 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
293 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
295 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
296 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
299 * Pretend we never started it
301 gensec_spnego_update_sub_abort(spnego_state);
305 spnego_state->neg_oid = all_sec[i].oid;
308 if (spnego_state->sub_sec_security) {
313 if (!spnego_state->sub_sec_security) {
314 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
315 return NT_STATUS_INVALID_PARAMETER;
319 /* Having tried any optimistic token from the client (if we
320 * were the server), if we didn't get anywhere, walk our list
321 * in our preference order */
322 unwrapped_in = data_blob_null;
324 if (!spnego_state->sub_sec_security) {
325 for (i=0; all_sec && all_sec[i].op; i++) {
326 nt_status = gensec_subcontext_start(spnego_state,
328 &spnego_state->sub_sec_security);
329 if (!NT_STATUS_IS_OK(nt_status)) {
332 /* select the sub context */
333 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
335 if (!NT_STATUS_IS_OK(nt_status)) {
337 * Pretend we never started it.
339 gensec_spnego_update_sub_abort(spnego_state);
343 spnego_state->neg_oid = all_sec[i].oid;
345 /* only get the helping start blob for the first OID */
346 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
351 if (NT_STATUS_IS_OK(nt_status)) {
352 spnego_state->sub_sec_ready = true;
355 /* it is likely that a NULL input token will
356 * not be liked by most server mechs, but if
357 * we are in the client, we want the first
358 * update packet to be able to abort the use
360 if (spnego_state->state_position != SPNEGO_SERVER_START) {
361 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
362 NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
363 NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
364 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
365 const char *next = NULL;
366 const char *principal = NULL;
367 int dbg_level = DBGLVL_WARNING;
369 if (all_sec[i+1].op != NULL) {
370 next = all_sec[i+1].op->name;
371 dbg_level = DBGLVL_NOTICE;
374 if (gensec_security->target.principal != NULL) {
375 principal = gensec_security->target.principal;
376 } else if (gensec_security->target.service != NULL &&
377 gensec_security->target.hostname != NULL)
379 principal = talloc_asprintf(spnego_state->sub_sec_security,
381 gensec_security->target.service,
382 gensec_security->target.hostname);
384 principal = gensec_security->target.hostname;
387 DEBUG(dbg_level, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
388 spnego_state->sub_sec_security->ops->name,
390 next, nt_errstr(nt_status)));
393 * Pretend we never started it.
395 gensec_spnego_update_sub_abort(spnego_state);
404 if (spnego_state->sub_sec_security) {
405 /* it is likely that a NULL input token will
406 * not be liked by most server mechs, but this
407 * does the right thing in the CIFS client.
408 * just push us along the merry-go-round
409 * again, and hope for better luck next
412 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
413 *unwrapped_out = data_blob_null;
414 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
417 if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
418 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
419 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
421 /* We started the mech correctly, and the
422 * input from the other side was valid.
423 * Return the error (say bad password, invalid
425 gensec_spnego_update_sub_abort(spnego_state);
429 return nt_status; /* OK or MORE PROCESSING */
432 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
433 /* we could re-negotiate here, but it would only work
434 * if the client or server lied about what it could
435 * support the first time. Lets keep this code to
441 /** create a negTokenInit
443 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
445 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
446 struct spnego_state *spnego_state,
447 TALLOC_CTX *out_mem_ctx,
448 struct tevent_context *ev,
452 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
453 const char **mechTypes = NULL;
454 DATA_BLOB unwrapped_out = data_blob_null;
455 const struct gensec_security_ops_wrapper *all_sec;
457 mechTypes = gensec_security_oids(gensec_security,
458 out_mem_ctx, GENSEC_OID_SPNEGO);
460 all_sec = gensec_security_by_oid_list(gensec_security,
464 for (i=0; all_sec && all_sec[i].op; i++) {
465 struct spnego_data spnego_out;
466 const char **send_mech_types;
469 nt_status = gensec_subcontext_start(spnego_state,
471 &spnego_state->sub_sec_security);
472 if (!NT_STATUS_IS_OK(nt_status)) {
475 /* select the sub context */
476 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
478 if (!NT_STATUS_IS_OK(nt_status)) {
479 gensec_spnego_update_sub_abort(spnego_state);
483 /* In the client, try and produce the first (optimistic) packet */
484 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
485 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
490 if (NT_STATUS_IS_OK(nt_status)) {
491 spnego_state->sub_sec_ready = true;
494 if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
495 const char *next = NULL;
496 const char *principal = NULL;
497 int dbg_level = DBGLVL_WARNING;
499 if (all_sec[i+1].op != NULL) {
500 next = all_sec[i+1].op->name;
501 dbg_level = DBGLVL_NOTICE;
504 if (gensec_security->target.principal != NULL) {
505 principal = gensec_security->target.principal;
506 } else if (gensec_security->target.service != NULL &&
507 gensec_security->target.hostname != NULL)
509 principal = talloc_asprintf(spnego_state->sub_sec_security,
511 gensec_security->target.service,
512 gensec_security->target.hostname);
514 principal = gensec_security->target.hostname;
517 DEBUG(dbg_level, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
518 spnego_state->sub_sec_security->ops->name,
520 next, nt_errstr(nt_status)));
523 * Pretend we never started it
525 gensec_spnego_update_sub_abort(spnego_state);
530 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
532 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
535 ok = spnego_write_mech_types(spnego_state,
537 &spnego_state->mech_types);
539 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
540 return NT_STATUS_NO_MEMORY;
543 /* List the remaining mechs as options */
544 spnego_out.negTokenInit.mechTypes = send_mech_types;
545 spnego_out.negTokenInit.reqFlags = data_blob_null;
546 spnego_out.negTokenInit.reqFlagsPadding = 0;
548 if (spnego_state->state_position == SPNEGO_SERVER_START) {
549 spnego_out.negTokenInit.mechListMIC
550 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
552 spnego_out.negTokenInit.mechListMIC = data_blob_null;
555 spnego_out.negTokenInit.mechToken = unwrapped_out;
557 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
558 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
559 return NT_STATUS_INVALID_PARAMETER;
563 spnego_state->neg_oid = all_sec[i].oid;
565 if (spnego_state->state_position == SPNEGO_SERVER_START) {
566 spnego_state->state_position = SPNEGO_SERVER_START;
567 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
569 spnego_state->state_position = SPNEGO_CLIENT_TARG;
570 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
573 return NT_STATUS_MORE_PROCESSING_REQUIRED;
575 gensec_spnego_update_sub_abort(spnego_state);
577 DEBUG(10, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
582 /** create a server negTokenTarg
584 * This is the case, where the client is the first one who sends data
587 static NTSTATUS gensec_spnego_server_response(struct spnego_state *spnego_state,
588 TALLOC_CTX *out_mem_ctx,
590 const DATA_BLOB unwrapped_out,
591 DATA_BLOB mech_list_mic,
594 struct spnego_data spnego_out;
597 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
598 spnego_out.negTokenTarg.responseToken = unwrapped_out;
599 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
600 spnego_out.negTokenTarg.supportedMech = NULL;
602 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
603 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
604 if (spnego_state->mic_requested) {
605 spnego_out.negTokenTarg.negResult = SPNEGO_REQUEST_MIC;
606 spnego_state->mic_requested = false;
608 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
610 spnego_state->state_position = SPNEGO_SERVER_TARG;
611 } else if (NT_STATUS_IS_OK(nt_status)) {
612 if (unwrapped_out.data) {
613 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
615 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
616 spnego_state->state_position = SPNEGO_DONE;
618 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
619 spnego_out.negTokenTarg.mechListMIC = data_blob_null;
620 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
621 spnego_state->state_position = SPNEGO_DONE;
624 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
625 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
626 return NT_STATUS_INVALID_PARAMETER;
629 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
630 spnego_state->num_targs++;
635 static NTSTATUS gensec_spnego_update_client(struct gensec_security *gensec_security,
636 TALLOC_CTX *out_mem_ctx,
637 struct tevent_context *ev,
638 struct spnego_data *spnego_in,
641 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
642 DATA_BLOB mech_list_mic = data_blob_null;
643 DATA_BLOB unwrapped_out = data_blob_null;
644 struct spnego_data spnego_out;
646 *out = data_blob_null;
648 /* and switch into the state machine */
650 switch (spnego_state->state_position) {
651 case SPNEGO_CLIENT_START:
653 /* The server offers a list of mechanisms */
655 const char *my_mechs[] = {NULL, NULL};
656 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
658 const char *tp = NULL;
660 tp = spnego_in->negTokenInit.targetPrincipal;
661 if (tp != NULL && strcmp(tp, ADS_IGNORE_PRINCIPAL) != 0) {
662 DEBUG(5, ("Server claims it's principal name is %s\n", tp));
663 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
664 gensec_set_target_principal(gensec_security, tp);
668 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
675 if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
679 my_mechs[0] = spnego_state->neg_oid;
681 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
682 spnego_out.negTokenInit.mechTypes = my_mechs;
683 spnego_out.negTokenInit.reqFlags = data_blob_null;
684 spnego_out.negTokenInit.reqFlagsPadding = 0;
685 spnego_out.negTokenInit.mechListMIC = data_blob_null;
686 spnego_out.negTokenInit.mechToken = unwrapped_out;
688 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
689 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
690 return NT_STATUS_INVALID_PARAMETER;
693 ok = spnego_write_mech_types(spnego_state,
695 &spnego_state->mech_types);
697 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
698 return NT_STATUS_NO_MEMORY;
702 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
703 spnego_state->state_position = SPNEGO_CLIENT_TARG;
705 return NT_STATUS_MORE_PROCESSING_REQUIRED;
708 case SPNEGO_CLIENT_TARG:
710 NTSTATUS nt_status = NT_STATUS_INTERNAL_ERROR;
711 const struct spnego_negTokenTarg *ta =
712 &spnego_in->negTokenTarg;
714 spnego_state->num_targs++;
716 if (ta->negResult == SPNEGO_REJECT) {
717 return NT_STATUS_LOGON_FAILURE;
720 if (spnego_in->negTokenTarg.negResult == SPNEGO_REQUEST_MIC) {
721 spnego_state->mic_requested = true;
724 /* Server didn't like our choice of mech, and chose something else */
725 if (((ta->negResult == SPNEGO_ACCEPT_INCOMPLETE) ||
726 (ta->negResult == SPNEGO_REQUEST_MIC)) &&
727 ta->supportedMech != NULL&&
728 strcmp(ta->supportedMech, spnego_state->neg_oid) != 0) {
729 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
730 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid),
731 gensec_get_name_by_oid(gensec_security, ta->supportedMech)));
732 spnego_state->downgraded = true;
733 gensec_spnego_update_sub_abort(spnego_state);
734 nt_status = gensec_subcontext_start(spnego_state,
736 &spnego_state->sub_sec_security);
737 if (!NT_STATUS_IS_OK(nt_status)) {
740 /* select the sub context */
741 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
743 if (!NT_STATUS_IS_OK(nt_status)) {
747 spnego_state->neg_oid = talloc_strdup(spnego_state,
749 if (spnego_state->neg_oid == NULL) {
750 return NT_STATUS_NO_MEMORY;
754 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
755 DATA_BLOB *m = &spnego_in->negTokenTarg.mechListMIC;
756 const DATA_BLOB *r = &spnego_in->negTokenTarg.responseToken;
759 * Windows 2000 has a bug, it repeats the
760 * responseToken in the mechListMIC field.
762 if (m->length == r->length) {
765 cmp = memcmp(m->data, r->data, m->length);
772 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
773 if (spnego_state->sub_sec_ready) {
774 spnego_state->needs_mic_check = true;
778 if (spnego_state->needs_mic_check) {
779 if (spnego_in->negTokenTarg.responseToken.length != 0) {
780 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
781 return NT_STATUS_INVALID_PARAMETER;
784 if (spnego_in->negTokenTarg.mechListMIC.length == 0
785 && spnego_state->may_skip_mic_check) {
787 * In this case we don't require
788 * a mechListMIC from the server.
790 * This works around bugs in the Azure
791 * and Apple spnego implementations.
794 * https://bugzilla.samba.org/show_bug.cgi?id=11994
796 spnego_state->needs_mic_check = false;
797 nt_status = NT_STATUS_OK;
798 goto client_response;
801 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
802 spnego_state->mech_types.data,
803 spnego_state->mech_types.length,
804 spnego_state->mech_types.data,
805 spnego_state->mech_types.length,
806 &spnego_in->negTokenTarg.mechListMIC);
807 if (!NT_STATUS_IS_OK(nt_status)) {
808 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
809 nt_errstr(nt_status)));
812 spnego_state->needs_mic_check = false;
813 spnego_state->done_mic_check = true;
814 goto client_response;
817 if (!spnego_state->sub_sec_ready) {
818 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
820 spnego_in->negTokenTarg.responseToken,
822 if (NT_STATUS_IS_OK(nt_status)) {
823 spnego_state->sub_sec_ready = true;
825 if (!NT_STATUS_IS_OK(nt_status)) {
826 goto client_response;
829 nt_status = NT_STATUS_OK;
832 if (!spnego_state->done_mic_check) {
833 bool have_sign = true;
834 bool new_spnego = false;
836 have_sign = gensec_have_feature(spnego_state->sub_sec_security,
837 GENSEC_FEATURE_SIGN);
838 if (spnego_state->simulate_w2k) {
841 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
842 GENSEC_FEATURE_NEW_SPNEGO);
844 switch (spnego_in->negTokenTarg.negResult) {
845 case SPNEGO_ACCEPT_COMPLETED:
846 case SPNEGO_NONE_RESULT:
847 if (spnego_state->num_targs == 1) {
849 * the first exchange doesn't require
857 case SPNEGO_ACCEPT_INCOMPLETE:
858 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
863 if (spnego_state->downgraded) {
865 * A downgrade should be protected if
872 * The caller may just asked for
873 * GENSEC_FEATURE_SESSION_KEY, this
874 * is only reflected in the want_features.
877 * gensec_have_features(GENSEC_FEATURE_SIGN)
880 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
883 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
887 * Here we're sure our preferred mech was
888 * selected by the server and our caller doesn't
889 * need GENSEC_FEATURE_SIGN nor
890 * GENSEC_FEATURE_SEAL support.
892 * In this case we don't require
893 * a mechListMIC from the server.
895 * This works around bugs in the Azure
896 * and Apple spnego implementations.
899 * https://bugzilla.samba.org/show_bug.cgi?id=11994
901 spnego_state->may_skip_mic_check = true;
904 case SPNEGO_REQUEST_MIC:
905 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
913 if (spnego_state->mic_requested) {
919 if (have_sign && new_spnego) {
920 spnego_state->needs_mic_check = true;
921 spnego_state->needs_mic_sign = true;
925 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
926 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
927 spnego_state->mech_types.data,
928 spnego_state->mech_types.length,
929 spnego_state->mech_types.data,
930 spnego_state->mech_types.length,
931 &spnego_in->negTokenTarg.mechListMIC);
932 if (!NT_STATUS_IS_OK(nt_status)) {
933 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
934 nt_errstr(nt_status)));
937 spnego_state->needs_mic_check = false;
938 spnego_state->done_mic_check = true;
941 if (spnego_state->needs_mic_sign) {
942 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
944 spnego_state->mech_types.data,
945 spnego_state->mech_types.length,
946 spnego_state->mech_types.data,
947 spnego_state->mech_types.length,
949 if (!NT_STATUS_IS_OK(nt_status)) {
950 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
951 nt_errstr(nt_status)));
954 spnego_state->needs_mic_sign = false;
957 if (spnego_state->needs_mic_check) {
958 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
962 if (GENSEC_UPDATE_IS_NTERROR(nt_status)) {
963 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
964 spnego_state->sub_sec_security->ops->name,
965 nt_errstr(nt_status)));
969 if (unwrapped_out.length || mech_list_mic.length) {
971 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
972 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
973 spnego_out.negTokenTarg.supportedMech = NULL;
974 spnego_out.negTokenTarg.responseToken = unwrapped_out;
975 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
977 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
978 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
979 return NT_STATUS_INVALID_PARAMETER;
982 spnego_state->num_targs++;
983 spnego_state->state_position = SPNEGO_CLIENT_TARG;
984 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
987 /* all done - server has accepted, and we agree */
988 *out = data_blob_null;
990 if (ta->negResult != SPNEGO_ACCEPT_COMPLETED) {
991 /* unless of course it did not accept */
992 DEBUG(1,("gensec_update ok but not accepted\n"));
993 nt_status = NT_STATUS_INVALID_PARAMETER;
996 spnego_state->state_position = SPNEGO_DONE;
1006 smb_panic(__location__);
1007 return NT_STATUS_INTERNAL_ERROR;
1010 static NTSTATUS gensec_spnego_update_server(struct gensec_security *gensec_security,
1011 TALLOC_CTX *out_mem_ctx,
1012 struct tevent_context *ev,
1013 struct spnego_data *spnego_in,
1016 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1017 DATA_BLOB mech_list_mic = data_blob_null;
1018 DATA_BLOB unwrapped_out = data_blob_null;
1020 /* and switch into the state machine */
1022 switch (spnego_state->state_position) {
1023 case SPNEGO_SERVER_START:
1027 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
1034 if (spnego_state->simulate_w2k) {
1036 * Windows 2000 returns the unwrapped token
1037 * also in the mech_list_mic field.
1039 * In order to verify our client code,
1040 * we need a way to have a server with this
1043 mech_list_mic = unwrapped_out;
1046 nt_status = gensec_spnego_server_response(spnego_state,
1056 case SPNEGO_SERVER_TARG:
1059 bool have_sign = true;
1060 bool new_spnego = false;
1062 spnego_state->num_targs++;
1064 if (!spnego_state->sub_sec_security) {
1065 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1066 return NT_STATUS_INVALID_PARAMETER;
1069 if (spnego_state->needs_mic_check) {
1070 if (spnego_in->negTokenTarg.responseToken.length != 0) {
1071 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1072 return NT_STATUS_INVALID_PARAMETER;
1075 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1076 spnego_state->mech_types.data,
1077 spnego_state->mech_types.length,
1078 spnego_state->mech_types.data,
1079 spnego_state->mech_types.length,
1080 &spnego_in->negTokenTarg.mechListMIC);
1081 if (NT_STATUS_IS_OK(nt_status)) {
1082 spnego_state->needs_mic_check = false;
1083 spnego_state->done_mic_check = true;
1085 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1086 nt_errstr(nt_status)));
1088 goto server_response;
1091 if (!spnego_state->sub_sec_ready) {
1092 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
1094 spnego_in->negTokenTarg.responseToken,
1096 if (NT_STATUS_IS_OK(nt_status)) {
1097 spnego_state->sub_sec_ready = true;
1099 if (!NT_STATUS_IS_OK(nt_status)) {
1100 goto server_response;
1103 nt_status = NT_STATUS_OK;
1106 have_sign = gensec_have_feature(spnego_state->sub_sec_security,
1107 GENSEC_FEATURE_SIGN);
1108 if (spnego_state->simulate_w2k) {
1111 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1112 GENSEC_FEATURE_NEW_SPNEGO);
1113 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
1117 if (have_sign && new_spnego) {
1118 spnego_state->needs_mic_check = true;
1119 spnego_state->needs_mic_sign = true;
1122 if (have_sign && spnego_in->negTokenTarg.mechListMIC.length > 0) {
1123 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1124 spnego_state->mech_types.data,
1125 spnego_state->mech_types.length,
1126 spnego_state->mech_types.data,
1127 spnego_state->mech_types.length,
1128 &spnego_in->negTokenTarg.mechListMIC);
1129 if (!NT_STATUS_IS_OK(nt_status)) {
1130 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1131 nt_errstr(nt_status)));
1132 goto server_response;
1135 spnego_state->needs_mic_check = false;
1136 spnego_state->done_mic_check = true;
1139 if (spnego_state->needs_mic_sign) {
1140 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1142 spnego_state->mech_types.data,
1143 spnego_state->mech_types.length,
1144 spnego_state->mech_types.data,
1145 spnego_state->mech_types.length,
1147 if (!NT_STATUS_IS_OK(nt_status)) {
1148 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1149 nt_errstr(nt_status)));
1150 goto server_response;
1152 spnego_state->needs_mic_sign = false;
1155 if (spnego_state->needs_mic_check) {
1156 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1160 nt_status = gensec_spnego_server_response(spnego_state,
1174 smb_panic(__location__);
1175 return NT_STATUS_INTERNAL_ERROR;
1178 struct gensec_spnego_update_state {
1179 struct gensec_security *gensec;
1180 struct spnego_state *spnego;
1182 struct spnego_data _spnego_in;
1183 struct spnego_data *spnego_in;
1188 static void gensec_spnego_update_cleanup(struct tevent_req *req,
1189 enum tevent_req_state req_state)
1191 struct gensec_spnego_update_state *state =
1192 tevent_req_data(req,
1193 struct gensec_spnego_update_state);
1195 switch (req_state) {
1196 case TEVENT_REQ_USER_ERROR:
1197 case TEVENT_REQ_TIMED_OUT:
1198 case TEVENT_REQ_NO_MEMORY:
1200 * A fatal error, further updates are not allowed.
1202 state->spnego->state_position = SPNEGO_DONE;
1209 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1210 const DATA_BLOB in, TALLOC_CTX *mem_ctx,
1211 DATA_BLOB *full_in);
1212 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1213 TALLOC_CTX *out_mem_ctx,
1216 static struct tevent_req *gensec_spnego_update_send(TALLOC_CTX *mem_ctx,
1217 struct tevent_context *ev,
1218 struct gensec_security *gensec_security,
1221 struct spnego_state *spnego_state =
1222 talloc_get_type_abort(gensec_security->private_data,
1223 struct spnego_state);
1224 struct tevent_req *req = NULL;
1225 struct gensec_spnego_update_state *state = NULL;
1229 req = tevent_req_create(mem_ctx, &state,
1230 struct gensec_spnego_update_state);
1234 state->gensec = gensec_security;
1235 state->spnego = spnego_state;
1236 tevent_req_set_cleanup_fn(req, gensec_spnego_update_cleanup);
1238 if (spnego_state->out_frag.length > 0) {
1239 if (in.length > 0) {
1240 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1241 return tevent_req_post(req, ev);
1244 status = gensec_spnego_update_out(gensec_security,
1245 state, &state->out);
1246 if (GENSEC_UPDATE_IS_NTERROR(status)) {
1247 tevent_req_nterror(req, status);
1248 return tevent_req_post(req, ev);
1251 state->status = status;
1252 tevent_req_done(req);
1253 return tevent_req_post(req, ev);
1256 status = gensec_spnego_update_in(gensec_security, in,
1257 state, &state->full_in);
1258 state->status = status;
1259 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1260 tevent_req_done(req);
1261 return tevent_req_post(req, ev);
1263 if (tevent_req_nterror(req, status)) {
1264 return tevent_req_post(req, ev);
1267 /* Check if we got a valid SPNEGO blob... */
1269 switch (spnego_state->state_position) {
1270 case SPNEGO_FALLBACK:
1273 case SPNEGO_CLIENT_TARG:
1274 case SPNEGO_SERVER_TARG:
1275 if (state->full_in.length == 0) {
1276 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1277 return tevent_req_post(req, ev);
1281 case SPNEGO_CLIENT_START:
1282 case SPNEGO_SERVER_START:
1284 if (state->full_in.length == 0) {
1285 /* create_negTokenInit later */
1289 len = spnego_read_data(state,
1291 &state->_spnego_in);
1293 if (spnego_state->state_position != SPNEGO_SERVER_START) {
1294 DEBUG(1, ("Invalid SPNEGO request:\n"));
1295 dump_data(1, state->full_in.data,
1296 state->full_in.length);
1297 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1298 return tevent_req_post(req, ev);
1302 * This is the 'fallback' case, where we don't get
1303 * SPNEGO, and have to try all the other options (and
1304 * hope they all have a magic string they check)
1306 status = gensec_spnego_server_try_fallback(gensec_security,
1310 if (tevent_req_nterror(req, status)) {
1311 return tevent_req_post(req, ev);
1315 * We'll continue with SPNEGO_FALLBACK below...
1319 state->spnego_in = &state->_spnego_in;
1321 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1322 if (state->spnego_in->type != spnego_state->expected_packet) {
1323 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n",
1324 state->spnego_in->type,
1325 spnego_state->expected_packet));
1326 dump_data(1, state->full_in.data,
1327 state->full_in.length);
1328 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1329 return tevent_req_post(req, ev);
1335 smb_panic(__location__);
1339 /* and switch into the state machine */
1341 switch (spnego_state->state_position) {
1342 case SPNEGO_FALLBACK:
1343 status = gensec_update_ev(spnego_state->sub_sec_security,
1346 &spnego_state->out_frag);
1349 case SPNEGO_CLIENT_START:
1350 if (state->spnego_in == NULL) {
1351 /* client to produce negTokenInit */
1352 status = gensec_spnego_create_negTokenInit(gensec_security,
1353 spnego_state, state, ev,
1354 &spnego_state->out_frag);
1359 case SPNEGO_CLIENT_TARG:
1360 status = gensec_spnego_update_client(gensec_security,
1363 &spnego_state->out_frag);
1366 case SPNEGO_SERVER_START:
1367 if (state->spnego_in == NULL) {
1368 /* server to produce negTokenInit */
1369 status = gensec_spnego_create_negTokenInit(gensec_security,
1370 spnego_state, state, ev,
1371 &spnego_state->out_frag);
1376 case SPNEGO_SERVER_TARG:
1377 status = gensec_spnego_update_server(gensec_security,
1380 &spnego_state->out_frag);
1384 smb_panic(__location__);
1388 if (GENSEC_UPDATE_IS_NTERROR(status)) {
1389 tevent_req_nterror(req, status);
1390 return tevent_req_post(req, ev);
1393 if (NT_STATUS_IS_OK(status)) {
1394 bool reset_full = true;
1396 reset_full = !spnego_state->done_mic_check;
1398 status = gensec_may_reset_crypto(spnego_state->sub_sec_security,
1400 if (tevent_req_nterror(req, status)) {
1401 return tevent_req_post(req, ev);
1405 spnego_state->out_status = status;
1407 status = gensec_spnego_update_out(gensec_security,
1408 state, &state->out);
1409 if (GENSEC_UPDATE_IS_NTERROR(status)) {
1410 tevent_req_nterror(req, status);
1411 return tevent_req_post(req, ev);
1414 state->status = status;
1415 tevent_req_done(req);
1416 return tevent_req_post(req, ev);
1419 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1420 const DATA_BLOB in, TALLOC_CTX *mem_ctx,
1423 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1427 *full_in = data_blob_null;
1429 switch (spnego_state->state_position) {
1430 case SPNEGO_FALLBACK:
1432 spnego_state->in_needed = 0;
1433 return NT_STATUS_OK;
1435 case SPNEGO_CLIENT_START:
1436 case SPNEGO_CLIENT_TARG:
1437 case SPNEGO_SERVER_START:
1438 case SPNEGO_SERVER_TARG:
1443 return NT_STATUS_INVALID_PARAMETER;
1446 if (spnego_state->in_needed == 0) {
1451 * try to work out the size of the full
1452 * input token, it might be fragmented
1454 ret = asn1_peek_full_tag(in, ASN1_APPLICATION(0), &size);
1455 if ((ret != 0) && (ret != EAGAIN)) {
1456 ret = asn1_peek_full_tag(in, ASN1_CONTEXT(1), &size);
1459 if ((ret == 0) || (ret == EAGAIN)) {
1460 spnego_state->in_needed = size;
1463 * If it is not an asn1 message
1464 * just call the next layer.
1466 spnego_state->in_needed = in.length;
1470 if (spnego_state->in_needed > UINT16_MAX) {
1472 * limit the incoming message to 0xFFFF
1473 * to avoid DoS attacks.
1475 return NT_STATUS_INVALID_BUFFER_SIZE;
1478 if ((spnego_state->in_needed > 0) && (in.length == 0)) {
1480 * If we reach this, we know we got at least
1481 * part of an asn1 message, getting 0 means
1482 * the remote peer wants us to spin.
1484 return NT_STATUS_INVALID_PARAMETER;
1487 expected = spnego_state->in_needed - spnego_state->in_frag.length;
1488 if (in.length > expected) {
1490 * we got more than expected
1492 return NT_STATUS_INVALID_PARAMETER;
1495 if (in.length == spnego_state->in_needed) {
1497 * if the in.length contains the full blob
1500 * Note: this implies spnego_state->in_frag.length == 0,
1501 * but we do not need to check this explicitly
1502 * because we already know that we did not get
1503 * more than expected.
1506 spnego_state->in_needed = 0;
1507 return NT_STATUS_OK;
1510 ok = data_blob_append(spnego_state, &spnego_state->in_frag,
1511 in.data, in.length);
1513 return NT_STATUS_NO_MEMORY;
1516 if (spnego_state->in_needed > spnego_state->in_frag.length) {
1517 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1520 *full_in = spnego_state->in_frag;
1521 talloc_steal(mem_ctx, full_in->data);
1522 spnego_state->in_frag = data_blob_null;
1523 spnego_state->in_needed = 0;
1524 return NT_STATUS_OK;
1527 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1528 TALLOC_CTX *out_mem_ctx,
1531 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1532 DATA_BLOB out = data_blob_null;
1535 *_out = data_blob_null;
1537 if (spnego_state->out_frag.length <= spnego_state->out_max_length) {
1539 * Fast path, we can deliver everything
1542 *_out = spnego_state->out_frag;
1543 if (spnego_state->out_frag.length > 0) {
1544 talloc_steal(out_mem_ctx, _out->data);
1545 spnego_state->out_frag = data_blob_null;
1548 if (!NT_STATUS_IS_OK(spnego_state->out_status)) {
1549 return spnego_state->out_status;
1553 * We're completely done, further updates are not allowed.
1555 spnego_state->state_position = SPNEGO_DONE;
1556 return gensec_child_ready(gensec_security,
1557 spnego_state->sub_sec_security);
1560 out = spnego_state->out_frag;
1563 * copy the remaining bytes
1565 spnego_state->out_frag = data_blob_talloc(spnego_state,
1566 out.data + spnego_state->out_max_length,
1567 out.length - spnego_state->out_max_length);
1568 if (spnego_state->out_frag.data == NULL) {
1569 return NT_STATUS_NO_MEMORY;
1573 * truncate the buffer
1575 ok = data_blob_realloc(spnego_state, &out,
1576 spnego_state->out_max_length);
1578 return NT_STATUS_NO_MEMORY;
1581 talloc_steal(out_mem_ctx, out.data);
1583 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1586 static NTSTATUS gensec_spnego_update_recv(struct tevent_req *req,
1587 TALLOC_CTX *out_mem_ctx,
1590 struct gensec_spnego_update_state *state =
1591 tevent_req_data(req,
1592 struct gensec_spnego_update_state);
1595 *out = data_blob_null;
1597 if (tevent_req_is_nterror(req, &status)) {
1598 tevent_req_received(req);
1603 talloc_steal(out_mem_ctx, state->out.data);
1604 status = state->status;
1605 tevent_req_received(req);
1609 static const char *gensec_spnego_oids[] = {
1614 static const struct gensec_security_ops gensec_spnego_security_ops = {
1616 .sasl_name = "GSS-SPNEGO",
1617 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1618 .oid = gensec_spnego_oids,
1619 .client_start = gensec_spnego_client_start,
1620 .server_start = gensec_spnego_server_start,
1621 .update_send = gensec_spnego_update_send,
1622 .update_recv = gensec_spnego_update_recv,
1623 .seal_packet = gensec_child_seal_packet,
1624 .sign_packet = gensec_child_sign_packet,
1625 .sig_size = gensec_child_sig_size,
1626 .max_wrapped_size = gensec_child_max_wrapped_size,
1627 .max_input_size = gensec_child_max_input_size,
1628 .check_packet = gensec_child_check_packet,
1629 .unseal_packet = gensec_child_unseal_packet,
1630 .wrap = gensec_child_wrap,
1631 .unwrap = gensec_child_unwrap,
1632 .session_key = gensec_child_session_key,
1633 .session_info = gensec_child_session_info,
1634 .want_feature = gensec_child_want_feature,
1635 .have_feature = gensec_child_have_feature,
1636 .expire_time = gensec_child_expire_time,
1637 .final_auth_type = gensec_child_final_auth_type,
1639 .priority = GENSEC_SPNEGO
1642 _PUBLIC_ NTSTATUS gensec_spnego_init(TALLOC_CTX *ctx)
1645 ret = gensec_register(ctx, &gensec_spnego_security_ops);
1646 if (!NT_STATUS_IS_OK(ret)) {
1647 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1648 gensec_spnego_security_ops.name));