auth/spnego: simplify the error handling logic in gensec_spnego_parse_negTokenInit()
[Samba.git] / auth / gensec / spnego.c
blobcec44bc1e2bec85c038c8051c5e1623b65253720
1 /*
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/>.
25 #include "includes.h"
26 #include <tevent.h>
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"
37 #undef strcasecmp
39 _PUBLIC_ NTSTATUS gensec_spnego_init(TALLOC_CTX *ctx);
41 enum spnego_state_position {
42 SPNEGO_SERVER_START,
43 SPNEGO_CLIENT_START,
44 SPNEGO_SERVER_TARG,
45 SPNEGO_CLIENT_TARG,
46 SPNEGO_FALLBACK,
47 SPNEGO_DONE
50 struct spnego_state {
51 enum spnego_message_type expected_packet;
52 enum spnego_state_position state_position;
53 struct gensec_security *sub_sec_security;
54 bool sub_sec_ready;
56 const char *neg_oid;
58 DATA_BLOB mech_types;
59 size_t num_targs;
60 bool downgraded;
61 bool mic_requested;
62 bool needs_mic_sign;
63 bool needs_mic_check;
64 bool may_skip_mic_check;
65 bool done_mic_check;
67 bool simulate_w2k;
70 * The following is used to implement
71 * the update token fragmentation
73 size_t in_needed;
74 DATA_BLOB in_frag;
75 size_t out_max_length;
76 DATA_BLOB out_frag;
77 NTSTATUS out_status;
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);
91 if (!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;
107 return NT_STATUS_OK;
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);
115 if (!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;
131 return NT_STATUS_OK;
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
138 * they check)
141 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
142 struct spnego_state *spnego_state,
143 TALLOC_CTX *mem_ctx,
144 const DATA_BLOB in)
146 int i,j;
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++) {
152 bool is_spnego;
153 NTSTATUS nt_status;
155 if (gensec_security != NULL &&
156 !gensec_security_ops_enabled(all_ops[i], gensec_security))
158 continue;
161 if (!all_ops[i]->oid) {
162 continue;
165 is_spnego = false;
166 for (j=0; all_ops[i]->oid[j]; j++) {
167 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
168 is_spnego = true;
171 if (is_spnego) {
172 continue;
175 if (!all_ops[i]->magic) {
176 continue;
179 nt_status = all_ops[i]->magic(gensec_security, &in);
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 continue;
184 spnego_state->state_position = SPNEGO_FALLBACK;
186 nt_status = gensec_subcontext_start(spnego_state,
187 gensec_security,
188 &spnego_state->sub_sec_security);
190 if (!NT_STATUS_IS_OK(nt_status)) {
191 return nt_status;
193 /* select the sub context */
194 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
195 all_ops[i]);
196 if (!NT_STATUS_IS_OK(nt_status)) {
197 return nt_status;
200 return NT_STATUS_OK;
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)
218 int i;
219 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
220 const char * const *mechType = NULL;
221 DATA_BLOB unwrapped_in = data_blob_null;
222 bool ok;
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,
233 out_mem_ctx,
234 mechType,
235 GENSEC_OID_SPNEGO);
237 ok = spnego_write_mech_types(spnego_state,
238 mechType,
239 &spnego_state->mech_types);
240 if (!ok) {
241 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
242 return NT_STATUS_NO_MEMORY;
245 if (spnego_state->state_position == SPNEGO_SERVER_START) {
246 uint32_t j;
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) {
250 continue;
253 nt_status = gensec_subcontext_start(spnego_state,
254 gensec_security,
255 &spnego_state->sub_sec_security);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 return nt_status;
259 /* select the sub context */
260 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
261 all_sec[i].op);
262 if (!NT_STATUS_IS_OK(nt_status)) {
264 * Pretend we never started it
266 gensec_spnego_update_sub_abort(spnego_state);
267 break;
270 if (j > 0) {
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
277 * mic.
279 spnego_state->downgraded = true;
280 spnego_state->mic_requested = true;
281 break;
284 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
285 out_mem_ctx,
287 unwrapped_in,
288 unwrapped_out);
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);
302 break;
305 spnego_state->neg_oid = all_sec[i].oid;
306 break;
308 if (spnego_state->sub_sec_security) {
309 break;
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,
327 gensec_security,
328 &spnego_state->sub_sec_security);
329 if (!NT_STATUS_IS_OK(nt_status)) {
330 return nt_status;
332 /* select the sub context */
333 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
334 all_sec[i].op);
335 if (!NT_STATUS_IS_OK(nt_status)) {
337 * Pretend we never started it.
339 gensec_spnego_update_sub_abort(spnego_state);
340 continue;
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,
347 out_mem_ctx,
349 unwrapped_in,
350 unwrapped_out);
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
359 * of this mech */
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,
380 "%s/%s",
381 gensec_security->target.service,
382 gensec_security->target.hostname);
383 } else {
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,
389 principal,
390 next, nt_errstr(nt_status)));
393 * Pretend we never started it.
395 gensec_spnego_update_sub_abort(spnego_state);
396 continue;
400 break;
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
410 * time */
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
424 * ticket) */
425 gensec_spnego_update_sub_abort(spnego_state);
426 return nt_status;
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
436 * reality */
438 return nt_status;
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,
449 DATA_BLOB *out)
451 int i;
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,
461 out_mem_ctx,
462 mechTypes,
463 GENSEC_OID_SPNEGO);
464 for (i=0; all_sec && all_sec[i].op; i++) {
465 struct spnego_data spnego_out;
466 const char **send_mech_types;
467 bool ok;
469 nt_status = gensec_subcontext_start(spnego_state,
470 gensec_security,
471 &spnego_state->sub_sec_security);
472 if (!NT_STATUS_IS_OK(nt_status)) {
473 return nt_status;
475 /* select the sub context */
476 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
477 all_sec[i].op);
478 if (!NT_STATUS_IS_OK(nt_status)) {
479 gensec_spnego_update_sub_abort(spnego_state);
480 continue;
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,
486 out_mem_ctx,
488 data_blob_null,
489 &unwrapped_out);
490 if (NT_STATUS_IS_OK(nt_status)) {
491 spnego_state->sub_sec_ready = true;
494 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
495 && !NT_STATUS_IS_OK(nt_status)) {
496 const char *next = NULL;
497 const char *principal = NULL;
498 int dbg_level = DBGLVL_WARNING;
500 if (all_sec[i+1].op != NULL) {
501 next = all_sec[i+1].op->name;
502 dbg_level = DBGLVL_NOTICE;
505 if (gensec_security->target.principal != NULL) {
506 principal = gensec_security->target.principal;
507 } else if (gensec_security->target.service != NULL &&
508 gensec_security->target.hostname != NULL)
510 principal = talloc_asprintf(spnego_state->sub_sec_security,
511 "%s/%s",
512 gensec_security->target.service,
513 gensec_security->target.hostname);
514 } else {
515 principal = gensec_security->target.hostname;
518 DEBUG(dbg_level, ("SPNEGO(%s) creating NEG_TOKEN_INIT for %s failed (next[%s]): %s\n",
519 spnego_state->sub_sec_security->ops->name,
520 principal,
521 next, nt_errstr(nt_status)));
524 * Pretend we never started it
526 gensec_spnego_update_sub_abort(spnego_state);
527 continue;
531 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
533 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
534 &all_sec[i]);
536 ok = spnego_write_mech_types(spnego_state,
537 send_mech_types,
538 &spnego_state->mech_types);
539 if (!ok) {
540 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
541 return NT_STATUS_NO_MEMORY;
544 /* List the remaining mechs as options */
545 spnego_out.negTokenInit.mechTypes = send_mech_types;
546 spnego_out.negTokenInit.reqFlags = data_blob_null;
547 spnego_out.negTokenInit.reqFlagsPadding = 0;
549 if (spnego_state->state_position == SPNEGO_SERVER_START) {
550 spnego_out.negTokenInit.mechListMIC
551 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
552 } else {
553 spnego_out.negTokenInit.mechListMIC = data_blob_null;
556 spnego_out.negTokenInit.mechToken = unwrapped_out;
558 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
559 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
560 return NT_STATUS_INVALID_PARAMETER;
563 /* set next state */
564 spnego_state->neg_oid = all_sec[i].oid;
566 if (spnego_state->state_position == SPNEGO_SERVER_START) {
567 spnego_state->state_position = SPNEGO_SERVER_START;
568 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
569 } else {
570 spnego_state->state_position = SPNEGO_CLIENT_TARG;
571 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
574 return NT_STATUS_MORE_PROCESSING_REQUIRED;
576 gensec_spnego_update_sub_abort(spnego_state);
578 DEBUG(10, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
579 return nt_status;
583 /** create a server negTokenTarg
585 * This is the case, where the client is the first one who sends data
588 static NTSTATUS gensec_spnego_server_response(struct spnego_state *spnego_state,
589 TALLOC_CTX *out_mem_ctx,
590 NTSTATUS nt_status,
591 const DATA_BLOB unwrapped_out,
592 DATA_BLOB mech_list_mic,
593 DATA_BLOB *out)
595 struct spnego_data spnego_out;
597 /* compose reply */
598 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
599 spnego_out.negTokenTarg.responseToken = unwrapped_out;
600 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
601 spnego_out.negTokenTarg.supportedMech = NULL;
603 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
604 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
605 if (spnego_state->mic_requested) {
606 spnego_out.negTokenTarg.negResult = SPNEGO_REQUEST_MIC;
607 spnego_state->mic_requested = false;
608 } else {
609 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
611 spnego_state->state_position = SPNEGO_SERVER_TARG;
612 } else if (NT_STATUS_IS_OK(nt_status)) {
613 if (unwrapped_out.data) {
614 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
616 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
617 spnego_state->state_position = SPNEGO_DONE;
618 } else {
619 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
620 spnego_out.negTokenTarg.mechListMIC = data_blob_null;
621 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
622 spnego_state->state_position = SPNEGO_DONE;
625 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
626 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
627 return NT_STATUS_INVALID_PARAMETER;
630 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
631 spnego_state->num_targs++;
633 return nt_status;
636 static NTSTATUS gensec_spnego_update_client(struct gensec_security *gensec_security,
637 TALLOC_CTX *out_mem_ctx,
638 struct tevent_context *ev,
639 struct spnego_data *spnego_in,
640 DATA_BLOB *out)
642 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
643 DATA_BLOB mech_list_mic = data_blob_null;
644 DATA_BLOB unwrapped_out = data_blob_null;
645 struct spnego_data spnego_out;
647 *out = data_blob_null;
649 /* and switch into the state machine */
651 switch (spnego_state->state_position) {
652 case SPNEGO_CLIENT_START:
654 /* The server offers a list of mechanisms */
656 const char *my_mechs[] = {NULL, NULL};
657 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
658 bool ok;
659 const char *tp = NULL;
661 tp = spnego_in->negTokenInit.targetPrincipal;
662 if (tp != NULL && strcmp(tp, ADS_IGNORE_PRINCIPAL) != 0) {
663 DEBUG(5, ("Server claims it's principal name is %s\n", tp));
664 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
665 gensec_set_target_principal(gensec_security, tp);
669 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
670 spnego_state,
671 out_mem_ctx,
673 spnego_in,
674 &unwrapped_out);
676 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
677 return nt_status;
680 my_mechs[0] = spnego_state->neg_oid;
681 /* compose reply */
682 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
683 spnego_out.negTokenInit.mechTypes = my_mechs;
684 spnego_out.negTokenInit.reqFlags = data_blob_null;
685 spnego_out.negTokenInit.reqFlagsPadding = 0;
686 spnego_out.negTokenInit.mechListMIC = data_blob_null;
687 spnego_out.negTokenInit.mechToken = unwrapped_out;
689 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
690 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
691 return NT_STATUS_INVALID_PARAMETER;
694 ok = spnego_write_mech_types(spnego_state,
695 my_mechs,
696 &spnego_state->mech_types);
697 if (!ok) {
698 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
699 return NT_STATUS_NO_MEMORY;
702 /* set next state */
703 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
704 spnego_state->state_position = SPNEGO_CLIENT_TARG;
706 return NT_STATUS_MORE_PROCESSING_REQUIRED;
709 case SPNEGO_CLIENT_TARG:
711 NTSTATUS nt_status = NT_STATUS_INTERNAL_ERROR;
712 const struct spnego_negTokenTarg *ta =
713 &spnego_in->negTokenTarg;
715 spnego_state->num_targs++;
717 if (ta->negResult == SPNEGO_REJECT) {
718 return NT_STATUS_LOGON_FAILURE;
721 if (spnego_in->negTokenTarg.negResult == SPNEGO_REQUEST_MIC) {
722 spnego_state->mic_requested = true;
725 /* Server didn't like our choice of mech, and chose something else */
726 if (((ta->negResult == SPNEGO_ACCEPT_INCOMPLETE) ||
727 (ta->negResult == SPNEGO_REQUEST_MIC)) &&
728 ta->supportedMech != NULL&&
729 strcmp(ta->supportedMech, spnego_state->neg_oid) != 0) {
730 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
731 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid),
732 gensec_get_name_by_oid(gensec_security, ta->supportedMech)));
733 spnego_state->downgraded = true;
734 gensec_spnego_update_sub_abort(spnego_state);
735 nt_status = gensec_subcontext_start(spnego_state,
736 gensec_security,
737 &spnego_state->sub_sec_security);
738 if (!NT_STATUS_IS_OK(nt_status)) {
739 return nt_status;
741 /* select the sub context */
742 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
743 ta->supportedMech);
744 if (!NT_STATUS_IS_OK(nt_status)) {
745 return nt_status;
748 spnego_state->neg_oid = talloc_strdup(spnego_state,
749 ta->supportedMech);
750 if (spnego_state->neg_oid == NULL) {
751 return NT_STATUS_NO_MEMORY;
755 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
756 DATA_BLOB *m = &spnego_in->negTokenTarg.mechListMIC;
757 const DATA_BLOB *r = &spnego_in->negTokenTarg.responseToken;
760 * Windows 2000 has a bug, it repeats the
761 * responseToken in the mechListMIC field.
763 if (m->length == r->length) {
764 int cmp;
766 cmp = memcmp(m->data, r->data, m->length);
767 if (cmp == 0) {
768 data_blob_free(m);
773 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
774 if (spnego_state->sub_sec_ready) {
775 spnego_state->needs_mic_check = true;
779 if (spnego_state->needs_mic_check) {
780 if (spnego_in->negTokenTarg.responseToken.length != 0) {
781 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
782 return NT_STATUS_INVALID_PARAMETER;
785 if (spnego_in->negTokenTarg.mechListMIC.length == 0
786 && spnego_state->may_skip_mic_check) {
788 * In this case we don't require
789 * a mechListMIC from the server.
791 * This works around bugs in the Azure
792 * and Apple spnego implementations.
794 * See
795 * https://bugzilla.samba.org/show_bug.cgi?id=11994
797 spnego_state->needs_mic_check = false;
798 nt_status = NT_STATUS_OK;
799 goto client_response;
802 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
803 spnego_state->mech_types.data,
804 spnego_state->mech_types.length,
805 spnego_state->mech_types.data,
806 spnego_state->mech_types.length,
807 &spnego_in->negTokenTarg.mechListMIC);
808 if (!NT_STATUS_IS_OK(nt_status)) {
809 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
810 nt_errstr(nt_status)));
811 return nt_status;
813 spnego_state->needs_mic_check = false;
814 spnego_state->done_mic_check = true;
815 goto client_response;
818 if (!spnego_state->sub_sec_ready) {
819 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
820 out_mem_ctx, ev,
821 spnego_in->negTokenTarg.responseToken,
822 &unwrapped_out);
823 if (NT_STATUS_IS_OK(nt_status)) {
824 spnego_state->sub_sec_ready = true;
826 if (!NT_STATUS_IS_OK(nt_status)) {
827 goto client_response;
829 } else {
830 nt_status = NT_STATUS_OK;
833 if (!spnego_state->done_mic_check) {
834 bool have_sign = true;
835 bool new_spnego = false;
837 have_sign = gensec_have_feature(spnego_state->sub_sec_security,
838 GENSEC_FEATURE_SIGN);
839 if (spnego_state->simulate_w2k) {
840 have_sign = false;
842 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
843 GENSEC_FEATURE_NEW_SPNEGO);
845 switch (spnego_in->negTokenTarg.negResult) {
846 case SPNEGO_ACCEPT_COMPLETED:
847 case SPNEGO_NONE_RESULT:
848 if (spnego_state->num_targs == 1) {
850 * the first exchange doesn't require
851 * verification
853 new_spnego = false;
856 break;
858 case SPNEGO_ACCEPT_INCOMPLETE:
859 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
860 new_spnego = true;
861 break;
864 if (spnego_state->downgraded) {
866 * A downgrade should be protected if
867 * supported
869 break;
873 * The caller may just asked for
874 * GENSEC_FEATURE_SESSION_KEY, this
875 * is only reflected in the want_features.
877 * As it will imply
878 * gensec_have_features(GENSEC_FEATURE_SIGN)
879 * to return true.
881 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
882 break;
884 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
885 break;
888 * Here we're sure our preferred mech was
889 * selected by the server and our caller doesn't
890 * need GENSEC_FEATURE_SIGN nor
891 * GENSEC_FEATURE_SEAL support.
893 * In this case we don't require
894 * a mechListMIC from the server.
896 * This works around bugs in the Azure
897 * and Apple spnego implementations.
899 * See
900 * https://bugzilla.samba.org/show_bug.cgi?id=11994
902 spnego_state->may_skip_mic_check = true;
903 break;
905 case SPNEGO_REQUEST_MIC:
906 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
907 new_spnego = true;
909 break;
910 default:
911 break;
914 if (spnego_state->mic_requested) {
915 if (have_sign) {
916 new_spnego = true;
920 if (have_sign && new_spnego) {
921 spnego_state->needs_mic_check = true;
922 spnego_state->needs_mic_sign = true;
926 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
927 nt_status = gensec_check_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,
932 &spnego_in->negTokenTarg.mechListMIC);
933 if (!NT_STATUS_IS_OK(nt_status)) {
934 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
935 nt_errstr(nt_status)));
936 return nt_status;
938 spnego_state->needs_mic_check = false;
939 spnego_state->done_mic_check = true;
942 if (spnego_state->needs_mic_sign) {
943 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
944 out_mem_ctx,
945 spnego_state->mech_types.data,
946 spnego_state->mech_types.length,
947 spnego_state->mech_types.data,
948 spnego_state->mech_types.length,
949 &mech_list_mic);
950 if (!NT_STATUS_IS_OK(nt_status)) {
951 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
952 nt_errstr(nt_status)));
953 return nt_status;
955 spnego_state->needs_mic_sign = false;
958 if (spnego_state->needs_mic_check) {
959 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
962 client_response:
963 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
964 && !NT_STATUS_IS_OK(nt_status)) {
965 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
966 spnego_state->sub_sec_security->ops->name,
967 nt_errstr(nt_status)));
968 return nt_status;
971 if (unwrapped_out.length || mech_list_mic.length) {
972 /* compose reply */
973 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
974 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
975 spnego_out.negTokenTarg.supportedMech = NULL;
976 spnego_out.negTokenTarg.responseToken = unwrapped_out;
977 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
979 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
980 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
981 return NT_STATUS_INVALID_PARAMETER;
984 spnego_state->num_targs++;
985 spnego_state->state_position = SPNEGO_CLIENT_TARG;
986 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
987 } else {
989 /* all done - server has accepted, and we agree */
990 *out = data_blob_null;
992 if (ta->negResult != SPNEGO_ACCEPT_COMPLETED) {
993 /* unless of course it did not accept */
994 DEBUG(1,("gensec_update ok but not accepted\n"));
995 nt_status = NT_STATUS_INVALID_PARAMETER;
998 spnego_state->state_position = SPNEGO_DONE;
1001 return nt_status;
1004 default:
1005 break;
1008 smb_panic(__location__);
1009 return NT_STATUS_INTERNAL_ERROR;
1012 static NTSTATUS gensec_spnego_update_server(struct gensec_security *gensec_security,
1013 TALLOC_CTX *out_mem_ctx,
1014 struct tevent_context *ev,
1015 struct spnego_data *spnego_in,
1016 DATA_BLOB *out)
1018 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1019 DATA_BLOB mech_list_mic = data_blob_null;
1020 DATA_BLOB unwrapped_out = data_blob_null;
1022 /* and switch into the state machine */
1024 switch (spnego_state->state_position) {
1025 case SPNEGO_SERVER_START:
1027 NTSTATUS nt_status;
1029 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
1030 spnego_state,
1031 out_mem_ctx,
1033 spnego_in,
1034 &unwrapped_out);
1036 if (spnego_state->simulate_w2k) {
1038 * Windows 2000 returns the unwrapped token
1039 * also in the mech_list_mic field.
1041 * In order to verify our client code,
1042 * we need a way to have a server with this
1043 * broken behaviour
1045 mech_list_mic = unwrapped_out;
1048 nt_status = gensec_spnego_server_response(spnego_state,
1049 out_mem_ctx,
1050 nt_status,
1051 unwrapped_out,
1052 mech_list_mic,
1053 out);
1055 return nt_status;
1058 case SPNEGO_SERVER_TARG:
1060 NTSTATUS nt_status;
1061 bool have_sign = true;
1062 bool new_spnego = false;
1064 spnego_state->num_targs++;
1066 if (!spnego_state->sub_sec_security) {
1067 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1068 return NT_STATUS_INVALID_PARAMETER;
1071 if (spnego_state->needs_mic_check) {
1072 if (spnego_in->negTokenTarg.responseToken.length != 0) {
1073 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
1074 return NT_STATUS_INVALID_PARAMETER;
1077 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1078 spnego_state->mech_types.data,
1079 spnego_state->mech_types.length,
1080 spnego_state->mech_types.data,
1081 spnego_state->mech_types.length,
1082 &spnego_in->negTokenTarg.mechListMIC);
1083 if (NT_STATUS_IS_OK(nt_status)) {
1084 spnego_state->needs_mic_check = false;
1085 spnego_state->done_mic_check = true;
1086 } else {
1087 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1088 nt_errstr(nt_status)));
1090 goto server_response;
1093 if (!spnego_state->sub_sec_ready) {
1094 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
1095 out_mem_ctx, ev,
1096 spnego_in->negTokenTarg.responseToken,
1097 &unwrapped_out);
1098 if (NT_STATUS_IS_OK(nt_status)) {
1099 spnego_state->sub_sec_ready = true;
1101 if (!NT_STATUS_IS_OK(nt_status)) {
1102 goto server_response;
1104 } else {
1105 nt_status = NT_STATUS_OK;
1108 have_sign = gensec_have_feature(spnego_state->sub_sec_security,
1109 GENSEC_FEATURE_SIGN);
1110 if (spnego_state->simulate_w2k) {
1111 have_sign = false;
1113 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1114 GENSEC_FEATURE_NEW_SPNEGO);
1115 if (spnego_in->negTokenTarg.mechListMIC.length > 0) {
1116 new_spnego = true;
1119 if (have_sign && new_spnego) {
1120 spnego_state->needs_mic_check = true;
1121 spnego_state->needs_mic_sign = true;
1124 if (have_sign && spnego_in->negTokenTarg.mechListMIC.length > 0) {
1125 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1126 spnego_state->mech_types.data,
1127 spnego_state->mech_types.length,
1128 spnego_state->mech_types.data,
1129 spnego_state->mech_types.length,
1130 &spnego_in->negTokenTarg.mechListMIC);
1131 if (!NT_STATUS_IS_OK(nt_status)) {
1132 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1133 nt_errstr(nt_status)));
1134 goto server_response;
1137 spnego_state->needs_mic_check = false;
1138 spnego_state->done_mic_check = true;
1141 if (spnego_state->needs_mic_sign) {
1142 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1143 out_mem_ctx,
1144 spnego_state->mech_types.data,
1145 spnego_state->mech_types.length,
1146 spnego_state->mech_types.data,
1147 spnego_state->mech_types.length,
1148 &mech_list_mic);
1149 if (!NT_STATUS_IS_OK(nt_status)) {
1150 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1151 nt_errstr(nt_status)));
1152 goto server_response;
1154 spnego_state->needs_mic_sign = false;
1157 if (spnego_state->needs_mic_check) {
1158 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1161 server_response:
1162 nt_status = gensec_spnego_server_response(spnego_state,
1163 out_mem_ctx,
1164 nt_status,
1165 unwrapped_out,
1166 mech_list_mic,
1167 out);
1169 return nt_status;
1172 default:
1173 break;
1176 smb_panic(__location__);
1177 return NT_STATUS_INTERNAL_ERROR;
1180 struct gensec_spnego_update_state {
1181 struct gensec_security *gensec;
1182 struct spnego_state *spnego;
1183 DATA_BLOB full_in;
1184 struct spnego_data _spnego_in;
1185 struct spnego_data *spnego_in;
1186 NTSTATUS status;
1187 DATA_BLOB out;
1190 static void gensec_spnego_update_cleanup(struct tevent_req *req,
1191 enum tevent_req_state req_state)
1193 struct gensec_spnego_update_state *state =
1194 tevent_req_data(req,
1195 struct gensec_spnego_update_state);
1197 switch (req_state) {
1198 case TEVENT_REQ_USER_ERROR:
1199 case TEVENT_REQ_TIMED_OUT:
1200 case TEVENT_REQ_NO_MEMORY:
1202 * A fatal error, further updates are not allowed.
1204 state->spnego->state_position = SPNEGO_DONE;
1205 break;
1206 default:
1207 break;
1211 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1212 const DATA_BLOB in, TALLOC_CTX *mem_ctx,
1213 DATA_BLOB *full_in);
1214 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1215 TALLOC_CTX *out_mem_ctx,
1216 DATA_BLOB *_out);
1218 static struct tevent_req *gensec_spnego_update_send(TALLOC_CTX *mem_ctx,
1219 struct tevent_context *ev,
1220 struct gensec_security *gensec_security,
1221 const DATA_BLOB in)
1223 struct spnego_state *spnego_state =
1224 talloc_get_type_abort(gensec_security->private_data,
1225 struct spnego_state);
1226 struct tevent_req *req = NULL;
1227 struct gensec_spnego_update_state *state = NULL;
1228 NTSTATUS status;
1229 ssize_t len;
1231 req = tevent_req_create(mem_ctx, &state,
1232 struct gensec_spnego_update_state);
1233 if (req == NULL) {
1234 return NULL;
1236 state->gensec = gensec_security;
1237 state->spnego = spnego_state;
1238 tevent_req_set_cleanup_fn(req, gensec_spnego_update_cleanup);
1240 if (spnego_state->out_frag.length > 0) {
1241 if (in.length > 0) {
1242 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1243 return tevent_req_post(req, ev);
1246 status = gensec_spnego_update_out(gensec_security,
1247 state, &state->out);
1248 state->status = status;
1249 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1250 tevent_req_done(req);
1251 return tevent_req_post(req, ev);
1253 if (tevent_req_nterror(req, status)) {
1254 return tevent_req_post(req, ev);
1257 tevent_req_done(req);
1258 return tevent_req_post(req, ev);
1261 status = gensec_spnego_update_in(gensec_security, in,
1262 state, &state->full_in);
1263 state->status = status;
1264 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1265 tevent_req_done(req);
1266 return tevent_req_post(req, ev);
1268 if (tevent_req_nterror(req, status)) {
1269 return tevent_req_post(req, ev);
1272 /* Check if we got a valid SPNEGO blob... */
1274 switch (spnego_state->state_position) {
1275 case SPNEGO_FALLBACK:
1276 break;
1278 case SPNEGO_CLIENT_TARG:
1279 case SPNEGO_SERVER_TARG:
1280 if (state->full_in.length == 0) {
1281 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1282 return tevent_req_post(req, ev);
1285 /* fall through */
1286 case SPNEGO_CLIENT_START:
1287 case SPNEGO_SERVER_START:
1289 if (state->full_in.length == 0) {
1290 /* create_negTokenInit later */
1291 break;
1294 len = spnego_read_data(state,
1295 state->full_in,
1296 &state->_spnego_in);
1297 if (len == -1) {
1298 if (spnego_state->state_position != SPNEGO_SERVER_START) {
1299 DEBUG(1, ("Invalid SPNEGO request:\n"));
1300 dump_data(1, state->full_in.data,
1301 state->full_in.length);
1302 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1303 return tevent_req_post(req, ev);
1307 * This is the 'fallback' case, where we don't get
1308 * SPNEGO, and have to try all the other options (and
1309 * hope they all have a magic string they check)
1311 status = gensec_spnego_server_try_fallback(gensec_security,
1312 spnego_state,
1313 state,
1314 state->full_in);
1315 if (tevent_req_nterror(req, status)) {
1316 return tevent_req_post(req, ev);
1320 * We'll continue with SPNEGO_FALLBACK below...
1322 break;
1324 state->spnego_in = &state->_spnego_in;
1326 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1327 if (state->spnego_in->type != spnego_state->expected_packet) {
1328 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n",
1329 state->spnego_in->type,
1330 spnego_state->expected_packet));
1331 dump_data(1, state->full_in.data,
1332 state->full_in.length);
1333 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1334 return tevent_req_post(req, ev);
1337 break;
1339 default:
1340 smb_panic(__location__);
1341 return NULL;
1344 /* and switch into the state machine */
1346 switch (spnego_state->state_position) {
1347 case SPNEGO_FALLBACK:
1348 status = gensec_update_ev(spnego_state->sub_sec_security,
1349 state, ev,
1350 state->full_in,
1351 &spnego_state->out_frag);
1352 break;
1354 case SPNEGO_CLIENT_START:
1355 if (state->spnego_in == NULL) {
1356 /* client to produce negTokenInit */
1357 status = gensec_spnego_create_negTokenInit(gensec_security,
1358 spnego_state, state, ev,
1359 &spnego_state->out_frag);
1360 break;
1363 /* fall through */
1364 case SPNEGO_CLIENT_TARG:
1365 status = gensec_spnego_update_client(gensec_security,
1366 state, ev,
1367 state->spnego_in,
1368 &spnego_state->out_frag);
1369 break;
1371 case SPNEGO_SERVER_START:
1372 if (state->spnego_in == NULL) {
1373 /* server to produce negTokenInit */
1374 status = gensec_spnego_create_negTokenInit(gensec_security,
1375 spnego_state, state, ev,
1376 &spnego_state->out_frag);
1377 break;
1380 /* fall through */
1381 case SPNEGO_SERVER_TARG:
1382 status = gensec_spnego_update_server(gensec_security,
1383 state, ev,
1384 state->spnego_in,
1385 &spnego_state->out_frag);
1386 break;
1388 default:
1389 smb_panic(__location__);
1390 return NULL;
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,
1399 reset_full);
1401 if (!NT_STATUS_IS_OK(status) &&
1402 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1403 tevent_req_nterror(req, status);
1404 return tevent_req_post(req, ev);
1407 spnego_state->out_status = status;
1409 status = gensec_spnego_update_out(gensec_security,
1410 state, &state->out);
1411 state->status = status;
1412 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1413 tevent_req_done(req);
1414 return tevent_req_post(req, ev);
1416 if (tevent_req_nterror(req, status)) {
1417 return tevent_req_post(req, ev);
1420 tevent_req_done(req);
1421 return tevent_req_post(req, ev);
1424 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1425 const DATA_BLOB in, TALLOC_CTX *mem_ctx,
1426 DATA_BLOB *full_in)
1428 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1429 size_t expected;
1430 bool ok;
1432 *full_in = data_blob_null;
1434 switch (spnego_state->state_position) {
1435 case SPNEGO_FALLBACK:
1436 *full_in = in;
1437 spnego_state->in_needed = 0;
1438 return NT_STATUS_OK;
1440 case SPNEGO_CLIENT_START:
1441 case SPNEGO_CLIENT_TARG:
1442 case SPNEGO_SERVER_START:
1443 case SPNEGO_SERVER_TARG:
1444 break;
1446 case SPNEGO_DONE:
1447 default:
1448 return NT_STATUS_INVALID_PARAMETER;
1451 if (spnego_state->in_needed == 0) {
1452 size_t size = 0;
1453 int ret;
1456 * try to work out the size of the full
1457 * input token, it might be fragmented
1459 ret = asn1_peek_full_tag(in, ASN1_APPLICATION(0), &size);
1460 if ((ret != 0) && (ret != EAGAIN)) {
1461 ret = asn1_peek_full_tag(in, ASN1_CONTEXT(1), &size);
1464 if ((ret == 0) || (ret == EAGAIN)) {
1465 spnego_state->in_needed = size;
1466 } else {
1468 * If it is not an asn1 message
1469 * just call the next layer.
1471 spnego_state->in_needed = in.length;
1475 if (spnego_state->in_needed > UINT16_MAX) {
1477 * limit the incoming message to 0xFFFF
1478 * to avoid DoS attacks.
1480 return NT_STATUS_INVALID_BUFFER_SIZE;
1483 if ((spnego_state->in_needed > 0) && (in.length == 0)) {
1485 * If we reach this, we know we got at least
1486 * part of an asn1 message, getting 0 means
1487 * the remote peer wants us to spin.
1489 return NT_STATUS_INVALID_PARAMETER;
1492 expected = spnego_state->in_needed - spnego_state->in_frag.length;
1493 if (in.length > expected) {
1495 * we got more than expected
1497 return NT_STATUS_INVALID_PARAMETER;
1500 if (in.length == spnego_state->in_needed) {
1502 * if the in.length contains the full blob
1503 * we are done.
1505 * Note: this implies spnego_state->in_frag.length == 0,
1506 * but we do not need to check this explicitly
1507 * because we already know that we did not get
1508 * more than expected.
1510 *full_in = in;
1511 spnego_state->in_needed = 0;
1512 return NT_STATUS_OK;
1515 ok = data_blob_append(spnego_state, &spnego_state->in_frag,
1516 in.data, in.length);
1517 if (!ok) {
1518 return NT_STATUS_NO_MEMORY;
1521 if (spnego_state->in_needed > spnego_state->in_frag.length) {
1522 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1525 *full_in = spnego_state->in_frag;
1526 talloc_steal(mem_ctx, full_in->data);
1527 spnego_state->in_frag = data_blob_null;
1528 spnego_state->in_needed = 0;
1529 return NT_STATUS_OK;
1532 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1533 TALLOC_CTX *out_mem_ctx,
1534 DATA_BLOB *_out)
1536 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1537 DATA_BLOB out = data_blob_null;
1538 bool ok;
1540 *_out = data_blob_null;
1542 if (spnego_state->out_frag.length <= spnego_state->out_max_length) {
1544 * Fast path, we can deliver everything
1547 *_out = spnego_state->out_frag;
1548 if (spnego_state->out_frag.length > 0) {
1549 talloc_steal(out_mem_ctx, _out->data);
1550 spnego_state->out_frag = data_blob_null;
1553 if (!NT_STATUS_IS_OK(spnego_state->out_status)) {
1554 return spnego_state->out_status;
1558 * We're completely done, further updates are not allowed.
1560 spnego_state->state_position = SPNEGO_DONE;
1561 return gensec_child_ready(gensec_security,
1562 spnego_state->sub_sec_security);
1565 out = spnego_state->out_frag;
1568 * copy the remaining bytes
1570 spnego_state->out_frag = data_blob_talloc(spnego_state,
1571 out.data + spnego_state->out_max_length,
1572 out.length - spnego_state->out_max_length);
1573 if (spnego_state->out_frag.data == NULL) {
1574 return NT_STATUS_NO_MEMORY;
1578 * truncate the buffer
1580 ok = data_blob_realloc(spnego_state, &out,
1581 spnego_state->out_max_length);
1582 if (!ok) {
1583 return NT_STATUS_NO_MEMORY;
1586 talloc_steal(out_mem_ctx, out.data);
1587 *_out = out;
1588 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1591 static NTSTATUS gensec_spnego_update_recv(struct tevent_req *req,
1592 TALLOC_CTX *out_mem_ctx,
1593 DATA_BLOB *out)
1595 struct gensec_spnego_update_state *state =
1596 tevent_req_data(req,
1597 struct gensec_spnego_update_state);
1598 NTSTATUS status;
1600 *out = data_blob_null;
1602 if (tevent_req_is_nterror(req, &status)) {
1603 tevent_req_received(req);
1604 return status;
1607 *out = state->out;
1608 talloc_steal(out_mem_ctx, state->out.data);
1609 status = state->status;
1610 tevent_req_received(req);
1611 return status;
1614 static const char *gensec_spnego_oids[] = {
1615 GENSEC_OID_SPNEGO,
1616 NULL
1619 static const struct gensec_security_ops gensec_spnego_security_ops = {
1620 .name = "spnego",
1621 .sasl_name = "GSS-SPNEGO",
1622 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1623 .oid = gensec_spnego_oids,
1624 .client_start = gensec_spnego_client_start,
1625 .server_start = gensec_spnego_server_start,
1626 .update_send = gensec_spnego_update_send,
1627 .update_recv = gensec_spnego_update_recv,
1628 .seal_packet = gensec_child_seal_packet,
1629 .sign_packet = gensec_child_sign_packet,
1630 .sig_size = gensec_child_sig_size,
1631 .max_wrapped_size = gensec_child_max_wrapped_size,
1632 .max_input_size = gensec_child_max_input_size,
1633 .check_packet = gensec_child_check_packet,
1634 .unseal_packet = gensec_child_unseal_packet,
1635 .wrap = gensec_child_wrap,
1636 .unwrap = gensec_child_unwrap,
1637 .session_key = gensec_child_session_key,
1638 .session_info = gensec_child_session_info,
1639 .want_feature = gensec_child_want_feature,
1640 .have_feature = gensec_child_have_feature,
1641 .expire_time = gensec_child_expire_time,
1642 .final_auth_type = gensec_child_final_auth_type,
1643 .enabled = true,
1644 .priority = GENSEC_SPNEGO
1647 _PUBLIC_ NTSTATUS gensec_spnego_init(TALLOC_CTX *ctx)
1649 NTSTATUS ret;
1650 ret = gensec_register(ctx, &gensec_spnego_security_ops);
1651 if (!NT_STATUS_IS_OK(ret)) {
1652 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1653 gensec_spnego_security_ops.name));
1654 return ret;
1657 return ret;