2 Unix SMB/CIFS implementation.
4 RFC2478 Compliant SPNEGO implementation
6 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 Copyright (C) Stefan Metzmacher <metze@samba.org> 2004-2008
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "../libcli/auth/spnego.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_proto.h"
31 #include "auth/gensec/gensec_toplevel_proto.h"
32 #include "param/param.h"
34 _PUBLIC_ NTSTATUS
gensec_spnego_init(void);
36 enum spnego_state_position
{
46 enum spnego_message_type expected_packet
;
47 enum spnego_state_position state_position
;
48 struct gensec_security
*sub_sec_security
;
49 bool no_response_expected
;
57 static NTSTATUS
gensec_spnego_client_start(struct gensec_security
*gensec_security
)
59 struct spnego_state
*spnego_state
;
61 spnego_state
= talloc(gensec_security
, struct spnego_state
);
63 return NT_STATUS_NO_MEMORY
;
66 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
67 spnego_state
->state_position
= SPNEGO_CLIENT_START
;
68 spnego_state
->sub_sec_security
= NULL
;
69 spnego_state
->no_response_expected
= false;
70 spnego_state
->mech_types
= data_blob(NULL
, 0);
72 gensec_security
->private_data
= spnego_state
;
76 static NTSTATUS
gensec_spnego_server_start(struct gensec_security
*gensec_security
)
78 struct spnego_state
*spnego_state
;
80 spnego_state
= talloc(gensec_security
, struct spnego_state
);
82 return NT_STATUS_NO_MEMORY
;
85 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
86 spnego_state
->state_position
= SPNEGO_SERVER_START
;
87 spnego_state
->sub_sec_security
= NULL
;
88 spnego_state
->no_response_expected
= false;
89 spnego_state
->mech_types
= data_blob(NULL
, 0);
91 gensec_security
->private_data
= spnego_state
;
96 wrappers for the spnego_*() functions
98 static NTSTATUS
gensec_spnego_unseal_packet(struct gensec_security
*gensec_security
,
99 uint8_t *data
, size_t length
,
100 const uint8_t *whole_pdu
, size_t pdu_length
,
101 const DATA_BLOB
*sig
)
103 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
105 if (spnego_state
->state_position
!= SPNEGO_DONE
106 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
107 return NT_STATUS_INVALID_PARAMETER
;
110 return gensec_unseal_packet(spnego_state
->sub_sec_security
,
112 whole_pdu
, pdu_length
,
116 static NTSTATUS
gensec_spnego_check_packet(struct gensec_security
*gensec_security
,
117 const uint8_t *data
, size_t length
,
118 const uint8_t *whole_pdu
, size_t pdu_length
,
119 const DATA_BLOB
*sig
)
121 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
123 if (spnego_state
->state_position
!= SPNEGO_DONE
124 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
125 return NT_STATUS_INVALID_PARAMETER
;
128 return gensec_check_packet(spnego_state
->sub_sec_security
,
130 whole_pdu
, pdu_length
,
134 static NTSTATUS
gensec_spnego_seal_packet(struct gensec_security
*gensec_security
,
136 uint8_t *data
, size_t length
,
137 const uint8_t *whole_pdu
, size_t pdu_length
,
140 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
142 if (spnego_state
->state_position
!= SPNEGO_DONE
143 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
144 return NT_STATUS_INVALID_PARAMETER
;
147 return gensec_seal_packet(spnego_state
->sub_sec_security
,
150 whole_pdu
, pdu_length
,
154 static NTSTATUS
gensec_spnego_sign_packet(struct gensec_security
*gensec_security
,
156 const uint8_t *data
, size_t length
,
157 const uint8_t *whole_pdu
, size_t pdu_length
,
160 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
162 if (spnego_state
->state_position
!= SPNEGO_DONE
163 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
164 return NT_STATUS_INVALID_PARAMETER
;
167 return gensec_sign_packet(spnego_state
->sub_sec_security
,
170 whole_pdu
, pdu_length
,
174 static NTSTATUS
gensec_spnego_wrap(struct gensec_security
*gensec_security
,
179 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
181 if (spnego_state
->state_position
!= SPNEGO_DONE
182 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
183 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
184 return NT_STATUS_INVALID_PARAMETER
;
187 return gensec_wrap(spnego_state
->sub_sec_security
,
191 static NTSTATUS
gensec_spnego_unwrap(struct gensec_security
*gensec_security
,
196 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
198 if (spnego_state
->state_position
!= SPNEGO_DONE
199 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
200 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
201 return NT_STATUS_INVALID_PARAMETER
;
204 return gensec_unwrap(spnego_state
->sub_sec_security
,
208 static NTSTATUS
gensec_spnego_wrap_packets(struct gensec_security
*gensec_security
,
212 size_t *len_processed
)
214 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
216 if (spnego_state
->state_position
!= SPNEGO_DONE
217 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
218 DEBUG(1, ("gensec_spnego_wrap: wrong state for wrap\n"));
219 return NT_STATUS_INVALID_PARAMETER
;
222 return gensec_wrap_packets(spnego_state
->sub_sec_security
,
227 static NTSTATUS
gensec_spnego_packet_full_request(struct gensec_security
*gensec_security
,
228 DATA_BLOB blob
, size_t *size
)
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_unwrap: wrong state for unwrap\n"));
235 return NT_STATUS_INVALID_PARAMETER
;
238 return gensec_packet_full_request(spnego_state
->sub_sec_security
,
242 static NTSTATUS
gensec_spnego_unwrap_packets(struct gensec_security
*gensec_security
,
246 size_t *len_processed
)
248 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
250 if (spnego_state
->state_position
!= SPNEGO_DONE
251 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
252 DEBUG(1, ("gensec_spnego_unwrap: wrong state for unwrap\n"));
253 return NT_STATUS_INVALID_PARAMETER
;
256 return gensec_unwrap_packets(spnego_state
->sub_sec_security
,
261 static size_t gensec_spnego_sig_size(struct gensec_security
*gensec_security
, size_t data_size
)
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
) {
270 return gensec_sig_size(spnego_state
->sub_sec_security
, data_size
);
273 static size_t gensec_spnego_max_input_size(struct gensec_security
*gensec_security
)
275 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
277 if (spnego_state
->state_position
!= SPNEGO_DONE
278 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
282 return gensec_max_input_size(spnego_state
->sub_sec_security
);
285 static size_t gensec_spnego_max_wrapped_size(struct gensec_security
*gensec_security
)
287 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
289 if (spnego_state
->state_position
!= SPNEGO_DONE
290 && spnego_state
->state_position
!= SPNEGO_FALLBACK
) {
294 return gensec_max_wrapped_size(spnego_state
->sub_sec_security
);
297 static NTSTATUS
gensec_spnego_session_key(struct gensec_security
*gensec_security
,
299 DATA_BLOB
*session_key
)
301 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
302 if (!spnego_state
->sub_sec_security
) {
303 return NT_STATUS_INVALID_PARAMETER
;
306 return gensec_session_key(spnego_state
->sub_sec_security
,
311 static NTSTATUS
gensec_spnego_session_info(struct gensec_security
*gensec_security
,
313 struct auth_session_info
**session_info
)
315 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
316 if (!spnego_state
->sub_sec_security
) {
317 return NT_STATUS_INVALID_PARAMETER
;
320 return gensec_session_info(spnego_state
->sub_sec_security
,
325 /** Fallback to another GENSEC mechanism, based on magic strings
327 * This is the 'fallback' case, where we don't get SPNEGO, and have to
328 * try all the other options (and hope they all have a magic string
332 static NTSTATUS
gensec_spnego_server_try_fallback(struct gensec_security
*gensec_security
,
333 struct spnego_state
*spnego_state
,
334 TALLOC_CTX
*out_mem_ctx
,
335 const DATA_BLOB in
, DATA_BLOB
*out
)
338 struct gensec_security_ops
**all_ops
339 = gensec_security_mechs(gensec_security
, out_mem_ctx
);
340 for (i
=0; all_ops
[i
]; i
++) {
344 if (gensec_security
!= NULL
&&
345 !gensec_security_ops_enabled(all_ops
[i
], gensec_security
))
348 if (!all_ops
[i
]->oid
) {
353 for (j
=0; all_ops
[i
]->oid
[j
]; j
++) {
354 if (strcasecmp(GENSEC_OID_SPNEGO
,all_ops
[i
]->oid
[j
]) == 0) {
362 if (!all_ops
[i
]->magic
) {
366 nt_status
= all_ops
[i
]->magic(gensec_security
, &in
);
367 if (!NT_STATUS_IS_OK(nt_status
)) {
371 spnego_state
->state_position
= SPNEGO_FALLBACK
;
373 nt_status
= gensec_subcontext_start(spnego_state
,
375 &spnego_state
->sub_sec_security
);
377 if (!NT_STATUS_IS_OK(nt_status
)) {
380 /* select the sub context */
381 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
383 if (!NT_STATUS_IS_OK(nt_status
)) {
386 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
387 out_mem_ctx
, in
, out
);
390 DEBUG(1, ("Failed to parse SPNEGO request\n"));
391 return NT_STATUS_INVALID_PARAMETER
;
396 Parse the netTokenInit, either from the client, to the server, or
397 from the server to the client.
400 static NTSTATUS
gensec_spnego_parse_negTokenInit(struct gensec_security
*gensec_security
,
401 struct spnego_state
*spnego_state
,
402 TALLOC_CTX
*out_mem_ctx
,
403 const char **mechType
,
404 const DATA_BLOB unwrapped_in
, DATA_BLOB
*unwrapped_out
)
407 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
408 DATA_BLOB null_data_blob
= data_blob(NULL
,0);
411 const struct gensec_security_ops_wrapper
*all_sec
412 = gensec_security_by_oid_list(gensec_security
,
417 ok
= spnego_write_mech_types(spnego_state
,
419 &spnego_state
->mech_types
);
421 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
422 return NT_STATUS_NO_MEMORY
;
425 if (spnego_state
->state_position
== SPNEGO_SERVER_START
) {
427 for (j
=0; mechType
&& mechType
[j
]; j
++) {
428 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
429 nt_status
= gensec_subcontext_start(spnego_state
,
431 &spnego_state
->sub_sec_security
);
432 if (!NT_STATUS_IS_OK(nt_status
)) {
435 /* select the sub context */
436 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
438 if (!NT_STATUS_IS_OK(nt_status
)) {
439 talloc_free(spnego_state
->sub_sec_security
);
440 spnego_state
->sub_sec_security
= NULL
;
445 /* no optimistic token */
446 spnego_state
->neg_oid
= all_sec
[i
].oid
;
447 *unwrapped_out
= data_blob_null
;
448 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
452 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
456 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
) ||
457 NT_STATUS_EQUAL(nt_status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
458 /* Pretend we never started it (lets the first run find some incompatible demand) */
460 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse contents: %s\n",
461 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
462 talloc_free(spnego_state
->sub_sec_security
);
463 spnego_state
->sub_sec_security
= NULL
;
467 spnego_state
->neg_oid
= all_sec
[i
].oid
;
470 if (spnego_state
->sub_sec_security
) {
475 if (!spnego_state
->sub_sec_security
) {
476 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
477 return NT_STATUS_INVALID_PARAMETER
;
481 /* Having tried any optimistic token from the client (if we
482 * were the server), if we didn't get anywhere, walk our list
483 * in our preference order */
485 if (!spnego_state
->sub_sec_security
) {
486 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
487 nt_status
= gensec_subcontext_start(spnego_state
,
489 &spnego_state
->sub_sec_security
);
490 if (!NT_STATUS_IS_OK(nt_status
)) {
493 /* select the sub context */
494 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
496 if (!NT_STATUS_IS_OK(nt_status
)) {
497 talloc_free(spnego_state
->sub_sec_security
);
498 spnego_state
->sub_sec_security
= NULL
;
502 spnego_state
->neg_oid
= all_sec
[i
].oid
;
504 /* only get the helping start blob for the first OID */
505 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
510 /* it is likely that a NULL input token will
511 * not be liked by most server mechs, but if
512 * we are in the client, we want the first
513 * update packet to be able to abort the use
515 if (spnego_state
->state_position
!= SPNEGO_SERVER_START
) {
516 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
) ||
517 NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_LOGON_SERVERS
) ||
518 NT_STATUS_EQUAL(nt_status
, NT_STATUS_TIME_DIFFERENCE_AT_DC
) ||
519 NT_STATUS_EQUAL(nt_status
, NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
520 /* Pretend we never started it (lets the first run find some incompatible demand) */
522 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed to parse: %s\n",
523 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
524 talloc_free(spnego_state
->sub_sec_security
);
525 spnego_state
->sub_sec_security
= NULL
;
534 if (spnego_state
->sub_sec_security
) {
535 /* it is likely that a NULL input token will
536 * not be liked by most server mechs, but this
537 * does the right thing in the CIFS client.
538 * just push us along the merry-go-round
539 * again, and hope for better luck next
542 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
)) {
543 *unwrapped_out
= data_blob(NULL
, 0);
544 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
547 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_PARAMETER
)
548 && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
549 && !NT_STATUS_IS_OK(nt_status
)) {
550 DEBUG(1, ("SPNEGO(%s) NEG_TOKEN_INIT failed: %s\n",
551 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
552 talloc_free(spnego_state
->sub_sec_security
);
553 spnego_state
->sub_sec_security
= NULL
;
555 /* We started the mech correctly, and the
556 * input from the other side was valid.
557 * Return the error (say bad password, invalid
563 return nt_status
; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
566 DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
567 /* we could re-negotiate here, but it would only work
568 * if the client or server lied about what it could
569 * support the first time. Lets keep this code to
575 /** create a negTokenInit
577 * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
579 static NTSTATUS
gensec_spnego_create_negTokenInit(struct gensec_security
*gensec_security
,
580 struct spnego_state
*spnego_state
,
581 TALLOC_CTX
*out_mem_ctx
,
582 const DATA_BLOB in
, DATA_BLOB
*out
)
585 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
586 DATA_BLOB null_data_blob
= data_blob(NULL
,0);
587 const char **mechTypes
= NULL
;
588 DATA_BLOB unwrapped_out
= data_blob(NULL
, 0);
589 const struct gensec_security_ops_wrapper
*all_sec
;
591 mechTypes
= gensec_security_oids(gensec_security
,
592 out_mem_ctx
, GENSEC_OID_SPNEGO
);
594 all_sec
= gensec_security_by_oid_list(gensec_security
,
598 for (i
=0; all_sec
&& all_sec
[i
].op
; i
++) {
599 struct spnego_data spnego_out
;
600 const char **send_mech_types
;
603 nt_status
= gensec_subcontext_start(spnego_state
,
605 &spnego_state
->sub_sec_security
);
606 if (!NT_STATUS_IS_OK(nt_status
)) {
609 /* select the sub context */
610 nt_status
= gensec_start_mech_by_ops(spnego_state
->sub_sec_security
,
612 if (!NT_STATUS_IS_OK(nt_status
)) {
613 talloc_free(spnego_state
->sub_sec_security
);
614 spnego_state
->sub_sec_security
= NULL
;
618 /* In the client, try and produce the first (optimistic) packet */
619 if (spnego_state
->state_position
== SPNEGO_CLIENT_START
) {
620 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
625 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
626 && !NT_STATUS_IS_OK(nt_status
)) {
627 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n",
628 spnego_state
->sub_sec_security
->ops
->name
, nt_errstr(nt_status
)));
629 talloc_free(spnego_state
->sub_sec_security
);
630 spnego_state
->sub_sec_security
= NULL
;
631 /* Pretend we never started it (lets the first run find some incompatible demand) */
637 spnego_out
.type
= SPNEGO_NEG_TOKEN_INIT
;
639 send_mech_types
= gensec_security_oids_from_ops_wrapped(out_mem_ctx
,
642 ok
= spnego_write_mech_types(spnego_state
,
644 &spnego_state
->mech_types
);
646 DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
647 return NT_STATUS_NO_MEMORY
;
650 /* List the remaining mechs as options */
651 spnego_out
.negTokenInit
.mechTypes
= send_mech_types
;
652 spnego_out
.negTokenInit
.reqFlags
= null_data_blob
;
653 spnego_out
.negTokenInit
.reqFlagsPadding
= 0;
655 if (spnego_state
->state_position
== SPNEGO_SERVER_START
) {
656 spnego_out
.negTokenInit
.mechListMIC
657 = data_blob_string_const(ADS_IGNORE_PRINCIPAL
);
659 spnego_out
.negTokenInit
.mechListMIC
= null_data_blob
;
662 spnego_out
.negTokenInit
.mechToken
= unwrapped_out
;
664 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
665 DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
666 return NT_STATUS_INVALID_PARAMETER
;
670 spnego_state
->neg_oid
= all_sec
[i
].oid
;
672 if (NT_STATUS_IS_OK(nt_status
)) {
673 spnego_state
->no_response_expected
= true;
676 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
678 talloc_free(spnego_state
->sub_sec_security
);
679 spnego_state
->sub_sec_security
= NULL
;
681 DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status
)));
682 return NT_STATUS_INVALID_PARAMETER
;
686 /** create a server negTokenTarg
688 * This is the case, where the client is the first one who sends data
691 static NTSTATUS
gensec_spnego_server_negTokenTarg(struct gensec_security
*gensec_security
,
692 struct spnego_state
*spnego_state
,
693 TALLOC_CTX
*out_mem_ctx
,
695 const DATA_BLOB unwrapped_out
,
696 DATA_BLOB mech_list_mic
,
699 struct spnego_data spnego_out
;
700 DATA_BLOB null_data_blob
= data_blob(NULL
, 0);
703 spnego_out
.type
= SPNEGO_NEG_TOKEN_TARG
;
704 spnego_out
.negTokenTarg
.responseToken
= unwrapped_out
;
705 spnego_out
.negTokenTarg
.mechListMIC
= null_data_blob
;
706 spnego_out
.negTokenTarg
.supportedMech
= NULL
;
708 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
709 spnego_out
.negTokenTarg
.supportedMech
= spnego_state
->neg_oid
;
710 spnego_out
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
711 spnego_state
->state_position
= SPNEGO_SERVER_TARG
;
712 } else if (NT_STATUS_IS_OK(nt_status
)) {
713 if (unwrapped_out
.data
) {
714 spnego_out
.negTokenTarg
.supportedMech
= spnego_state
->neg_oid
;
716 spnego_out
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
717 spnego_out
.negTokenTarg
.mechListMIC
= mech_list_mic
;
718 spnego_state
->state_position
= SPNEGO_DONE
;
720 spnego_out
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
721 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status
)));
722 spnego_state
->state_position
= SPNEGO_DONE
;
725 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
726 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
727 return NT_STATUS_INVALID_PARAMETER
;
730 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
736 static NTSTATUS
gensec_spnego_update(struct gensec_security
*gensec_security
, TALLOC_CTX
*out_mem_ctx
,
737 const DATA_BLOB in
, DATA_BLOB
*out
)
739 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
740 DATA_BLOB null_data_blob
= data_blob(NULL
, 0);
741 DATA_BLOB mech_list_mic
= data_blob(NULL
, 0);
742 DATA_BLOB unwrapped_out
= data_blob(NULL
, 0);
743 struct spnego_data spnego_out
;
744 struct spnego_data spnego
;
748 *out
= data_blob(NULL
, 0);
751 out_mem_ctx
= spnego_state
;
754 /* and switch into the state machine */
756 switch (spnego_state
->state_position
) {
757 case SPNEGO_FALLBACK
:
758 return gensec_update(spnego_state
->sub_sec_security
,
759 out_mem_ctx
, in
, out
);
760 case SPNEGO_SERVER_START
:
765 len
= spnego_read_data(gensec_security
, in
, &spnego
);
767 return gensec_spnego_server_try_fallback(gensec_security
, spnego_state
,
768 out_mem_ctx
, in
, out
);
770 /* client sent NegTargetInit, we send NegTokenTarg */
772 /* OK, so it's real SPNEGO, check the packet's the one we expect */
773 if (spnego
.type
!= spnego_state
->expected_packet
) {
774 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
775 spnego_state
->expected_packet
));
776 dump_data(1, in
.data
, in
.length
);
777 spnego_free_data(&spnego
);
778 return NT_STATUS_INVALID_PARAMETER
;
781 nt_status
= gensec_spnego_parse_negTokenInit(gensec_security
,
784 spnego
.negTokenInit
.mechTypes
,
785 spnego
.negTokenInit
.mechToken
,
788 nt_status
= gensec_spnego_server_negTokenTarg(gensec_security
,
796 spnego_free_data(&spnego
);
800 nt_status
= gensec_spnego_create_negTokenInit(gensec_security
, spnego_state
,
801 out_mem_ctx
, in
, out
);
802 spnego_state
->state_position
= SPNEGO_SERVER_START
;
803 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_INIT
;
808 case SPNEGO_CLIENT_START
:
810 /* The server offers a list of mechanisms */
812 const char *my_mechs
[] = {NULL
, NULL
};
813 NTSTATUS nt_status
= NT_STATUS_INVALID_PARAMETER
;
816 /* client to produce negTokenInit */
817 nt_status
= gensec_spnego_create_negTokenInit(gensec_security
, spnego_state
,
818 out_mem_ctx
, in
, out
);
819 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
820 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
824 len
= spnego_read_data(gensec_security
, in
, &spnego
);
827 DEBUG(1, ("Invalid SPNEGO request:\n"));
828 dump_data(1, in
.data
, in
.length
);
829 return NT_STATUS_INVALID_PARAMETER
;
832 /* OK, so it's real SPNEGO, check the packet's the one we expect */
833 if (spnego
.type
!= spnego_state
->expected_packet
) {
834 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
835 spnego_state
->expected_packet
));
836 dump_data(1, in
.data
, in
.length
);
837 spnego_free_data(&spnego
);
838 return NT_STATUS_INVALID_PARAMETER
;
841 if (spnego
.negTokenInit
.targetPrincipal
842 && strcmp(spnego
.negTokenInit
.targetPrincipal
, ADS_IGNORE_PRINCIPAL
) != 0) {
843 DEBUG(5, ("Server claims it's principal name is %s\n", spnego
.negTokenInit
.targetPrincipal
));
844 if (lpcfg_client_use_spnego_principal(gensec_security
->settings
->lp_ctx
)) {
845 gensec_set_target_principal(gensec_security
, spnego
.negTokenInit
.targetPrincipal
);
849 nt_status
= gensec_spnego_parse_negTokenInit(gensec_security
,
852 spnego
.negTokenInit
.mechTypes
,
853 spnego
.negTokenInit
.mechToken
,
856 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) && !NT_STATUS_IS_OK(nt_status
)) {
857 spnego_free_data(&spnego
);
861 my_mechs
[0] = spnego_state
->neg_oid
;
863 spnego_out
.type
= SPNEGO_NEG_TOKEN_INIT
;
864 spnego_out
.negTokenInit
.mechTypes
= my_mechs
;
865 spnego_out
.negTokenInit
.reqFlags
= null_data_blob
;
866 spnego_out
.negTokenInit
.reqFlagsPadding
= 0;
867 spnego_out
.negTokenInit
.mechListMIC
= null_data_blob
;
868 spnego_out
.negTokenInit
.mechToken
= unwrapped_out
;
870 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
871 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
872 return NT_STATUS_INVALID_PARAMETER
;
876 spnego_state
->expected_packet
= SPNEGO_NEG_TOKEN_TARG
;
877 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
879 if (NT_STATUS_IS_OK(nt_status
)) {
880 spnego_state
->no_response_expected
= true;
883 spnego_free_data(&spnego
);
884 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
886 case SPNEGO_SERVER_TARG
:
889 bool new_spnego
= false;
892 return NT_STATUS_INVALID_PARAMETER
;
895 len
= spnego_read_data(gensec_security
, in
, &spnego
);
898 DEBUG(1, ("Invalid SPNEGO request:\n"));
899 dump_data(1, in
.data
, in
.length
);
900 return NT_STATUS_INVALID_PARAMETER
;
903 /* OK, so it's real SPNEGO, check the packet's the one we expect */
904 if (spnego
.type
!= spnego_state
->expected_packet
) {
905 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
906 spnego_state
->expected_packet
));
907 dump_data(1, in
.data
, in
.length
);
908 spnego_free_data(&spnego
);
909 return NT_STATUS_INVALID_PARAMETER
;
912 if (!spnego_state
->sub_sec_security
) {
913 DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
914 spnego_free_data(&spnego
);
915 return NT_STATUS_INVALID_PARAMETER
;
918 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
920 spnego
.negTokenTarg
.responseToken
,
922 if (NT_STATUS_IS_OK(nt_status
) && spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
924 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
925 spnego_state
->mech_types
.data
,
926 spnego_state
->mech_types
.length
,
927 spnego_state
->mech_types
.data
,
928 spnego_state
->mech_types
.length
,
929 &spnego
.negTokenTarg
.mechListMIC
);
930 if (!NT_STATUS_IS_OK(nt_status
)) {
931 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
932 nt_errstr(nt_status
)));
935 if (NT_STATUS_IS_OK(nt_status
) && new_spnego
) {
936 nt_status
= gensec_sign_packet(spnego_state
->sub_sec_security
,
938 spnego_state
->mech_types
.data
,
939 spnego_state
->mech_types
.length
,
940 spnego_state
->mech_types
.data
,
941 spnego_state
->mech_types
.length
,
943 if (!NT_STATUS_IS_OK(nt_status
)) {
944 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
945 nt_errstr(nt_status
)));
949 nt_status
= gensec_spnego_server_negTokenTarg(gensec_security
,
957 spnego_free_data(&spnego
);
961 case SPNEGO_CLIENT_TARG
:
965 return NT_STATUS_INVALID_PARAMETER
;
968 len
= spnego_read_data(gensec_security
, in
, &spnego
);
971 DEBUG(1, ("Invalid SPNEGO request:\n"));
972 dump_data(1, in
.data
, in
.length
);
973 return NT_STATUS_INVALID_PARAMETER
;
976 /* OK, so it's real SPNEGO, check the packet's the one we expect */
977 if (spnego
.type
!= spnego_state
->expected_packet
) {
978 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego
.type
,
979 spnego_state
->expected_packet
));
980 dump_data(1, in
.data
, in
.length
);
981 spnego_free_data(&spnego
);
982 return NT_STATUS_INVALID_PARAMETER
;
985 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
986 spnego_free_data(&spnego
);
987 return NT_STATUS_ACCESS_DENIED
;
990 /* Server didn't like our choice of mech, and chose something else */
991 if ((spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_INCOMPLETE
) &&
992 spnego
.negTokenTarg
.supportedMech
&&
993 strcmp(spnego
.negTokenTarg
.supportedMech
, spnego_state
->neg_oid
) != 0) {
994 DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
995 gensec_get_name_by_oid(gensec_security
, spnego
.negTokenTarg
.supportedMech
),
996 gensec_get_name_by_oid(gensec_security
, spnego_state
->neg_oid
)));
998 talloc_free(spnego_state
->sub_sec_security
);
999 nt_status
= gensec_subcontext_start(spnego_state
,
1001 &spnego_state
->sub_sec_security
);
1002 if (!NT_STATUS_IS_OK(nt_status
)) {
1003 spnego_free_data(&spnego
);
1006 /* select the sub context */
1007 nt_status
= gensec_start_mech_by_oid(spnego_state
->sub_sec_security
,
1008 spnego
.negTokenTarg
.supportedMech
);
1009 if (!NT_STATUS_IS_OK(nt_status
)) {
1010 spnego_free_data(&spnego
);
1014 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
1016 spnego
.negTokenTarg
.responseToken
,
1018 spnego_state
->neg_oid
= talloc_strdup(spnego_state
, spnego
.negTokenTarg
.supportedMech
);
1019 } else if (spnego_state
->no_response_expected
) {
1020 if (spnego
.negTokenTarg
.negResult
!= SPNEGO_ACCEPT_COMPLETED
) {
1021 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1022 nt_status
= NT_STATUS_INVALID_PARAMETER
;
1023 } else if (spnego
.negTokenTarg
.responseToken
.length
) {
1024 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1025 nt_status
= NT_STATUS_INVALID_PARAMETER
;
1027 nt_status
= NT_STATUS_OK
;
1029 if (NT_STATUS_IS_OK(nt_status
) && spnego
.negTokenTarg
.mechListMIC
.length
> 0) {
1030 nt_status
= gensec_check_packet(spnego_state
->sub_sec_security
,
1031 spnego_state
->mech_types
.data
,
1032 spnego_state
->mech_types
.length
,
1033 spnego_state
->mech_types
.data
,
1034 spnego_state
->mech_types
.length
,
1035 &spnego
.negTokenTarg
.mechListMIC
);
1036 if (!NT_STATUS_IS_OK(nt_status
)) {
1037 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1038 nt_errstr(nt_status
)));
1042 bool new_spnego
= false;
1044 nt_status
= gensec_update(spnego_state
->sub_sec_security
,
1046 spnego
.negTokenTarg
.responseToken
,
1049 if (NT_STATUS_IS_OK(nt_status
)
1050 && spnego
.negTokenTarg
.negResult
!= SPNEGO_ACCEPT_COMPLETED
) {
1051 new_spnego
= gensec_have_feature(spnego_state
->sub_sec_security
,
1052 GENSEC_FEATURE_NEW_SPNEGO
);
1054 if (NT_STATUS_IS_OK(nt_status
) && new_spnego
) {
1055 nt_status
= gensec_sign_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
,
1062 if (!NT_STATUS_IS_OK(nt_status
)) {
1063 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1064 nt_errstr(nt_status
)));
1067 if (NT_STATUS_IS_OK(nt_status
)) {
1068 spnego_state
->no_response_expected
= true;
1072 spnego_free_data(&spnego
);
1074 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)
1075 && !NT_STATUS_IS_OK(nt_status
)) {
1076 DEBUG(1, ("SPNEGO(%s) login failed: %s\n",
1077 spnego_state
->sub_sec_security
->ops
->name
,
1078 nt_errstr(nt_status
)));
1082 if (unwrapped_out
.length
|| mech_list_mic
.length
) {
1084 spnego_out
.type
= SPNEGO_NEG_TOKEN_TARG
;
1085 spnego_out
.negTokenTarg
.negResult
= SPNEGO_NONE_RESULT
;
1086 spnego_out
.negTokenTarg
.supportedMech
= NULL
;
1087 spnego_out
.negTokenTarg
.responseToken
= unwrapped_out
;
1088 spnego_out
.negTokenTarg
.mechListMIC
= mech_list_mic
;
1090 if (spnego_write_data(out_mem_ctx
, out
, &spnego_out
) == -1) {
1091 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1092 return NT_STATUS_INVALID_PARAMETER
;
1095 spnego_state
->state_position
= SPNEGO_CLIENT_TARG
;
1096 nt_status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1099 /* all done - server has accepted, and we agree */
1100 *out
= null_data_blob
;
1102 if (spnego
.negTokenTarg
.negResult
!= SPNEGO_ACCEPT_COMPLETED
) {
1103 /* unless of course it did not accept */
1104 DEBUG(1,("gensec_update ok but not accepted\n"));
1105 nt_status
= NT_STATUS_INVALID_PARAMETER
;
1108 spnego_state
->state_position
= SPNEGO_DONE
;
1114 /* We should not be called after we are 'done' */
1115 return NT_STATUS_INVALID_PARAMETER
;
1117 return NT_STATUS_INVALID_PARAMETER
;
1120 static void gensec_spnego_want_feature(struct gensec_security
*gensec_security
,
1123 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1125 if (!spnego_state
|| !spnego_state
->sub_sec_security
) {
1126 gensec_security
->want_features
|= feature
;
1130 gensec_want_feature(spnego_state
->sub_sec_security
,
1134 static bool gensec_spnego_have_feature(struct gensec_security
*gensec_security
,
1137 struct spnego_state
*spnego_state
= (struct spnego_state
*)gensec_security
->private_data
;
1138 if (!spnego_state
->sub_sec_security
) {
1142 return gensec_have_feature(spnego_state
->sub_sec_security
,
1146 static const char *gensec_spnego_oids
[] = {
1151 static const struct gensec_security_ops gensec_spnego_security_ops
= {
1153 .sasl_name
= "GSS-SPNEGO",
1154 .auth_type
= DCERPC_AUTH_TYPE_SPNEGO
,
1155 .oid
= gensec_spnego_oids
,
1156 .client_start
= gensec_spnego_client_start
,
1157 .server_start
= gensec_spnego_server_start
,
1158 .update
= gensec_spnego_update
,
1159 .seal_packet
= gensec_spnego_seal_packet
,
1160 .sign_packet
= gensec_spnego_sign_packet
,
1161 .sig_size
= gensec_spnego_sig_size
,
1162 .max_wrapped_size
= gensec_spnego_max_wrapped_size
,
1163 .max_input_size
= gensec_spnego_max_input_size
,
1164 .check_packet
= gensec_spnego_check_packet
,
1165 .unseal_packet
= gensec_spnego_unseal_packet
,
1166 .packet_full_request
= gensec_spnego_packet_full_request
,
1167 .wrap
= gensec_spnego_wrap
,
1168 .unwrap
= gensec_spnego_unwrap
,
1169 .wrap_packets
= gensec_spnego_wrap_packets
,
1170 .unwrap_packets
= gensec_spnego_unwrap_packets
,
1171 .session_key
= gensec_spnego_session_key
,
1172 .session_info
= gensec_spnego_session_info
,
1173 .want_feature
= gensec_spnego_want_feature
,
1174 .have_feature
= gensec_spnego_have_feature
,
1176 .priority
= GENSEC_SPNEGO
1179 _PUBLIC_ NTSTATUS
gensec_spnego_init(void)
1182 ret
= gensec_register(&gensec_spnego_security_ops
);
1183 if (!NT_STATUS_IS_OK(ret
)) {
1184 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1185 gensec_spnego_security_ops
.name
));