lib: Align nt_time_to_unix_timespec with unix_timespec_to_nt_time
[Samba.git] / auth / gensec / spnego.c
blob20cacdb9b451d477a2bf486d11d09de0b4956525
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 "../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_internal.h"
31 #include "param/param.h"
32 #include "lib/util/asn1.h"
34 #undef strcasecmp
36 _PUBLIC_ NTSTATUS gensec_spnego_init(void);
38 enum spnego_state_position {
39 SPNEGO_SERVER_START,
40 SPNEGO_CLIENT_START,
41 SPNEGO_SERVER_TARG,
42 SPNEGO_CLIENT_TARG,
43 SPNEGO_FALLBACK,
44 SPNEGO_DONE
47 struct spnego_state {
48 enum spnego_message_type expected_packet;
49 enum spnego_state_position state_position;
50 struct gensec_security *sub_sec_security;
51 bool no_response_expected;
53 const char *neg_oid;
55 DATA_BLOB mech_types;
58 * The following is used to implement
59 * the update token fragmentation
61 size_t in_needed;
62 DATA_BLOB in_frag;
63 size_t out_max_length;
64 DATA_BLOB out_frag;
65 NTSTATUS out_status;
69 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
71 struct spnego_state *spnego_state;
73 spnego_state = talloc_zero(gensec_security, struct spnego_state);
74 if (!spnego_state) {
75 return NT_STATUS_NO_MEMORY;
78 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
79 spnego_state->state_position = SPNEGO_CLIENT_START;
80 spnego_state->sub_sec_security = NULL;
81 spnego_state->no_response_expected = false;
82 spnego_state->mech_types = data_blob(NULL, 0);
83 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
84 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
86 gensec_security->private_data = spnego_state;
87 return NT_STATUS_OK;
90 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
92 struct spnego_state *spnego_state;
94 spnego_state = talloc_zero(gensec_security, struct spnego_state);
95 if (!spnego_state) {
96 return NT_STATUS_NO_MEMORY;
99 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
100 spnego_state->state_position = SPNEGO_SERVER_START;
101 spnego_state->sub_sec_security = NULL;
102 spnego_state->no_response_expected = false;
103 spnego_state->mech_types = data_blob(NULL, 0);
104 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
105 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
107 gensec_security->private_data = spnego_state;
108 return NT_STATUS_OK;
112 wrappers for the spnego_*() functions
114 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security,
115 uint8_t *data, size_t length,
116 const uint8_t *whole_pdu, size_t pdu_length,
117 const DATA_BLOB *sig)
119 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
121 if (spnego_state->state_position != SPNEGO_DONE
122 && spnego_state->state_position != SPNEGO_FALLBACK) {
123 return NT_STATUS_INVALID_PARAMETER;
126 return gensec_unseal_packet(spnego_state->sub_sec_security,
127 data, length,
128 whole_pdu, pdu_length,
129 sig);
132 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security,
133 const uint8_t *data, size_t length,
134 const uint8_t *whole_pdu, size_t pdu_length,
135 const DATA_BLOB *sig)
137 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
139 if (spnego_state->state_position != SPNEGO_DONE
140 && spnego_state->state_position != SPNEGO_FALLBACK) {
141 return NT_STATUS_INVALID_PARAMETER;
144 return gensec_check_packet(spnego_state->sub_sec_security,
145 data, length,
146 whole_pdu, pdu_length,
147 sig);
150 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security,
151 TALLOC_CTX *mem_ctx,
152 uint8_t *data, size_t length,
153 const uint8_t *whole_pdu, size_t pdu_length,
154 DATA_BLOB *sig)
156 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
158 if (spnego_state->state_position != SPNEGO_DONE
159 && spnego_state->state_position != SPNEGO_FALLBACK) {
160 return NT_STATUS_INVALID_PARAMETER;
163 return gensec_seal_packet(spnego_state->sub_sec_security,
164 mem_ctx,
165 data, length,
166 whole_pdu, pdu_length,
167 sig);
170 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security,
171 TALLOC_CTX *mem_ctx,
172 const uint8_t *data, size_t length,
173 const uint8_t *whole_pdu, size_t pdu_length,
174 DATA_BLOB *sig)
176 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
178 if (spnego_state->state_position != SPNEGO_DONE
179 && spnego_state->state_position != SPNEGO_FALLBACK) {
180 return NT_STATUS_INVALID_PARAMETER;
183 return gensec_sign_packet(spnego_state->sub_sec_security,
184 mem_ctx,
185 data, length,
186 whole_pdu, pdu_length,
187 sig);
190 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security,
191 TALLOC_CTX *mem_ctx,
192 const DATA_BLOB *in,
193 DATA_BLOB *out)
195 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
197 if (spnego_state->state_position != SPNEGO_DONE
198 && spnego_state->state_position != SPNEGO_FALLBACK) {
199 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
200 return NT_STATUS_INVALID_PARAMETER;
203 return gensec_wrap(spnego_state->sub_sec_security,
204 mem_ctx, in, out);
207 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security,
208 TALLOC_CTX *mem_ctx,
209 const DATA_BLOB *in,
210 DATA_BLOB *out)
212 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
214 if (spnego_state->state_position != SPNEGO_DONE
215 && spnego_state->state_position != SPNEGO_FALLBACK) {
216 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
217 return NT_STATUS_INVALID_PARAMETER;
220 return gensec_unwrap(spnego_state->sub_sec_security,
221 mem_ctx, in, out);
224 static NTSTATUS gensec_spnego_wrap_packets(struct gensec_security *gensec_security,
225 TALLOC_CTX *mem_ctx,
226 const DATA_BLOB *in,
227 DATA_BLOB *out,
228 size_t *len_processed)
230 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
232 if (spnego_state->state_position != SPNEGO_DONE
233 && spnego_state->state_position != SPNEGO_FALLBACK) {
234 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
235 return NT_STATUS_INVALID_PARAMETER;
238 return gensec_wrap_packets(spnego_state->sub_sec_security,
239 mem_ctx, in, out,
240 len_processed);
243 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security,
244 DATA_BLOB blob, size_t *size)
246 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
248 if (spnego_state->state_position != SPNEGO_DONE
249 && spnego_state->state_position != SPNEGO_FALLBACK) {
250 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
251 return NT_STATUS_INVALID_PARAMETER;
254 return gensec_packet_full_request(spnego_state->sub_sec_security,
255 blob, size);
258 static NTSTATUS gensec_spnego_unwrap_packets(struct gensec_security *gensec_security,
259 TALLOC_CTX *mem_ctx,
260 const DATA_BLOB *in,
261 DATA_BLOB *out,
262 size_t *len_processed)
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) {
268 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
269 return NT_STATUS_INVALID_PARAMETER;
272 return gensec_unwrap_packets(spnego_state->sub_sec_security,
273 mem_ctx, in, out,
274 len_processed);
277 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size)
279 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
281 if (spnego_state->state_position != SPNEGO_DONE
282 && spnego_state->state_position != SPNEGO_FALLBACK) {
283 return 0;
286 return gensec_sig_size(spnego_state->sub_sec_security, data_size);
289 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security)
291 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
293 if (spnego_state->state_position != SPNEGO_DONE
294 && spnego_state->state_position != SPNEGO_FALLBACK) {
295 return 0;
298 return gensec_max_input_size(spnego_state->sub_sec_security);
301 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security)
303 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
305 if (spnego_state->state_position != SPNEGO_DONE
306 && spnego_state->state_position != SPNEGO_FALLBACK) {
307 return 0;
310 return gensec_max_wrapped_size(spnego_state->sub_sec_security);
313 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security,
314 TALLOC_CTX *mem_ctx,
315 DATA_BLOB *session_key)
317 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
318 if (!spnego_state->sub_sec_security) {
319 return NT_STATUS_INVALID_PARAMETER;
322 return gensec_session_key(spnego_state->sub_sec_security,
323 mem_ctx,
324 session_key);
327 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
328 TALLOC_CTX *mem_ctx,
329 struct auth_session_info **session_info)
331 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
332 if (!spnego_state->sub_sec_security) {
333 return NT_STATUS_INVALID_PARAMETER;
336 return gensec_session_info(spnego_state->sub_sec_security,
337 mem_ctx,
338 session_info);
341 /** Fallback to another GENSEC mechanism, based on magic strings
343 * This is the 'fallback' case, where we don't get SPNEGO, and have to
344 * try all the other options (and hope they all have a magic string
345 * they check)
348 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
349 struct spnego_state *spnego_state,
350 struct tevent_context *ev,
351 TALLOC_CTX *out_mem_ctx,
352 const DATA_BLOB in, DATA_BLOB *out)
354 int i,j;
355 const struct gensec_security_ops **all_ops;
357 all_ops = gensec_security_mechs(gensec_security, out_mem_ctx);
359 for (i=0; all_ops && all_ops[i]; i++) {
360 bool is_spnego;
361 NTSTATUS nt_status;
363 if (gensec_security != NULL &&
364 !gensec_security_ops_enabled(all_ops[i], gensec_security))
365 continue;
367 if (!all_ops[i]->oid) {
368 continue;
371 is_spnego = false;
372 for (j=0; all_ops[i]->oid[j]; j++) {
373 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
374 is_spnego = true;
377 if (is_spnego) {
378 continue;
381 if (!all_ops[i]->magic) {
382 continue;
385 nt_status = all_ops[i]->magic(gensec_security, &in);
386 if (!NT_STATUS_IS_OK(nt_status)) {
387 continue;
390 spnego_state->state_position = SPNEGO_FALLBACK;
392 nt_status = gensec_subcontext_start(spnego_state,
393 gensec_security,
394 &spnego_state->sub_sec_security);
396 if (!NT_STATUS_IS_OK(nt_status)) {
397 return nt_status;
399 /* select the sub context */
400 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
401 all_ops[i]);
402 if (!NT_STATUS_IS_OK(nt_status)) {
403 return nt_status;
405 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
406 ev, out_mem_ctx, in, out);
407 return nt_status;
409 DEBUG(1, ("Failed to parse SPNEGO request\n"));
410 return NT_STATUS_INVALID_PARAMETER;
414 Parse the netTokenInit, either from the client, to the server, or
415 from the server to the client.
418 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
419 struct spnego_state *spnego_state,
420 TALLOC_CTX *out_mem_ctx,
421 struct tevent_context *ev,
422 const char * const *mechType,
423 const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out)
425 int i;
426 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
427 DATA_BLOB null_data_blob = data_blob(NULL,0);
428 bool ok;
430 const struct gensec_security_ops_wrapper *all_sec
431 = gensec_security_by_oid_list(gensec_security,
432 out_mem_ctx,
433 mechType,
434 GENSEC_OID_SPNEGO);
436 ok = spnego_write_mech_types(spnego_state,
437 mechType,
438 &spnego_state->mech_types);
439 if (!ok) {
440 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
441 return NT_STATUS_NO_MEMORY;
444 if (spnego_state->state_position == SPNEGO_SERVER_START) {
445 uint32_t j;
446 for (j=0; mechType && mechType[j]; j++) {
447 for (i=0; all_sec && all_sec[i].op; i++) {
448 if (strcmp(mechType[j], all_sec[i].oid) != 0) {
449 continue;
452 nt_status = gensec_subcontext_start(spnego_state,
453 gensec_security,
454 &spnego_state->sub_sec_security);
455 if (!NT_STATUS_IS_OK(nt_status)) {
456 return nt_status;
458 /* select the sub context */
459 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
460 all_sec[i].op);
461 if (!NT_STATUS_IS_OK(nt_status)) {
462 talloc_free(spnego_state->sub_sec_security);
463 spnego_state->sub_sec_security = NULL;
464 break;
467 if (j > 0) {
468 /* no optimistic token */
469 spnego_state->neg_oid = all_sec[i].oid;
470 *unwrapped_out = data_blob_null;
471 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
472 break;
475 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
476 out_mem_ctx,
478 unwrapped_in,
479 unwrapped_out);
480 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
481 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
482 /* Pretend we never started it (lets the first run find some incompatible demand) */
484 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
485 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
486 talloc_free(spnego_state->sub_sec_security);
487 spnego_state->sub_sec_security = NULL;
488 break;
491 spnego_state->neg_oid = all_sec[i].oid;
492 break;
494 if (spnego_state->sub_sec_security) {
495 break;
499 if (!spnego_state->sub_sec_security) {
500 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
501 return NT_STATUS_INVALID_PARAMETER;
505 /* Having tried any optimistic token from the client (if we
506 * were the server), if we didn't get anywhere, walk our list
507 * in our preference order */
509 if (!spnego_state->sub_sec_security) {
510 for (i=0; all_sec && all_sec[i].op; i++) {
511 nt_status = gensec_subcontext_start(spnego_state,
512 gensec_security,
513 &spnego_state->sub_sec_security);
514 if (!NT_STATUS_IS_OK(nt_status)) {
515 return nt_status;
517 /* select the sub context */
518 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
519 all_sec[i].op);
520 if (!NT_STATUS_IS_OK(nt_status)) {
521 talloc_free(spnego_state->sub_sec_security);
522 spnego_state->sub_sec_security = NULL;
523 continue;
526 spnego_state->neg_oid = all_sec[i].oid;
528 /* only get the helping start blob for the first OID */
529 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
530 out_mem_ctx,
532 null_data_blob,
533 unwrapped_out);
535 /* it is likely that a NULL input token will
536 * not be liked by most server mechs, but if
537 * we are in the client, we want the first
538 * update packet to be able to abort the use
539 * of this mech */
540 if (spnego_state->state_position != SPNEGO_SERVER_START) {
541 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
542 NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
543 NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
544 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
545 /* Pretend we never started it (lets the first run find some incompatible demand) */
547 DEBUG(3, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
548 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
549 talloc_free(spnego_state->sub_sec_security);
550 spnego_state->sub_sec_security = NULL;
551 continue;
555 break;
559 if (spnego_state->sub_sec_security) {
560 /* it is likely that a NULL input token will
561 * not be liked by most server mechs, but this
562 * does the right thing in the CIFS client.
563 * just push us along the merry-go-round
564 * again, and hope for better luck next
565 * time */
567 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
568 *unwrapped_out = data_blob(NULL, 0);
569 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
572 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
573 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
574 && !NT_STATUS_IS_OK(nt_status)) {
575 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
576 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
577 talloc_free(spnego_state->sub_sec_security);
578 spnego_state->sub_sec_security = NULL;
580 /* We started the mech correctly, and the
581 * input from the other side was valid.
582 * Return the error (say bad password, invalid
583 * ticket) */
584 return nt_status;
587 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
590 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
591 /* we could re-negotiate here, but it would only work
592 * if the client or server lied about what it could
593 * support the first time. Lets keep this code to
594 * reality */
596 return nt_status;
599 /** create a negTokenInit
601 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
603 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
604 struct spnego_state *spnego_state,
605 TALLOC_CTX *out_mem_ctx,
606 struct tevent_context *ev,
607 const DATA_BLOB in, DATA_BLOB *out)
609 int i;
610 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
611 DATA_BLOB null_data_blob = data_blob(NULL,0);
612 const char **mechTypes = NULL;
613 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
614 const struct gensec_security_ops_wrapper *all_sec;
616 mechTypes = gensec_security_oids(gensec_security,
617 out_mem_ctx, GENSEC_OID_SPNEGO);
619 all_sec = gensec_security_by_oid_list(gensec_security,
620 out_mem_ctx,
621 mechTypes,
622 GENSEC_OID_SPNEGO);
623 for (i=0; all_sec && all_sec[i].op; i++) {
624 struct spnego_data spnego_out;
625 const char **send_mech_types;
626 bool ok;
628 nt_status = gensec_subcontext_start(spnego_state,
629 gensec_security,
630 &spnego_state->sub_sec_security);
631 if (!NT_STATUS_IS_OK(nt_status)) {
632 return nt_status;
634 /* select the sub context */
635 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
636 all_sec[i].op);
637 if (!NT_STATUS_IS_OK(nt_status)) {
638 talloc_free(spnego_state->sub_sec_security);
639 spnego_state->sub_sec_security = NULL;
640 continue;
643 /* In the client, try and produce the first (optimistic) packet */
644 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
645 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
646 out_mem_ctx,
648 null_data_blob,
649 &unwrapped_out);
651 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
652 && !NT_STATUS_IS_OK(nt_status)) {
653 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
654 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
655 talloc_free(spnego_state->sub_sec_security);
656 spnego_state->sub_sec_security = NULL;
657 /* Pretend we never started it (lets the first run find some incompatible demand) */
659 continue;
663 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
665 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
666 &all_sec[i]);
668 ok = spnego_write_mech_types(spnego_state,
669 send_mech_types,
670 &spnego_state->mech_types);
671 if (!ok) {
672 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
673 return NT_STATUS_NO_MEMORY;
676 /* List the remaining mechs as options */
677 spnego_out.negTokenInit.mechTypes = send_mech_types;
678 spnego_out.negTokenInit.reqFlags = null_data_blob;
679 spnego_out.negTokenInit.reqFlagsPadding = 0;
681 if (spnego_state->state_position == SPNEGO_SERVER_START) {
682 spnego_out.negTokenInit.mechListMIC
683 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
684 } else {
685 spnego_out.negTokenInit.mechListMIC = null_data_blob;
688 spnego_out.negTokenInit.mechToken = unwrapped_out;
690 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
691 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
692 return NT_STATUS_INVALID_PARAMETER;
695 /* set next state */
696 spnego_state->neg_oid = all_sec[i].oid;
698 if (NT_STATUS_IS_OK(nt_status)) {
699 spnego_state->no_response_expected = true;
702 return NT_STATUS_MORE_PROCESSING_REQUIRED;
704 talloc_free(spnego_state->sub_sec_security);
705 spnego_state->sub_sec_security = NULL;
707 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
708 return nt_status;
712 /** create a server negTokenTarg
714 * This is the case, where the client is the first one who sends data
717 static NTSTATUS gensec_spnego_server_negTokenTarg(struct spnego_state *spnego_state,
718 TALLOC_CTX *out_mem_ctx,
719 NTSTATUS nt_status,
720 const DATA_BLOB unwrapped_out,
721 DATA_BLOB mech_list_mic,
722 DATA_BLOB *out)
724 struct spnego_data spnego_out;
725 DATA_BLOB null_data_blob = data_blob(NULL, 0);
727 /* compose reply */
728 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
729 spnego_out.negTokenTarg.responseToken = unwrapped_out;
730 spnego_out.negTokenTarg.mechListMIC = null_data_blob;
731 spnego_out.negTokenTarg.supportedMech = NULL;
733 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
734 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
735 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
736 spnego_state->state_position = SPNEGO_SERVER_TARG;
737 } else if (NT_STATUS_IS_OK(nt_status)) {
738 if (unwrapped_out.data) {
739 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
741 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
742 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
743 spnego_state->state_position = SPNEGO_DONE;
744 } else {
745 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
746 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
747 spnego_state->state_position = SPNEGO_DONE;
750 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
751 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
752 return NT_STATUS_INVALID_PARAMETER;
755 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
757 return nt_status;
761 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
762 struct tevent_context *ev,
763 const DATA_BLOB in, DATA_BLOB *out)
765 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
766 DATA_BLOB null_data_blob = data_blob(NULL, 0);
767 DATA_BLOB mech_list_mic = data_blob(NULL, 0);
768 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
769 struct spnego_data spnego_out;
770 struct spnego_data spnego;
772 ssize_t len;
774 *out = data_blob(NULL, 0);
776 if (!out_mem_ctx) {
777 out_mem_ctx = spnego_state;
780 /* and switch into the state machine */
782 switch (spnego_state->state_position) {
783 case SPNEGO_FALLBACK:
784 return gensec_update_ev(spnego_state->sub_sec_security, ev,
785 out_mem_ctx, in, out);
786 case SPNEGO_SERVER_START:
788 NTSTATUS nt_status;
789 if (in.length) {
791 len = spnego_read_data(gensec_security, in, &spnego);
792 if (len == -1) {
793 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
794 out_mem_ctx, ev, in, out);
796 /* client sent NegTargetInit, we send NegTokenTarg */
798 /* OK, so it's real SPNEGO, check the packet's the one we expect */
799 if (spnego.type != spnego_state->expected_packet) {
800 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
801 spnego_state->expected_packet));
802 dump_data(1, in.data, in.length);
803 spnego_free_data(&spnego);
804 return NT_STATUS_INVALID_PARAMETER;
807 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
808 spnego_state,
809 out_mem_ctx,
811 spnego.negTokenInit.mechTypes,
812 spnego.negTokenInit.mechToken,
813 &unwrapped_out);
815 nt_status = gensec_spnego_server_negTokenTarg(spnego_state,
816 out_mem_ctx,
817 nt_status,
818 unwrapped_out,
819 null_data_blob,
820 out);
822 spnego_free_data(&spnego);
824 return nt_status;
825 } else {
826 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
827 out_mem_ctx, ev, in, out);
828 spnego_state->state_position = SPNEGO_SERVER_START;
829 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
830 return nt_status;
834 case SPNEGO_CLIENT_START:
836 /* The server offers a list of mechanisms */
838 const char *my_mechs[] = {NULL, NULL};
839 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
841 if (!in.length) {
842 /* client to produce negTokenInit */
843 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
844 out_mem_ctx, ev, in, out);
845 spnego_state->state_position = SPNEGO_CLIENT_TARG;
846 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
847 return nt_status;
850 len = spnego_read_data(gensec_security, in, &spnego);
852 if (len == -1) {
853 DEBUG(1, ("Invalid SPNEGO request:\n"));
854 dump_data(1, in.data, in.length);
855 return NT_STATUS_INVALID_PARAMETER;
858 /* OK, so it's real SPNEGO, check the packet's the one we expect */
859 if (spnego.type != spnego_state->expected_packet) {
860 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
861 spnego_state->expected_packet));
862 dump_data(1, in.data, in.length);
863 spnego_free_data(&spnego);
864 return NT_STATUS_INVALID_PARAMETER;
867 if (spnego.negTokenInit.targetPrincipal
868 && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
869 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
870 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
871 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
875 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
876 spnego_state,
877 out_mem_ctx,
879 spnego.negTokenInit.mechTypes,
880 spnego.negTokenInit.mechToken,
881 &unwrapped_out);
883 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
884 spnego_free_data(&spnego);
885 return nt_status;
888 my_mechs[0] = spnego_state->neg_oid;
889 /* compose reply */
890 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
891 spnego_out.negTokenInit.mechTypes = my_mechs;
892 spnego_out.negTokenInit.reqFlags = null_data_blob;
893 spnego_out.negTokenInit.reqFlagsPadding = 0;
894 spnego_out.negTokenInit.mechListMIC = null_data_blob;
895 spnego_out.negTokenInit.mechToken = unwrapped_out;
897 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
898 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
899 return NT_STATUS_INVALID_PARAMETER;
902 /* set next state */
903 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
904 spnego_state->state_position = SPNEGO_CLIENT_TARG;
906 if (NT_STATUS_IS_OK(nt_status)) {
907 spnego_state->no_response_expected = true;
910 spnego_free_data(&spnego);
911 return NT_STATUS_MORE_PROCESSING_REQUIRED;
913 case SPNEGO_SERVER_TARG:
915 NTSTATUS nt_status;
916 bool new_spnego = false;
918 if (!in.length) {
919 return NT_STATUS_INVALID_PARAMETER;
922 len = spnego_read_data(gensec_security, in, &spnego);
924 if (len == -1) {
925 DEBUG(1, ("Invalid SPNEGO request:\n"));
926 dump_data(1, in.data, in.length);
927 return NT_STATUS_INVALID_PARAMETER;
930 /* OK, so it's real SPNEGO, check the packet's the one we expect */
931 if (spnego.type != spnego_state->expected_packet) {
932 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
933 spnego_state->expected_packet));
934 dump_data(1, in.data, in.length);
935 spnego_free_data(&spnego);
936 return NT_STATUS_INVALID_PARAMETER;
939 if (!spnego_state->sub_sec_security) {
940 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
941 spnego_free_data(&spnego);
942 return NT_STATUS_INVALID_PARAMETER;
945 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
946 out_mem_ctx, ev,
947 spnego.negTokenTarg.responseToken,
948 &unwrapped_out);
949 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
950 new_spnego = true;
951 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
952 spnego_state->mech_types.data,
953 spnego_state->mech_types.length,
954 spnego_state->mech_types.data,
955 spnego_state->mech_types.length,
956 &spnego.negTokenTarg.mechListMIC);
957 if (!NT_STATUS_IS_OK(nt_status)) {
958 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
959 nt_errstr(nt_status)));
962 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
963 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
964 out_mem_ctx,
965 spnego_state->mech_types.data,
966 spnego_state->mech_types.length,
967 spnego_state->mech_types.data,
968 spnego_state->mech_types.length,
969 &mech_list_mic);
970 if (!NT_STATUS_IS_OK(nt_status)) {
971 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
972 nt_errstr(nt_status)));
976 nt_status = gensec_spnego_server_negTokenTarg(spnego_state,
977 out_mem_ctx,
978 nt_status,
979 unwrapped_out,
980 mech_list_mic,
981 out);
983 spnego_free_data(&spnego);
985 return nt_status;
987 case SPNEGO_CLIENT_TARG:
989 NTSTATUS nt_status;
990 if (!in.length) {
991 return NT_STATUS_INVALID_PARAMETER;
994 len = spnego_read_data(gensec_security, in, &spnego);
996 if (len == -1) {
997 DEBUG(1, ("Invalid SPNEGO request:\n"));
998 dump_data(1, in.data, in.length);
999 return NT_STATUS_INVALID_PARAMETER;
1002 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1003 if (spnego.type != spnego_state->expected_packet) {
1004 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
1005 spnego_state->expected_packet));
1006 dump_data(1, in.data, in.length);
1007 spnego_free_data(&spnego);
1008 return NT_STATUS_INVALID_PARAMETER;
1011 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1012 spnego_free_data(&spnego);
1013 return NT_STATUS_LOGON_FAILURE;
1016 /* Server didn't like our choice of mech, and chose something else */
1017 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
1018 spnego.negTokenTarg.supportedMech &&
1019 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
1020 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
1021 gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech),
1022 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
1024 talloc_free(spnego_state->sub_sec_security);
1025 nt_status = gensec_subcontext_start(spnego_state,
1026 gensec_security,
1027 &spnego_state->sub_sec_security);
1028 if (!NT_STATUS_IS_OK(nt_status)) {
1029 spnego_free_data(&spnego);
1030 return nt_status;
1032 /* select the sub context */
1033 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
1034 spnego.negTokenTarg.supportedMech);
1035 if (!NT_STATUS_IS_OK(nt_status)) {
1036 spnego_free_data(&spnego);
1037 return nt_status;
1040 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
1041 out_mem_ctx, ev,
1042 spnego.negTokenTarg.responseToken,
1043 &unwrapped_out);
1044 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1045 } else if (spnego_state->no_response_expected) {
1046 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1047 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1048 nt_status = NT_STATUS_INVALID_PARAMETER;
1049 } else if (spnego.negTokenTarg.responseToken.length) {
1050 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1051 nt_status = NT_STATUS_INVALID_PARAMETER;
1052 } else {
1053 nt_status = NT_STATUS_OK;
1055 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1056 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1057 spnego_state->mech_types.data,
1058 spnego_state->mech_types.length,
1059 spnego_state->mech_types.data,
1060 spnego_state->mech_types.length,
1061 &spnego.negTokenTarg.mechListMIC);
1062 if (!NT_STATUS_IS_OK(nt_status)) {
1063 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1064 nt_errstr(nt_status)));
1067 } else {
1068 bool new_spnego = false;
1070 nt_status = gensec_update_ev(spnego_state->sub_sec_security,
1071 out_mem_ctx, ev,
1072 spnego.negTokenTarg.responseToken,
1073 &unwrapped_out);
1075 if (NT_STATUS_IS_OK(nt_status)
1076 && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1077 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1078 GENSEC_FEATURE_NEW_SPNEGO);
1080 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1081 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1082 out_mem_ctx,
1083 spnego_state->mech_types.data,
1084 spnego_state->mech_types.length,
1085 spnego_state->mech_types.data,
1086 spnego_state->mech_types.length,
1087 &mech_list_mic);
1088 if (!NT_STATUS_IS_OK(nt_status)) {
1089 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1090 nt_errstr(nt_status)));
1093 if (NT_STATUS_IS_OK(nt_status)) {
1094 spnego_state->no_response_expected = true;
1098 spnego_free_data(&spnego);
1100 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1101 && !NT_STATUS_IS_OK(nt_status)) {
1102 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1103 spnego_state->sub_sec_security->ops->name,
1104 nt_errstr(nt_status)));
1105 return nt_status;
1108 if (unwrapped_out.length || mech_list_mic.length) {
1109 /* compose reply */
1110 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1111 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1112 spnego_out.negTokenTarg.supportedMech = NULL;
1113 spnego_out.negTokenTarg.responseToken = unwrapped_out;
1114 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1116 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1117 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1118 return NT_STATUS_INVALID_PARAMETER;
1121 spnego_state->state_position = SPNEGO_CLIENT_TARG;
1122 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1123 } else {
1125 /* all done - server has accepted, and we agree */
1126 *out = null_data_blob;
1128 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1129 /* unless of course it did not accept */
1130 DEBUG(1,("gensec_update ok but not accepted\n"));
1131 nt_status = NT_STATUS_INVALID_PARAMETER;
1134 spnego_state->state_position = SPNEGO_DONE;
1137 return nt_status;
1139 case SPNEGO_DONE:
1140 /* We should not be called after we are 'done' */
1141 return NT_STATUS_INVALID_PARAMETER;
1143 return NT_STATUS_INVALID_PARAMETER;
1146 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1147 const DATA_BLOB in, DATA_BLOB *full_in)
1149 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1150 size_t expected;
1151 NTSTATUS status;
1152 bool ok;
1154 *full_in = data_blob_null;
1156 if (spnego_state->in_needed == 0) {
1157 size_t size = 0;
1160 * try to work out the size of the full
1161 * input token, it might be fragmented
1163 status = asn1_peek_full_tag(in, ASN1_APPLICATION(0), &size);
1164 if (!NT_STATUS_IS_OK(status) &&
1165 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1166 status = asn1_peek_full_tag(in, ASN1_CONTEXT(1), &size);
1169 if (NT_STATUS_IS_OK(status) ||
1170 NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1171 spnego_state->in_needed = size;
1172 } else {
1174 * If it is not an asn1 message
1175 * just call the next layer.
1177 spnego_state->in_needed = in.length;
1181 if (spnego_state->in_needed > UINT16_MAX) {
1183 * limit the incoming message to 0xFFFF
1184 * to avoid DoS attacks.
1186 return NT_STATUS_INVALID_BUFFER_SIZE;
1189 if ((spnego_state->in_needed > 0) && (in.length == 0)) {
1191 * If we reach this, we know we got at least
1192 * part of an asn1 message, getting 0 means
1193 * the remote peer wants us to spin.
1195 return NT_STATUS_INVALID_PARAMETER;
1198 expected = spnego_state->in_needed - spnego_state->in_frag.length;
1199 if (in.length > expected) {
1201 * we got more than expected
1203 return NT_STATUS_INVALID_PARAMETER;
1206 if (in.length == spnego_state->in_needed) {
1208 * if the in.length contains the full blob
1209 * we are done.
1211 * Note: this implies spnego_state->in_frag.length == 0,
1212 * but we do not need to check this explicitly
1213 * because we already know that we did not get
1214 * more than expected.
1216 *full_in = in;
1217 return NT_STATUS_OK;
1220 ok = data_blob_append(spnego_state, &spnego_state->in_frag,
1221 in.data, in.length);
1222 if (!ok) {
1223 return NT_STATUS_NO_MEMORY;
1226 if (spnego_state->in_needed > spnego_state->in_frag.length) {
1227 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1230 *full_in = spnego_state->in_frag;
1231 return NT_STATUS_OK;
1234 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1235 TALLOC_CTX *out_mem_ctx,
1236 DATA_BLOB *_out)
1238 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1239 DATA_BLOB out = data_blob_null;
1241 *_out = data_blob_null;
1243 if (spnego_state->out_frag.length == 0) {
1244 return spnego_state->out_status;
1248 * There is still more data to be delivered
1249 * to the remote peer.
1252 if (spnego_state->out_frag.length <= spnego_state->out_max_length) {
1254 * Fast path, we can deliver everything
1257 *_out = spnego_state->out_frag;
1258 talloc_steal(out_mem_ctx, _out->data);
1259 spnego_state->out_frag = data_blob_null;
1260 return spnego_state->out_status;
1263 out = spnego_state->out_frag;
1266 * copy the remaining bytes
1268 spnego_state->out_frag = data_blob_talloc(spnego_state,
1269 out.data + spnego_state->out_max_length,
1270 out.length - spnego_state->out_max_length);
1271 if (spnego_state->out_frag.data == NULL) {
1272 return NT_STATUS_NO_MEMORY;
1276 * truncate the buffer
1278 data_blob_realloc(spnego_state, &out, spnego_state->out_max_length);
1280 talloc_steal(out_mem_ctx, out.data);
1281 *_out = out;
1282 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1285 static NTSTATUS gensec_spnego_update_wrapper(struct gensec_security *gensec_security,
1286 TALLOC_CTX *out_mem_ctx,
1287 struct tevent_context *ev,
1288 const DATA_BLOB in, DATA_BLOB *out)
1290 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1291 DATA_BLOB full_in = data_blob_null;
1292 NTSTATUS status;
1294 *out = data_blob_null;
1296 if (spnego_state->out_frag.length > 0) {
1297 if (in.length > 0) {
1298 return NT_STATUS_INVALID_PARAMETER;
1301 return gensec_spnego_update_out(gensec_security,
1302 out_mem_ctx,
1303 out);
1306 status = gensec_spnego_update_in(gensec_security,
1307 in, &full_in);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 return status;
1312 status = gensec_spnego_update(gensec_security,
1313 spnego_state, ev,
1314 full_in,
1315 &spnego_state->out_frag);
1316 data_blob_free(&spnego_state->in_frag);
1317 spnego_state->in_needed = 0;
1318 if (!NT_STATUS_IS_OK(status) &&
1319 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1320 return status;
1323 spnego_state->out_status = status;
1325 return gensec_spnego_update_out(gensec_security,
1326 out_mem_ctx,
1327 out);
1330 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1331 uint32_t feature)
1333 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1335 if (!spnego_state || !spnego_state->sub_sec_security) {
1336 gensec_security->want_features |= feature;
1337 return;
1340 gensec_want_feature(spnego_state->sub_sec_security,
1341 feature);
1344 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1345 uint32_t feature)
1347 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1348 if (!spnego_state->sub_sec_security) {
1349 return false;
1352 return gensec_have_feature(spnego_state->sub_sec_security,
1353 feature);
1356 static NTTIME gensec_spnego_expire_time(struct gensec_security *gensec_security)
1358 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1360 if (!spnego_state->sub_sec_security) {
1361 return GENSEC_EXPIRE_TIME_INFINITY;
1364 return gensec_expire_time(spnego_state->sub_sec_security);
1367 static const char *gensec_spnego_oids[] = {
1368 GENSEC_OID_SPNEGO,
1369 NULL
1372 static const struct gensec_security_ops gensec_spnego_security_ops = {
1373 .name = "spnego",
1374 .sasl_name = "GSS-SPNEGO",
1375 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1376 .oid = gensec_spnego_oids,
1377 .client_start = gensec_spnego_client_start,
1378 .server_start = gensec_spnego_server_start,
1379 .update = gensec_spnego_update_wrapper,
1380 .seal_packet = gensec_spnego_seal_packet,
1381 .sign_packet = gensec_spnego_sign_packet,
1382 .sig_size = gensec_spnego_sig_size,
1383 .max_wrapped_size = gensec_spnego_max_wrapped_size,
1384 .max_input_size = gensec_spnego_max_input_size,
1385 .check_packet = gensec_spnego_check_packet,
1386 .unseal_packet = gensec_spnego_unseal_packet,
1387 .packet_full_request = gensec_spnego_packet_full_request,
1388 .wrap = gensec_spnego_wrap,
1389 .unwrap = gensec_spnego_unwrap,
1390 .wrap_packets = gensec_spnego_wrap_packets,
1391 .unwrap_packets = gensec_spnego_unwrap_packets,
1392 .session_key = gensec_spnego_session_key,
1393 .session_info = gensec_spnego_session_info,
1394 .want_feature = gensec_spnego_want_feature,
1395 .have_feature = gensec_spnego_have_feature,
1396 .expire_time = gensec_spnego_expire_time,
1397 .enabled = true,
1398 .priority = GENSEC_SPNEGO
1401 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1403 NTSTATUS ret;
1404 ret = gensec_register(&gensec_spnego_security_ops);
1405 if (!NT_STATUS_IS_OK(ret)) {
1406 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1407 gensec_spnego_security_ops.name));
1408 return ret;
1411 return ret;