freebsd9: support both WAF MIT krb5 build and autoconf build against MIT krb5
[Samba/vl.git] / auth / gensec / spnego.c
blob6ce97d99fae760636b104e1a2dc0c251df508914
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 "param/param.h"
31 #include "lib/util/asn1.h"
33 #undef strcasecmp
35 _PUBLIC_ NTSTATUS gensec_spnego_init(void);
37 enum spnego_state_position {
38 SPNEGO_SERVER_START,
39 SPNEGO_CLIENT_START,
40 SPNEGO_SERVER_TARG,
41 SPNEGO_CLIENT_TARG,
42 SPNEGO_FALLBACK,
43 SPNEGO_DONE
46 struct spnego_state {
47 enum spnego_message_type expected_packet;
48 enum spnego_state_position state_position;
49 struct gensec_security *sub_sec_security;
50 bool no_response_expected;
52 const char *neg_oid;
54 DATA_BLOB mech_types;
57 * The following is used to implement
58 * the update token fragmentation
60 size_t in_needed;
61 DATA_BLOB in_frag;
62 size_t out_max_length;
63 DATA_BLOB out_frag;
64 NTSTATUS out_status;
68 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
70 struct spnego_state *spnego_state;
72 spnego_state = talloc_zero(gensec_security, struct spnego_state);
73 if (!spnego_state) {
74 return NT_STATUS_NO_MEMORY;
77 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
78 spnego_state->state_position = SPNEGO_CLIENT_START;
79 spnego_state->sub_sec_security = NULL;
80 spnego_state->no_response_expected = false;
81 spnego_state->mech_types = data_blob(NULL, 0);
82 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
83 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
85 gensec_security->private_data = spnego_state;
86 return NT_STATUS_OK;
89 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
91 struct spnego_state *spnego_state;
93 spnego_state = talloc_zero(gensec_security, struct spnego_state);
94 if (!spnego_state) {
95 return NT_STATUS_NO_MEMORY;
98 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
99 spnego_state->state_position = SPNEGO_SERVER_START;
100 spnego_state->sub_sec_security = NULL;
101 spnego_state->no_response_expected = false;
102 spnego_state->mech_types = data_blob(NULL, 0);
103 spnego_state->out_max_length = gensec_max_update_size(gensec_security);
104 spnego_state->out_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
106 gensec_security->private_data = spnego_state;
107 return NT_STATUS_OK;
111 wrappers for the spnego_*() functions
113 static NTSTATUS gensec_spnego_unseal_packet(struct gensec_security *gensec_security,
114 uint8_t *data, size_t length,
115 const uint8_t *whole_pdu, size_t pdu_length,
116 const DATA_BLOB *sig)
118 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
120 if (spnego_state->state_position != SPNEGO_DONE
121 && spnego_state->state_position != SPNEGO_FALLBACK) {
122 return NT_STATUS_INVALID_PARAMETER;
125 return gensec_unseal_packet(spnego_state->sub_sec_security,
126 data, length,
127 whole_pdu, pdu_length,
128 sig);
131 static NTSTATUS gensec_spnego_check_packet(struct gensec_security *gensec_security,
132 const uint8_t *data, size_t length,
133 const uint8_t *whole_pdu, size_t pdu_length,
134 const DATA_BLOB *sig)
136 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
138 if (spnego_state->state_position != SPNEGO_DONE
139 && spnego_state->state_position != SPNEGO_FALLBACK) {
140 return NT_STATUS_INVALID_PARAMETER;
143 return gensec_check_packet(spnego_state->sub_sec_security,
144 data, length,
145 whole_pdu, pdu_length,
146 sig);
149 static NTSTATUS gensec_spnego_seal_packet(struct gensec_security *gensec_security,
150 TALLOC_CTX *mem_ctx,
151 uint8_t *data, size_t length,
152 const uint8_t *whole_pdu, size_t pdu_length,
153 DATA_BLOB *sig)
155 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
157 if (spnego_state->state_position != SPNEGO_DONE
158 && spnego_state->state_position != SPNEGO_FALLBACK) {
159 return NT_STATUS_INVALID_PARAMETER;
162 return gensec_seal_packet(spnego_state->sub_sec_security,
163 mem_ctx,
164 data, length,
165 whole_pdu, pdu_length,
166 sig);
169 static NTSTATUS gensec_spnego_sign_packet(struct gensec_security *gensec_security,
170 TALLOC_CTX *mem_ctx,
171 const uint8_t *data, size_t length,
172 const uint8_t *whole_pdu, size_t pdu_length,
173 DATA_BLOB *sig)
175 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
177 if (spnego_state->state_position != SPNEGO_DONE
178 && spnego_state->state_position != SPNEGO_FALLBACK) {
179 return NT_STATUS_INVALID_PARAMETER;
182 return gensec_sign_packet(spnego_state->sub_sec_security,
183 mem_ctx,
184 data, length,
185 whole_pdu, pdu_length,
186 sig);
189 static NTSTATUS gensec_spnego_wrap(struct gensec_security *gensec_security,
190 TALLOC_CTX *mem_ctx,
191 const DATA_BLOB *in,
192 DATA_BLOB *out)
194 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
196 if (spnego_state->state_position != SPNEGO_DONE
197 && spnego_state->state_position != SPNEGO_FALLBACK) {
198 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
199 return NT_STATUS_INVALID_PARAMETER;
202 return gensec_wrap(spnego_state->sub_sec_security,
203 mem_ctx, in, out);
206 static NTSTATUS gensec_spnego_unwrap(struct gensec_security *gensec_security,
207 TALLOC_CTX *mem_ctx,
208 const DATA_BLOB *in,
209 DATA_BLOB *out)
211 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
213 if (spnego_state->state_position != SPNEGO_DONE
214 && spnego_state->state_position != SPNEGO_FALLBACK) {
215 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
216 return NT_STATUS_INVALID_PARAMETER;
219 return gensec_unwrap(spnego_state->sub_sec_security,
220 mem_ctx, in, out);
223 static NTSTATUS gensec_spnego_wrap_packets(struct gensec_security *gensec_security,
224 TALLOC_CTX *mem_ctx,
225 const DATA_BLOB *in,
226 DATA_BLOB *out,
227 size_t *len_processed)
229 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
231 if (spnego_state->state_position != SPNEGO_DONE
232 && spnego_state->state_position != SPNEGO_FALLBACK) {
233 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
234 return NT_STATUS_INVALID_PARAMETER;
237 return gensec_wrap_packets(spnego_state->sub_sec_security,
238 mem_ctx, in, out,
239 len_processed);
242 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security,
243 DATA_BLOB blob, size_t *size)
245 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
247 if (spnego_state->state_position != SPNEGO_DONE
248 && spnego_state->state_position != SPNEGO_FALLBACK) {
249 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
250 return NT_STATUS_INVALID_PARAMETER;
253 return gensec_packet_full_request(spnego_state->sub_sec_security,
254 blob, size);
257 static NTSTATUS gensec_spnego_unwrap_packets(struct gensec_security *gensec_security,
258 TALLOC_CTX *mem_ctx,
259 const DATA_BLOB *in,
260 DATA_BLOB *out,
261 size_t *len_processed)
263 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
265 if (spnego_state->state_position != SPNEGO_DONE
266 && spnego_state->state_position != SPNEGO_FALLBACK) {
267 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
268 return NT_STATUS_INVALID_PARAMETER;
271 return gensec_unwrap_packets(spnego_state->sub_sec_security,
272 mem_ctx, in, out,
273 len_processed);
276 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size)
278 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
280 if (spnego_state->state_position != SPNEGO_DONE
281 && spnego_state->state_position != SPNEGO_FALLBACK) {
282 return 0;
285 return gensec_sig_size(spnego_state->sub_sec_security, data_size);
288 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security)
290 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
292 if (spnego_state->state_position != SPNEGO_DONE
293 && spnego_state->state_position != SPNEGO_FALLBACK) {
294 return 0;
297 return gensec_max_input_size(spnego_state->sub_sec_security);
300 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security)
302 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
304 if (spnego_state->state_position != SPNEGO_DONE
305 && spnego_state->state_position != SPNEGO_FALLBACK) {
306 return 0;
309 return gensec_max_wrapped_size(spnego_state->sub_sec_security);
312 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security,
313 TALLOC_CTX *mem_ctx,
314 DATA_BLOB *session_key)
316 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
317 if (!spnego_state->sub_sec_security) {
318 return NT_STATUS_INVALID_PARAMETER;
321 return gensec_session_key(spnego_state->sub_sec_security,
322 mem_ctx,
323 session_key);
326 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
327 TALLOC_CTX *mem_ctx,
328 struct auth_session_info **session_info)
330 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
331 if (!spnego_state->sub_sec_security) {
332 return NT_STATUS_INVALID_PARAMETER;
335 return gensec_session_info(spnego_state->sub_sec_security,
336 mem_ctx,
337 session_info);
340 /** Fallback to another GENSEC mechanism, based on magic strings
342 * This is the 'fallback' case, where we don't get SPNEGO, and have to
343 * try all the other options (and hope they all have a magic string
344 * they check)
347 static NTSTATUS gensec_spnego_server_try_fallback(struct gensec_security *gensec_security,
348 struct spnego_state *spnego_state,
349 struct tevent_context *ev,
350 TALLOC_CTX *out_mem_ctx,
351 const DATA_BLOB in, DATA_BLOB *out)
353 int i,j;
354 struct gensec_security_ops **all_ops
355 = gensec_security_mechs(gensec_security, out_mem_ctx);
356 for (i=0; all_ops[i]; i++) {
357 bool is_spnego;
358 NTSTATUS nt_status;
360 if (gensec_security != NULL &&
361 !gensec_security_ops_enabled(all_ops[i], gensec_security))
362 continue;
364 if (!all_ops[i]->oid) {
365 continue;
368 is_spnego = false;
369 for (j=0; all_ops[i]->oid[j]; j++) {
370 if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
371 is_spnego = true;
374 if (is_spnego) {
375 continue;
378 if (!all_ops[i]->magic) {
379 continue;
382 nt_status = all_ops[i]->magic(gensec_security, &in);
383 if (!NT_STATUS_IS_OK(nt_status)) {
384 continue;
387 spnego_state->state_position = SPNEGO_FALLBACK;
389 nt_status = gensec_subcontext_start(spnego_state,
390 gensec_security,
391 &spnego_state->sub_sec_security);
393 if (!NT_STATUS_IS_OK(nt_status)) {
394 return nt_status;
396 /* select the sub context */
397 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
398 all_ops[i]);
399 if (!NT_STATUS_IS_OK(nt_status)) {
400 return nt_status;
402 nt_status = gensec_update(spnego_state->sub_sec_security,
403 ev, out_mem_ctx, in, out);
404 return nt_status;
406 DEBUG(1, ("Failed to parse SPNEGO request\n"));
407 return NT_STATUS_INVALID_PARAMETER;
412 Parse the netTokenInit, either from the client, to the server, or
413 from the server to the client.
416 static NTSTATUS gensec_spnego_parse_negTokenInit(struct gensec_security *gensec_security,
417 struct spnego_state *spnego_state,
418 TALLOC_CTX *out_mem_ctx,
419 struct tevent_context *ev,
420 const char **mechType,
421 const DATA_BLOB unwrapped_in, DATA_BLOB *unwrapped_out)
423 int i;
424 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
425 DATA_BLOB null_data_blob = data_blob(NULL,0);
426 bool ok;
428 const struct gensec_security_ops_wrapper *all_sec
429 = gensec_security_by_oid_list(gensec_security,
430 out_mem_ctx,
431 mechType,
432 GENSEC_OID_SPNEGO);
434 ok = spnego_write_mech_types(spnego_state,
435 mechType,
436 &spnego_state->mech_types);
437 if (!ok) {
438 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
439 return NT_STATUS_NO_MEMORY;
442 if (spnego_state->state_position == SPNEGO_SERVER_START) {
443 uint32_t j;
444 for (j=0; mechType && mechType[j]; j++) {
445 for (i=0; all_sec && all_sec[i].op; i++) {
446 if (strcmp(mechType[j], all_sec[i].oid) != 0) {
447 continue;
450 nt_status = gensec_subcontext_start(spnego_state,
451 gensec_security,
452 &spnego_state->sub_sec_security);
453 if (!NT_STATUS_IS_OK(nt_status)) {
454 return nt_status;
456 /* select the sub context */
457 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
458 all_sec[i].op);
459 if (!NT_STATUS_IS_OK(nt_status)) {
460 talloc_free(spnego_state->sub_sec_security);
461 spnego_state->sub_sec_security = NULL;
462 break;
465 if (j > 0) {
466 /* no optimistic token */
467 spnego_state->neg_oid = all_sec[i].oid;
468 *unwrapped_out = data_blob_null;
469 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
470 break;
473 nt_status = gensec_update(spnego_state->sub_sec_security,
474 out_mem_ctx,
476 unwrapped_in,
477 unwrapped_out);
478 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
479 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
480 /* Pretend we never started it (lets the first run find some incompatible demand) */
482 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
483 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
484 talloc_free(spnego_state->sub_sec_security);
485 spnego_state->sub_sec_security = NULL;
486 break;
489 spnego_state->neg_oid = all_sec[i].oid;
490 break;
492 if (spnego_state->sub_sec_security) {
493 break;
497 if (!spnego_state->sub_sec_security) {
498 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
499 return NT_STATUS_INVALID_PARAMETER;
503 /* Having tried any optimistic token from the client (if we
504 * were the server), if we didn't get anywhere, walk our list
505 * in our preference order */
507 if (!spnego_state->sub_sec_security) {
508 for (i=0; all_sec && all_sec[i].op; i++) {
509 nt_status = gensec_subcontext_start(spnego_state,
510 gensec_security,
511 &spnego_state->sub_sec_security);
512 if (!NT_STATUS_IS_OK(nt_status)) {
513 return nt_status;
515 /* select the sub context */
516 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
517 all_sec[i].op);
518 if (!NT_STATUS_IS_OK(nt_status)) {
519 talloc_free(spnego_state->sub_sec_security);
520 spnego_state->sub_sec_security = NULL;
521 continue;
524 spnego_state->neg_oid = all_sec[i].oid;
526 /* only get the helping start blob for the first OID */
527 nt_status = gensec_update(spnego_state->sub_sec_security,
528 out_mem_ctx,
530 null_data_blob,
531 unwrapped_out);
533 /* it is likely that a NULL input token will
534 * not be liked by most server mechs, but if
535 * we are in the client, we want the first
536 * update packet to be able to abort the use
537 * of this mech */
538 if (spnego_state->state_position != SPNEGO_SERVER_START) {
539 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER) ||
540 NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS) ||
541 NT_STATUS_EQUAL(nt_status, NT_STATUS_TIME_DIFFERENCE_AT_DC) ||
542 NT_STATUS_EQUAL(nt_status, NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
543 /* Pretend we never started it (lets the first run find some incompatible demand) */
545 DEBUG(3, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
546 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
547 talloc_free(spnego_state->sub_sec_security);
548 spnego_state->sub_sec_security = NULL;
549 continue;
553 break;
557 if (spnego_state->sub_sec_security) {
558 /* it is likely that a NULL input token will
559 * not be liked by most server mechs, but this
560 * does the right thing in the CIFS client.
561 * just push us along the merry-go-round
562 * again, and hope for better luck next
563 * time */
565 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)) {
566 *unwrapped_out = data_blob(NULL, 0);
567 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
570 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_PARAMETER)
571 && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
572 && !NT_STATUS_IS_OK(nt_status)) {
573 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
574 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
575 talloc_free(spnego_state->sub_sec_security);
576 spnego_state->sub_sec_security = NULL;
578 /* We started the mech correctly, and the
579 * input from the other side was valid.
580 * Return the error (say bad password, invalid
581 * ticket) */
582 return nt_status;
586 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
589 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
590 /* we could re-negotiate here, but it would only work
591 * if the client or server lied about what it could
592 * support the first time. Lets keep this code to
593 * reality */
595 return nt_status;
598 /** create a negTokenInit
600 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
602 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security,
603 struct spnego_state *spnego_state,
604 TALLOC_CTX *out_mem_ctx,
605 struct tevent_context *ev,
606 const DATA_BLOB in, DATA_BLOB *out)
608 int i;
609 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
610 DATA_BLOB null_data_blob = data_blob(NULL,0);
611 const char **mechTypes = NULL;
612 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
613 const struct gensec_security_ops_wrapper *all_sec;
615 mechTypes = gensec_security_oids(gensec_security,
616 out_mem_ctx, GENSEC_OID_SPNEGO);
618 all_sec = gensec_security_by_oid_list(gensec_security,
619 out_mem_ctx,
620 mechTypes,
621 GENSEC_OID_SPNEGO);
622 for (i=0; all_sec && all_sec[i].op; i++) {
623 struct spnego_data spnego_out;
624 const char **send_mech_types;
625 bool ok;
627 nt_status = gensec_subcontext_start(spnego_state,
628 gensec_security,
629 &spnego_state->sub_sec_security);
630 if (!NT_STATUS_IS_OK(nt_status)) {
631 return nt_status;
633 /* select the sub context */
634 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
635 all_sec[i].op);
636 if (!NT_STATUS_IS_OK(nt_status)) {
637 talloc_free(spnego_state->sub_sec_security);
638 spnego_state->sub_sec_security = NULL;
639 continue;
642 /* In the client, try and produce the first (optimistic) packet */
643 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
644 nt_status = gensec_update(spnego_state->sub_sec_security,
645 out_mem_ctx,
647 null_data_blob,
648 &unwrapped_out);
650 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
651 && !NT_STATUS_IS_OK(nt_status)) {
652 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
653 spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
654 talloc_free(spnego_state->sub_sec_security);
655 spnego_state->sub_sec_security = NULL;
656 /* Pretend we never started it (lets the first run find some incompatible demand) */
658 continue;
662 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
664 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
665 &all_sec[i]);
667 ok = spnego_write_mech_types(spnego_state,
668 send_mech_types,
669 &spnego_state->mech_types);
670 if (!ok) {
671 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
672 return NT_STATUS_NO_MEMORY;
675 /* List the remaining mechs as options */
676 spnego_out.negTokenInit.mechTypes = send_mech_types;
677 spnego_out.negTokenInit.reqFlags = null_data_blob;
678 spnego_out.negTokenInit.reqFlagsPadding = 0;
680 if (spnego_state->state_position == SPNEGO_SERVER_START) {
681 spnego_out.negTokenInit.mechListMIC
682 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
683 } else {
684 spnego_out.negTokenInit.mechListMIC = null_data_blob;
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 NEG_TOKEN_INIT\n"));
691 return NT_STATUS_INVALID_PARAMETER;
694 /* set next state */
695 spnego_state->neg_oid = all_sec[i].oid;
697 if (NT_STATUS_IS_OK(nt_status)) {
698 spnego_state->no_response_expected = true;
701 return NT_STATUS_MORE_PROCESSING_REQUIRED;
703 talloc_free(spnego_state->sub_sec_security);
704 spnego_state->sub_sec_security = NULL;
706 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
707 return NT_STATUS_INVALID_PARAMETER;
711 /** create a server negTokenTarg
713 * This is the case, where the client is the first one who sends data
716 static NTSTATUS gensec_spnego_server_negTokenTarg(struct gensec_security *gensec_security,
717 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(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(gensec_security,
816 spnego_state,
817 out_mem_ctx,
818 nt_status,
819 unwrapped_out,
820 null_data_blob,
821 out);
823 spnego_free_data(&spnego);
825 return nt_status;
826 } else {
827 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
828 out_mem_ctx, ev, in, out);
829 spnego_state->state_position = SPNEGO_SERVER_START;
830 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
831 return nt_status;
835 case SPNEGO_CLIENT_START:
837 /* The server offers a list of mechanisms */
839 const char *my_mechs[] = {NULL, NULL};
840 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
842 if (!in.length) {
843 /* client to produce negTokenInit */
844 nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state,
845 out_mem_ctx, ev, in, out);
846 spnego_state->state_position = SPNEGO_CLIENT_TARG;
847 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
848 return nt_status;
851 len = spnego_read_data(gensec_security, in, &spnego);
853 if (len == -1) {
854 DEBUG(1, ("Invalid SPNEGO request:\n"));
855 dump_data(1, in.data, in.length);
856 return NT_STATUS_INVALID_PARAMETER;
859 /* OK, so it's real SPNEGO, check the packet's the one we expect */
860 if (spnego.type != spnego_state->expected_packet) {
861 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
862 spnego_state->expected_packet));
863 dump_data(1, in.data, in.length);
864 spnego_free_data(&spnego);
865 return NT_STATUS_INVALID_PARAMETER;
868 if (spnego.negTokenInit.targetPrincipal
869 && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
870 DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
871 if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
872 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
876 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
877 spnego_state,
878 out_mem_ctx,
880 spnego.negTokenInit.mechTypes,
881 spnego.negTokenInit.mechToken,
882 &unwrapped_out);
884 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
885 spnego_free_data(&spnego);
886 return nt_status;
889 my_mechs[0] = spnego_state->neg_oid;
890 /* compose reply */
891 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
892 spnego_out.negTokenInit.mechTypes = my_mechs;
893 spnego_out.negTokenInit.reqFlags = null_data_blob;
894 spnego_out.negTokenInit.reqFlagsPadding = 0;
895 spnego_out.negTokenInit.mechListMIC = null_data_blob;
896 spnego_out.negTokenInit.mechToken = unwrapped_out;
898 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
899 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
900 return NT_STATUS_INVALID_PARAMETER;
903 /* set next state */
904 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
905 spnego_state->state_position = SPNEGO_CLIENT_TARG;
907 if (NT_STATUS_IS_OK(nt_status)) {
908 spnego_state->no_response_expected = true;
911 spnego_free_data(&spnego);
912 return NT_STATUS_MORE_PROCESSING_REQUIRED;
914 case SPNEGO_SERVER_TARG:
916 NTSTATUS nt_status;
917 bool new_spnego = false;
919 if (!in.length) {
920 return NT_STATUS_INVALID_PARAMETER;
923 len = spnego_read_data(gensec_security, in, &spnego);
925 if (len == -1) {
926 DEBUG(1, ("Invalid SPNEGO request:\n"));
927 dump_data(1, in.data, in.length);
928 return NT_STATUS_INVALID_PARAMETER;
931 /* OK, so it's real SPNEGO, check the packet's the one we expect */
932 if (spnego.type != spnego_state->expected_packet) {
933 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
934 spnego_state->expected_packet));
935 dump_data(1, in.data, in.length);
936 spnego_free_data(&spnego);
937 return NT_STATUS_INVALID_PARAMETER;
940 if (!spnego_state->sub_sec_security) {
941 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
942 spnego_free_data(&spnego);
943 return NT_STATUS_INVALID_PARAMETER;
946 nt_status = gensec_update(spnego_state->sub_sec_security,
947 out_mem_ctx, ev,
948 spnego.negTokenTarg.responseToken,
949 &unwrapped_out);
950 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
951 new_spnego = true;
952 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
953 spnego_state->mech_types.data,
954 spnego_state->mech_types.length,
955 spnego_state->mech_types.data,
956 spnego_state->mech_types.length,
957 &spnego.negTokenTarg.mechListMIC);
958 if (!NT_STATUS_IS_OK(nt_status)) {
959 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
960 nt_errstr(nt_status)));
963 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
964 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
965 out_mem_ctx,
966 spnego_state->mech_types.data,
967 spnego_state->mech_types.length,
968 spnego_state->mech_types.data,
969 spnego_state->mech_types.length,
970 &mech_list_mic);
971 if (!NT_STATUS_IS_OK(nt_status)) {
972 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
973 nt_errstr(nt_status)));
977 nt_status = gensec_spnego_server_negTokenTarg(gensec_security,
978 spnego_state,
979 out_mem_ctx,
980 nt_status,
981 unwrapped_out,
982 mech_list_mic,
983 out);
985 spnego_free_data(&spnego);
987 return nt_status;
989 case SPNEGO_CLIENT_TARG:
991 NTSTATUS nt_status;
992 if (!in.length) {
993 return NT_STATUS_INVALID_PARAMETER;
996 len = spnego_read_data(gensec_security, in, &spnego);
998 if (len == -1) {
999 DEBUG(1, ("Invalid SPNEGO request:\n"));
1000 dump_data(1, in.data, in.length);
1001 return NT_STATUS_INVALID_PARAMETER;
1004 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1005 if (spnego.type != spnego_state->expected_packet) {
1006 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type,
1007 spnego_state->expected_packet));
1008 dump_data(1, in.data, in.length);
1009 spnego_free_data(&spnego);
1010 return NT_STATUS_INVALID_PARAMETER;
1013 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1014 spnego_free_data(&spnego);
1015 return NT_STATUS_ACCESS_DENIED;
1018 /* Server didn't like our choice of mech, and chose something else */
1019 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
1020 spnego.negTokenTarg.supportedMech &&
1021 strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
1022 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
1023 gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech),
1024 gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
1026 talloc_free(spnego_state->sub_sec_security);
1027 nt_status = gensec_subcontext_start(spnego_state,
1028 gensec_security,
1029 &spnego_state->sub_sec_security);
1030 if (!NT_STATUS_IS_OK(nt_status)) {
1031 spnego_free_data(&spnego);
1032 return nt_status;
1034 /* select the sub context */
1035 nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
1036 spnego.negTokenTarg.supportedMech);
1037 if (!NT_STATUS_IS_OK(nt_status)) {
1038 spnego_free_data(&spnego);
1039 return nt_status;
1042 nt_status = gensec_update(spnego_state->sub_sec_security,
1043 out_mem_ctx, ev,
1044 spnego.negTokenTarg.responseToken,
1045 &unwrapped_out);
1046 spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1047 } else if (spnego_state->no_response_expected) {
1048 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1049 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1050 nt_status = NT_STATUS_INVALID_PARAMETER;
1051 } else if (spnego.negTokenTarg.responseToken.length) {
1052 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1053 nt_status = NT_STATUS_INVALID_PARAMETER;
1054 } else {
1055 nt_status = NT_STATUS_OK;
1057 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1058 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1059 spnego_state->mech_types.data,
1060 spnego_state->mech_types.length,
1061 spnego_state->mech_types.data,
1062 spnego_state->mech_types.length,
1063 &spnego.negTokenTarg.mechListMIC);
1064 if (!NT_STATUS_IS_OK(nt_status)) {
1065 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1066 nt_errstr(nt_status)));
1069 } else {
1070 bool new_spnego = false;
1072 nt_status = gensec_update(spnego_state->sub_sec_security,
1073 out_mem_ctx, ev,
1074 spnego.negTokenTarg.responseToken,
1075 &unwrapped_out);
1077 if (NT_STATUS_IS_OK(nt_status)
1078 && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1079 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1080 GENSEC_FEATURE_NEW_SPNEGO);
1082 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1083 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1084 out_mem_ctx,
1085 spnego_state->mech_types.data,
1086 spnego_state->mech_types.length,
1087 spnego_state->mech_types.data,
1088 spnego_state->mech_types.length,
1089 &mech_list_mic);
1090 if (!NT_STATUS_IS_OK(nt_status)) {
1091 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1092 nt_errstr(nt_status)));
1095 if (NT_STATUS_IS_OK(nt_status)) {
1096 spnego_state->no_response_expected = true;
1100 spnego_free_data(&spnego);
1102 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1103 && !NT_STATUS_IS_OK(nt_status)) {
1104 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1105 spnego_state->sub_sec_security->ops->name,
1106 nt_errstr(nt_status)));
1107 return nt_status;
1110 if (unwrapped_out.length || mech_list_mic.length) {
1111 /* compose reply */
1112 spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1113 spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1114 spnego_out.negTokenTarg.supportedMech = NULL;
1115 spnego_out.negTokenTarg.responseToken = unwrapped_out;
1116 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1118 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1119 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1120 return NT_STATUS_INVALID_PARAMETER;
1123 spnego_state->state_position = SPNEGO_CLIENT_TARG;
1124 nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1125 } else {
1127 /* all done - server has accepted, and we agree */
1128 *out = null_data_blob;
1130 if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1131 /* unless of course it did not accept */
1132 DEBUG(1,("gensec_update ok but not accepted\n"));
1133 nt_status = NT_STATUS_INVALID_PARAMETER;
1136 spnego_state->state_position = SPNEGO_DONE;
1139 return nt_status;
1141 case SPNEGO_DONE:
1142 /* We should not be called after we are 'done' */
1143 return NT_STATUS_INVALID_PARAMETER;
1145 return NT_STATUS_INVALID_PARAMETER;
1148 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1149 const DATA_BLOB in, DATA_BLOB *full_in)
1151 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1152 size_t expected;
1153 NTSTATUS status;
1154 bool ok;
1156 *full_in = data_blob_null;
1158 if (spnego_state->in_needed == 0) {
1159 size_t size = 0;
1162 * try to work out the size of the full
1163 * input token, it might be fragmented
1165 status = asn1_peek_full_tag(in, ASN1_APPLICATION(0), &size);
1166 if (!NT_STATUS_IS_OK(status) &&
1167 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1168 status = asn1_peek_full_tag(in, ASN1_CONTEXT(1), &size);
1171 if (NT_STATUS_IS_OK(status) ||
1172 NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1173 spnego_state->in_needed = size;
1174 } else {
1176 * If it is not an asn1 message
1177 * just call the next layer.
1179 spnego_state->in_needed = in.length;
1183 if (spnego_state->in_needed > UINT16_MAX) {
1185 * limit the incoming message to 0xFFFF
1186 * to avoid DoS attacks.
1188 return NT_STATUS_INVALID_BUFFER_SIZE;
1191 if ((spnego_state->in_needed > 0) && (in.length == 0)) {
1193 * If we reach this, we know we got at least
1194 * part of an asn1 message, getting 0 means
1195 * the remote peer wants us to spin.
1197 return NT_STATUS_INVALID_PARAMETER;
1200 expected = spnego_state->in_needed - spnego_state->in_frag.length;
1201 if (in.length > expected) {
1203 * we got more than expected
1205 return NT_STATUS_INVALID_PARAMETER;
1208 if (in.length == spnego_state->in_needed) {
1210 * if the in.length contains the full blob
1211 * we are done.
1213 * Note: this implies spnego_state->in_frag.length == 0,
1214 * but we do not need to check this explicitly
1215 * because we already know that we did not get
1216 * more than expected.
1218 *full_in = in;
1219 return NT_STATUS_OK;
1222 ok = data_blob_append(spnego_state, &spnego_state->in_frag,
1223 in.data, in.length);
1224 if (!ok) {
1225 return NT_STATUS_NO_MEMORY;
1228 if (spnego_state->in_needed > spnego_state->in_frag.length) {
1229 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1232 *full_in = spnego_state->in_frag;
1233 return NT_STATUS_OK;
1236 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1237 TALLOC_CTX *out_mem_ctx,
1238 DATA_BLOB *_out)
1240 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1241 DATA_BLOB out = data_blob_null;
1243 *_out = data_blob_null;
1245 if (spnego_state->out_frag.length == 0) {
1246 return spnego_state->out_status;
1250 * There is still more data to be delivered
1251 * to the remote peer.
1254 if (spnego_state->out_frag.length <= spnego_state->out_max_length) {
1256 * Fast path, we can deliver everything
1259 *_out = spnego_state->out_frag;
1260 talloc_steal(out_mem_ctx, _out->data);
1261 spnego_state->out_frag = data_blob_null;
1262 return spnego_state->out_status;
1265 out = spnego_state->out_frag;
1268 * copy the remaining bytes
1270 spnego_state->out_frag = data_blob_talloc(spnego_state,
1271 out.data + spnego_state->out_max_length,
1272 out.length - spnego_state->out_max_length);
1273 if (spnego_state->out_frag.data == NULL) {
1274 return NT_STATUS_NO_MEMORY;
1278 * truncate the buffer
1280 data_blob_realloc(spnego_state, &out, spnego_state->out_max_length);
1282 talloc_steal(out_mem_ctx, out.data);
1283 *_out = out;
1284 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1287 static NTSTATUS gensec_spnego_update_wrapper(struct gensec_security *gensec_security,
1288 TALLOC_CTX *out_mem_ctx,
1289 struct tevent_context *ev,
1290 const DATA_BLOB in, DATA_BLOB *out)
1292 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1293 DATA_BLOB full_in = data_blob_null;
1294 NTSTATUS status;
1296 *out = data_blob_null;
1298 if (spnego_state->out_frag.length > 0) {
1299 if (in.length > 0) {
1300 return NT_STATUS_INVALID_PARAMETER;
1303 return gensec_spnego_update_out(gensec_security,
1304 out_mem_ctx,
1305 out);
1308 status = gensec_spnego_update_in(gensec_security,
1309 in, &full_in);
1310 if (!NT_STATUS_IS_OK(status)) {
1311 return status;
1314 status = gensec_spnego_update(gensec_security,
1315 spnego_state, ev,
1316 full_in,
1317 &spnego_state->out_frag);
1318 data_blob_free(&spnego_state->in_frag);
1319 spnego_state->in_needed = 0;
1320 if (!NT_STATUS_IS_OK(status) &&
1321 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1322 return status;
1325 spnego_state->out_status = status;
1327 return gensec_spnego_update_out(gensec_security,
1328 out_mem_ctx,
1329 out);
1332 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1333 uint32_t feature)
1335 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1337 if (!spnego_state || !spnego_state->sub_sec_security) {
1338 gensec_security->want_features |= feature;
1339 return;
1342 gensec_want_feature(spnego_state->sub_sec_security,
1343 feature);
1346 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1347 uint32_t feature)
1349 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1350 if (!spnego_state->sub_sec_security) {
1351 return false;
1354 return gensec_have_feature(spnego_state->sub_sec_security,
1355 feature);
1358 static NTTIME gensec_spnego_expire_time(struct gensec_security *gensec_security)
1360 struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1362 if (!spnego_state->sub_sec_security) {
1363 return GENSEC_EXPIRE_TIME_INFINITY;
1366 return gensec_expire_time(spnego_state->sub_sec_security);
1369 static const char *gensec_spnego_oids[] = {
1370 GENSEC_OID_SPNEGO,
1371 NULL
1374 static const struct gensec_security_ops gensec_spnego_security_ops = {
1375 .name = "spnego",
1376 .sasl_name = "GSS-SPNEGO",
1377 .auth_type = DCERPC_AUTH_TYPE_SPNEGO,
1378 .oid = gensec_spnego_oids,
1379 .client_start = gensec_spnego_client_start,
1380 .server_start = gensec_spnego_server_start,
1381 .update = gensec_spnego_update_wrapper,
1382 .seal_packet = gensec_spnego_seal_packet,
1383 .sign_packet = gensec_spnego_sign_packet,
1384 .sig_size = gensec_spnego_sig_size,
1385 .max_wrapped_size = gensec_spnego_max_wrapped_size,
1386 .max_input_size = gensec_spnego_max_input_size,
1387 .check_packet = gensec_spnego_check_packet,
1388 .unseal_packet = gensec_spnego_unseal_packet,
1389 .packet_full_request = gensec_spnego_packet_full_request,
1390 .wrap = gensec_spnego_wrap,
1391 .unwrap = gensec_spnego_unwrap,
1392 .wrap_packets = gensec_spnego_wrap_packets,
1393 .unwrap_packets = gensec_spnego_unwrap_packets,
1394 .session_key = gensec_spnego_session_key,
1395 .session_info = gensec_spnego_session_info,
1396 .want_feature = gensec_spnego_want_feature,
1397 .have_feature = gensec_spnego_have_feature,
1398 .expire_time = gensec_spnego_expire_time,
1399 .enabled = true,
1400 .priority = GENSEC_SPNEGO
1403 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1405 NTSTATUS ret;
1406 ret = gensec_register(&gensec_spnego_security_ops);
1407 if (!NT_STATUS_IS_OK(ret)) {
1408 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1409 gensec_spnego_security_ops.name));
1410 return ret;
1413 return ret;