auth/gensec: introduce gensec_internal.h
[metze/samba/wip.git] / auth / gensec / spnego.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    RFC2478 Compliant SPNEGO implementation
5
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
9
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.
14
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.
19
20
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/>.
23 */
24
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"
33
34 #undef strcasecmp
35
36 _PUBLIC_ NTSTATUS gensec_spnego_init(void);
37
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
45 };
46
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;
52
53         const char *neg_oid;
54
55         DATA_BLOB mech_types;
56
57         /*
58          * The following is used to implement
59          * the update token fragmentation
60          */
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;
66 };
67
68
69 static NTSTATUS gensec_spnego_client_start(struct gensec_security *gensec_security)
70 {
71         struct spnego_state *spnego_state;
72
73         spnego_state = talloc_zero(gensec_security, struct spnego_state);
74         if (!spnego_state) {
75                 return NT_STATUS_NO_MEMORY;
76         }
77
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;
85
86         gensec_security->private_data = spnego_state;
87         return NT_STATUS_OK;
88 }
89
90 static NTSTATUS gensec_spnego_server_start(struct gensec_security *gensec_security)
91 {
92         struct spnego_state *spnego_state;
93
94         spnego_state = talloc_zero(gensec_security, struct spnego_state);
95         if (!spnego_state) {
96                 return NT_STATUS_NO_MEMORY;
97         }
98
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;
106
107         gensec_security->private_data = spnego_state;
108         return NT_STATUS_OK;
109 }
110
111 /*
112   wrappers for the spnego_*() functions
113 */
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)
118 {
119         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
120
121         if (spnego_state->state_position != SPNEGO_DONE 
122             && spnego_state->state_position != SPNEGO_FALLBACK) {
123                 return NT_STATUS_INVALID_PARAMETER;
124         }
125
126         return gensec_unseal_packet(spnego_state->sub_sec_security, 
127                                     data, length, 
128                                     whole_pdu, pdu_length,
129                                     sig); 
130 }
131
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)
136 {
137         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
138
139         if (spnego_state->state_position != SPNEGO_DONE 
140             && spnego_state->state_position != SPNEGO_FALLBACK) {
141                 return NT_STATUS_INVALID_PARAMETER;
142         }
143
144         return gensec_check_packet(spnego_state->sub_sec_security, 
145                                    data, length, 
146                                    whole_pdu, pdu_length,
147                                    sig);
148 }
149
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)
155 {
156         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
157
158         if (spnego_state->state_position != SPNEGO_DONE 
159             && spnego_state->state_position != SPNEGO_FALLBACK) {
160                 return NT_STATUS_INVALID_PARAMETER;
161         }
162
163         return gensec_seal_packet(spnego_state->sub_sec_security, 
164                                   mem_ctx, 
165                                   data, length, 
166                                   whole_pdu, pdu_length,
167                                   sig);
168 }
169
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)
175 {
176         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
177
178         if (spnego_state->state_position != SPNEGO_DONE 
179             && spnego_state->state_position != SPNEGO_FALLBACK) {
180                 return NT_STATUS_INVALID_PARAMETER;
181         }
182
183         return gensec_sign_packet(spnego_state->sub_sec_security, 
184                                   mem_ctx, 
185                                   data, length, 
186                                   whole_pdu, pdu_length,
187                                   sig);
188 }
189
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)
194 {
195         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
196
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;
201         }
202
203         return gensec_wrap(spnego_state->sub_sec_security, 
204                            mem_ctx, in, out);
205 }
206
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)
211 {
212         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
213
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;
218         }
219
220         return gensec_unwrap(spnego_state->sub_sec_security, 
221                              mem_ctx, in, out);
222 }
223
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) 
229 {
230         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
231
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;
236         }
237
238         return gensec_wrap_packets(spnego_state->sub_sec_security, 
239                                    mem_ctx, in, out,
240                                    len_processed);
241 }
242
243 static NTSTATUS gensec_spnego_packet_full_request(struct gensec_security *gensec_security, 
244                                                 DATA_BLOB blob, size_t *size)
245 {
246         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
247
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;
252         }
253
254         return gensec_packet_full_request(spnego_state->sub_sec_security, 
255                                           blob, size);
256 }
257
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) 
263 {
264         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
265
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;
270         }
271
272         return gensec_unwrap_packets(spnego_state->sub_sec_security, 
273                                      mem_ctx, in, out,
274                                      len_processed);
275 }
276
277 static size_t gensec_spnego_sig_size(struct gensec_security *gensec_security, size_t data_size) 
278 {
279         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
280
281         if (spnego_state->state_position != SPNEGO_DONE 
282             && spnego_state->state_position != SPNEGO_FALLBACK) {
283                 return 0;
284         }
285
286         return gensec_sig_size(spnego_state->sub_sec_security, data_size);
287 }
288
289 static size_t gensec_spnego_max_input_size(struct gensec_security *gensec_security) 
290 {
291         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
292
293         if (spnego_state->state_position != SPNEGO_DONE 
294             && spnego_state->state_position != SPNEGO_FALLBACK) {
295                 return 0;
296         }
297
298         return gensec_max_input_size(spnego_state->sub_sec_security);
299 }
300
301 static size_t gensec_spnego_max_wrapped_size(struct gensec_security *gensec_security) 
302 {
303         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
304
305         if (spnego_state->state_position != SPNEGO_DONE 
306             && spnego_state->state_position != SPNEGO_FALLBACK) {
307                 return 0;
308         }
309
310         return gensec_max_wrapped_size(spnego_state->sub_sec_security);
311 }
312
313 static NTSTATUS gensec_spnego_session_key(struct gensec_security *gensec_security, 
314                                           TALLOC_CTX *mem_ctx,
315                                           DATA_BLOB *session_key)
316 {
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;
320         }
321
322         return gensec_session_key(spnego_state->sub_sec_security, 
323                                   mem_ctx,
324                                   session_key);
325 }
326
327 static NTSTATUS gensec_spnego_session_info(struct gensec_security *gensec_security,
328                                            TALLOC_CTX *mem_ctx,
329                                            struct auth_session_info **session_info)
330 {
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;
334         }
335
336         return gensec_session_info(spnego_state->sub_sec_security, 
337                                    mem_ctx,
338                                    session_info);
339 }
340
341 /** Fallback to another GENSEC mechanism, based on magic strings 
342  *
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)
346 */
347
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) 
353 {
354         int i,j;
355         struct gensec_security_ops **all_ops
356                 = gensec_security_mechs(gensec_security, out_mem_ctx);
357         for (i=0; all_ops[i]; i++) {
358                 bool is_spnego;
359                 NTSTATUS nt_status;
360
361                 if (gensec_security != NULL && 
362                                 !gensec_security_ops_enabled(all_ops[i], gensec_security))
363                     continue;
364
365                 if (!all_ops[i]->oid) {
366                         continue;
367                 }
368
369                 is_spnego = false;
370                 for (j=0; all_ops[i]->oid[j]; j++) {
371                         if (strcasecmp(GENSEC_OID_SPNEGO,all_ops[i]->oid[j]) == 0) {
372                                 is_spnego = true;
373                         }
374                 }
375                 if (is_spnego) {
376                         continue;
377                 }
378
379                 if (!all_ops[i]->magic) {
380                         continue;
381                 }
382
383                 nt_status = all_ops[i]->magic(gensec_security, &in);
384                 if (!NT_STATUS_IS_OK(nt_status)) {
385                         continue;
386                 }
387
388                 spnego_state->state_position = SPNEGO_FALLBACK;
389
390                 nt_status = gensec_subcontext_start(spnego_state, 
391                                                     gensec_security, 
392                                                     &spnego_state->sub_sec_security);
393
394                 if (!NT_STATUS_IS_OK(nt_status)) {
395                         return nt_status;
396                 }
397                 /* select the sub context */
398                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
399                                                      all_ops[i]);
400                 if (!NT_STATUS_IS_OK(nt_status)) {
401                         return nt_status;
402                 }
403                 nt_status = gensec_update(spnego_state->sub_sec_security,
404                                           ev, out_mem_ctx, in, out);
405                 return nt_status;
406         }
407         DEBUG(1, ("Failed to parse SPNEGO request\n"));
408         return NT_STATUS_INVALID_PARAMETER;
409 }
410
411 /* 
412    Parse the netTokenInit, either from the client, to the server, or
413    from the server to the client.
414 */
415
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) 
422 {
423         int i;
424         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
425         DATA_BLOB null_data_blob = data_blob(NULL,0);
426         bool ok;
427
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);
433
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;
440         }
441
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;
448                                 }
449
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;
455                                 }
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;
463                                 }
464
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;
471                                 }
472
473                                 nt_status = gensec_update(spnego_state->sub_sec_security,
474                                                           out_mem_ctx, 
475                                                           ev,
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) */
481
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;
487                                 }
488
489                                 spnego_state->neg_oid = all_sec[i].oid;
490                                 break;
491                         }
492                         if (spnego_state->sub_sec_security) {
493                                 break;
494                         }
495                 }
496
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;
500                 }
501         }
502
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 */
506
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;
514                         }
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;
522                         }
523
524                         spnego_state->neg_oid = all_sec[i].oid;
525
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, 
529                                                   ev,
530                                                   null_data_blob, 
531                                                   unwrapped_out);
532
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) */
544
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;
550                                 }
551                         }
552
553                         break;
554                 }
555         }
556
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 */
564
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;
568                 }
569
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;
577
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;
583                 }
584
585                 return nt_status; /* OK, INVALID_PARAMETER ore MORE PROCESSING */
586         }
587
588         DEBUG(1, ("SPNEGO: Could not find a suitable mechtype in NEG_TOKEN_INIT\n"));
589         /* we could re-negotiate here, but it would only work
590          * if the client or server lied about what it could
591          * support the first time.  Lets keep this code to
592          * reality */
593
594         return nt_status;
595 }
596
597 /** create a negTokenInit 
598  *
599  * This is the same packet, no matter if the client or server sends it first, but it is always the first packet
600 */
601 static NTSTATUS gensec_spnego_create_negTokenInit(struct gensec_security *gensec_security, 
602                                                   struct spnego_state *spnego_state,
603                                                   TALLOC_CTX *out_mem_ctx, 
604                                                   struct tevent_context *ev,
605                                                   const DATA_BLOB in, DATA_BLOB *out) 
606 {
607         int i;
608         NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
609         DATA_BLOB null_data_blob = data_blob(NULL,0);
610         const char **mechTypes = NULL;
611         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
612         const struct gensec_security_ops_wrapper *all_sec;
613
614         mechTypes = gensec_security_oids(gensec_security, 
615                                          out_mem_ctx, GENSEC_OID_SPNEGO);
616
617         all_sec = gensec_security_by_oid_list(gensec_security, 
618                                               out_mem_ctx, 
619                                               mechTypes,
620                                               GENSEC_OID_SPNEGO);
621         for (i=0; all_sec && all_sec[i].op; i++) {
622                 struct spnego_data spnego_out;
623                 const char **send_mech_types;
624                 bool ok;
625
626                 nt_status = gensec_subcontext_start(spnego_state,
627                                                     gensec_security,
628                                                     &spnego_state->sub_sec_security);
629                 if (!NT_STATUS_IS_OK(nt_status)) {
630                         return nt_status;
631                 }
632                 /* select the sub context */
633                 nt_status = gensec_start_mech_by_ops(spnego_state->sub_sec_security,
634                                                      all_sec[i].op);
635                 if (!NT_STATUS_IS_OK(nt_status)) {
636                         talloc_free(spnego_state->sub_sec_security);
637                         spnego_state->sub_sec_security = NULL;
638                         continue;
639                 }
640
641                 /* In the client, try and produce the first (optimistic) packet */
642                 if (spnego_state->state_position == SPNEGO_CLIENT_START) {
643                         nt_status = gensec_update(spnego_state->sub_sec_security,
644                                                   out_mem_ctx, 
645                                                   ev,
646                                                   null_data_blob,
647                                                   &unwrapped_out);
648
649                         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
650                             && !NT_STATUS_IS_OK(nt_status)) {
651                                 DEBUG(1, ("SPNEGO(%s) creating NEG_TOKEN_INIT failed: %s\n", 
652                                           spnego_state->sub_sec_security->ops->name, nt_errstr(nt_status)));
653                                 talloc_free(spnego_state->sub_sec_security);
654                                 spnego_state->sub_sec_security = NULL;
655                                 /* Pretend we never started it (lets the first run find some incompatible demand) */
656
657                                 continue;
658                         }
659                 }
660
661                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
662
663                 send_mech_types = gensec_security_oids_from_ops_wrapped(out_mem_ctx,
664                                                                         &all_sec[i]);
665
666                 ok = spnego_write_mech_types(spnego_state,
667                                              send_mech_types,
668                                              &spnego_state->mech_types);
669                 if (!ok) {
670                         DEBUG(1, ("SPNEGO: Failed to write mechTypes\n"));
671                         return NT_STATUS_NO_MEMORY;
672                 }
673
674                 /* List the remaining mechs as options */
675                 spnego_out.negTokenInit.mechTypes = send_mech_types;
676                 spnego_out.negTokenInit.reqFlags = null_data_blob;
677                 spnego_out.negTokenInit.reqFlagsPadding = 0;
678
679                 if (spnego_state->state_position == SPNEGO_SERVER_START) {
680                         spnego_out.negTokenInit.mechListMIC
681                                 = data_blob_string_const(ADS_IGNORE_PRINCIPAL);
682                 } else {
683                         spnego_out.negTokenInit.mechListMIC = null_data_blob;
684                 }
685
686                 spnego_out.negTokenInit.mechToken = unwrapped_out;
687
688                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
689                         DEBUG(1, ("Failed to write NEG_TOKEN_INIT\n"));
690                                 return NT_STATUS_INVALID_PARAMETER;
691                 }
692
693                 /* set next state */
694                 spnego_state->neg_oid = all_sec[i].oid;
695
696                 if (NT_STATUS_IS_OK(nt_status)) {
697                         spnego_state->no_response_expected = true;
698                 }
699
700                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
701         } 
702         talloc_free(spnego_state->sub_sec_security);
703         spnego_state->sub_sec_security = NULL;
704
705         DEBUG(1, ("Failed to setup SPNEGO negTokenInit request: %s\n", nt_errstr(nt_status)));
706         return NT_STATUS_INVALID_PARAMETER;
707 }
708
709
710 /** create a server negTokenTarg 
711  *
712  * This is the case, where the client is the first one who sends data
713 */
714
715 static NTSTATUS gensec_spnego_server_negTokenTarg(struct spnego_state *spnego_state,
716                                                   TALLOC_CTX *out_mem_ctx, 
717                                                   NTSTATUS nt_status,
718                                                   const DATA_BLOB unwrapped_out,
719                                                   DATA_BLOB mech_list_mic,
720                                                   DATA_BLOB *out)
721 {
722         struct spnego_data spnego_out;
723         DATA_BLOB null_data_blob = data_blob(NULL, 0);
724
725         /* compose reply */
726         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
727         spnego_out.negTokenTarg.responseToken = unwrapped_out;
728         spnego_out.negTokenTarg.mechListMIC = null_data_blob;
729         spnego_out.negTokenTarg.supportedMech = NULL;
730
731         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {   
732                 spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
733                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
734                 spnego_state->state_position = SPNEGO_SERVER_TARG;
735         } else if (NT_STATUS_IS_OK(nt_status)) {
736                 if (unwrapped_out.data) {
737                         spnego_out.negTokenTarg.supportedMech = spnego_state->neg_oid;
738                 }
739                 spnego_out.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
740                 spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
741                 spnego_state->state_position = SPNEGO_DONE;
742         } else {
743                 spnego_out.negTokenTarg.negResult = SPNEGO_REJECT;
744                 DEBUG(2, ("SPNEGO login failed: %s\n", nt_errstr(nt_status)));
745                 spnego_state->state_position = SPNEGO_DONE;
746         }
747
748         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
749                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
750                 return NT_STATUS_INVALID_PARAMETER;
751         }
752
753         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
754
755         return nt_status;
756 }
757
758
759 static NTSTATUS gensec_spnego_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
760                                      struct tevent_context *ev,
761                                      const DATA_BLOB in, DATA_BLOB *out) 
762 {
763         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
764         DATA_BLOB null_data_blob = data_blob(NULL, 0);
765         DATA_BLOB mech_list_mic = data_blob(NULL, 0);
766         DATA_BLOB unwrapped_out = data_blob(NULL, 0);
767         struct spnego_data spnego_out;
768         struct spnego_data spnego;
769
770         ssize_t len;
771
772         *out = data_blob(NULL, 0);
773
774         if (!out_mem_ctx) {
775                 out_mem_ctx = spnego_state;
776         }
777
778         /* and switch into the state machine */
779
780         switch (spnego_state->state_position) {
781         case SPNEGO_FALLBACK:
782                 return gensec_update(spnego_state->sub_sec_security, ev,
783                                      out_mem_ctx, in, out);
784         case SPNEGO_SERVER_START:
785         {
786                 NTSTATUS nt_status;
787                 if (in.length) {
788
789                         len = spnego_read_data(gensec_security, in, &spnego);
790                         if (len == -1) {
791                                 return gensec_spnego_server_try_fallback(gensec_security, spnego_state,
792                                                                          out_mem_ctx, ev, in, out);
793                         }
794                         /* client sent NegTargetInit, we send NegTokenTarg */
795
796                         /* OK, so it's real SPNEGO, check the packet's the one we expect */
797                         if (spnego.type != spnego_state->expected_packet) {
798                                 DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
799                                           spnego_state->expected_packet));
800                                 dump_data(1, in.data, in.length);
801                                 spnego_free_data(&spnego);
802                                 return NT_STATUS_INVALID_PARAMETER;
803                         }
804
805                         nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
806                                                                      spnego_state,
807                                                                      out_mem_ctx, 
808                                                                      ev,
809                                                                      spnego.negTokenInit.mechTypes,
810                                                                      spnego.negTokenInit.mechToken, 
811                                                                      &unwrapped_out);
812
813                         nt_status = gensec_spnego_server_negTokenTarg(spnego_state,
814                                                                       out_mem_ctx,
815                                                                       nt_status,
816                                                                       unwrapped_out,
817                                                                       null_data_blob,
818                                                                       out);
819
820                         spnego_free_data(&spnego);
821
822                         return nt_status;
823                 } else {
824                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
825                                                                       out_mem_ctx, ev, in, out);
826                         spnego_state->state_position = SPNEGO_SERVER_START;
827                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_INIT;
828                         return nt_status;
829                 }
830         }
831
832         case SPNEGO_CLIENT_START:
833         {
834                 /* The server offers a list of mechanisms */
835
836                 const char *my_mechs[] = {NULL, NULL};
837                 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
838
839                 if (!in.length) {
840                         /* client to produce negTokenInit */
841                         nt_status = gensec_spnego_create_negTokenInit(gensec_security, spnego_state, 
842                                                                       out_mem_ctx, ev, in, out);
843                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
844                         spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
845                         return nt_status;
846                 }
847
848                 len = spnego_read_data(gensec_security, in, &spnego);
849
850                 if (len == -1) {
851                         DEBUG(1, ("Invalid SPNEGO request:\n"));
852                         dump_data(1, in.data, in.length);
853                         return NT_STATUS_INVALID_PARAMETER;
854                 }
855
856                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
857                 if (spnego.type != spnego_state->expected_packet) {
858                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
859                                   spnego_state->expected_packet));
860                         dump_data(1, in.data, in.length);
861                         spnego_free_data(&spnego);
862                         return NT_STATUS_INVALID_PARAMETER;
863                 }
864
865                 if (spnego.negTokenInit.targetPrincipal
866                     && strcmp(spnego.negTokenInit.targetPrincipal, ADS_IGNORE_PRINCIPAL) != 0) {
867                         DEBUG(5, ("Server claims it's principal name is %s\n", spnego.negTokenInit.targetPrincipal));
868                         if (lpcfg_client_use_spnego_principal(gensec_security->settings->lp_ctx)) {
869                                 gensec_set_target_principal(gensec_security, spnego.negTokenInit.targetPrincipal);
870                         }
871                 }
872
873                 nt_status = gensec_spnego_parse_negTokenInit(gensec_security,
874                                                              spnego_state,
875                                                              out_mem_ctx, 
876                                                              ev,
877                                                              spnego.negTokenInit.mechTypes,
878                                                              spnego.negTokenInit.mechToken, 
879                                                              &unwrapped_out);
880
881                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(nt_status)) {
882                         spnego_free_data(&spnego);
883                         return nt_status;
884                 }
885
886                 my_mechs[0] = spnego_state->neg_oid;
887                 /* compose reply */
888                 spnego_out.type = SPNEGO_NEG_TOKEN_INIT;
889                 spnego_out.negTokenInit.mechTypes = my_mechs;
890                 spnego_out.negTokenInit.reqFlags = null_data_blob;
891                 spnego_out.negTokenInit.reqFlagsPadding = 0;
892                 spnego_out.negTokenInit.mechListMIC = null_data_blob;
893                 spnego_out.negTokenInit.mechToken = unwrapped_out;
894
895                 if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
896                         DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_INIT\n"));
897                                 return NT_STATUS_INVALID_PARAMETER;
898                 }
899
900                 /* set next state */
901                 spnego_state->expected_packet = SPNEGO_NEG_TOKEN_TARG;
902                 spnego_state->state_position = SPNEGO_CLIENT_TARG;
903
904                 if (NT_STATUS_IS_OK(nt_status)) {
905                         spnego_state->no_response_expected = true;
906                 }
907
908                 spnego_free_data(&spnego);
909                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
910         }
911         case SPNEGO_SERVER_TARG:
912         {
913                 NTSTATUS nt_status;
914                 bool new_spnego = false;
915
916                 if (!in.length) {
917                         return NT_STATUS_INVALID_PARAMETER;
918                 }
919
920                 len = spnego_read_data(gensec_security, in, &spnego);
921
922                 if (len == -1) {
923                         DEBUG(1, ("Invalid SPNEGO request:\n"));
924                         dump_data(1, in.data, in.length);
925                         return NT_STATUS_INVALID_PARAMETER;
926                 }
927
928                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
929                 if (spnego.type != spnego_state->expected_packet) {
930                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
931                                   spnego_state->expected_packet));
932                         dump_data(1, in.data, in.length);
933                         spnego_free_data(&spnego);
934                         return NT_STATUS_INVALID_PARAMETER;
935                 }
936
937                 if (!spnego_state->sub_sec_security) {
938                         DEBUG(1, ("SPNEGO: Did not setup a mech in NEG_TOKEN_INIT\n"));
939                         spnego_free_data(&spnego);
940                         return NT_STATUS_INVALID_PARAMETER;
941                 }
942
943                 nt_status = gensec_update(spnego_state->sub_sec_security,
944                                           out_mem_ctx, ev,
945                                           spnego.negTokenTarg.responseToken,
946                                           &unwrapped_out);
947                 if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
948                         new_spnego = true;
949                         nt_status = gensec_check_packet(spnego_state->sub_sec_security,
950                                                         spnego_state->mech_types.data,
951                                                         spnego_state->mech_types.length,
952                                                         spnego_state->mech_types.data,
953                                                         spnego_state->mech_types.length,
954                                                         &spnego.negTokenTarg.mechListMIC);
955                         if (!NT_STATUS_IS_OK(nt_status)) {
956                                 DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
957                                         nt_errstr(nt_status)));
958                         }
959                 }
960                 if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
961                         nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
962                                                        out_mem_ctx,
963                                                        spnego_state->mech_types.data,
964                                                        spnego_state->mech_types.length,
965                                                        spnego_state->mech_types.data,
966                                                        spnego_state->mech_types.length,
967                                                        &mech_list_mic);
968                         if (!NT_STATUS_IS_OK(nt_status)) {
969                                 DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
970                                         nt_errstr(nt_status)));
971                         }
972                 }
973
974                 nt_status = gensec_spnego_server_negTokenTarg(spnego_state,
975                                                               out_mem_ctx, 
976                                                               nt_status,
977                                                               unwrapped_out,
978                                                               mech_list_mic,
979                                                               out);
980
981                 spnego_free_data(&spnego);
982
983                 return nt_status;
984         }
985         case SPNEGO_CLIENT_TARG:
986         {
987                 NTSTATUS nt_status;
988                 if (!in.length) {
989                         return NT_STATUS_INVALID_PARAMETER;
990                 }
991
992                 len = spnego_read_data(gensec_security, in, &spnego);
993
994                 if (len == -1) {
995                         DEBUG(1, ("Invalid SPNEGO request:\n"));
996                         dump_data(1, in.data, in.length);
997                         return NT_STATUS_INVALID_PARAMETER;
998                 }
999
1000                 /* OK, so it's real SPNEGO, check the packet's the one we expect */
1001                 if (spnego.type != spnego_state->expected_packet) {
1002                         DEBUG(1, ("Invalid SPNEGO request: %d, expected %d\n", spnego.type, 
1003                                   spnego_state->expected_packet));
1004                         dump_data(1, in.data, in.length);
1005                         spnego_free_data(&spnego);
1006                         return NT_STATUS_INVALID_PARAMETER;
1007                 }
1008
1009                 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1010                         spnego_free_data(&spnego);
1011                         return NT_STATUS_ACCESS_DENIED;
1012                 }
1013
1014                 /* Server didn't like our choice of mech, and chose something else */
1015                 if ((spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_INCOMPLETE) &&
1016                     spnego.negTokenTarg.supportedMech &&
1017                     strcmp(spnego.negTokenTarg.supportedMech, spnego_state->neg_oid) != 0) {
1018                         DEBUG(3,("GENSEC SPNEGO: client preferred mech (%s) not accepted, server wants: %s\n",
1019                                  gensec_get_name_by_oid(gensec_security, spnego.negTokenTarg.supportedMech), 
1020                                  gensec_get_name_by_oid(gensec_security, spnego_state->neg_oid)));
1021
1022                         talloc_free(spnego_state->sub_sec_security);
1023                         nt_status = gensec_subcontext_start(spnego_state,
1024                                                             gensec_security,
1025                                                             &spnego_state->sub_sec_security);
1026                         if (!NT_STATUS_IS_OK(nt_status)) {
1027                                 spnego_free_data(&spnego);
1028                                 return nt_status;
1029                         }
1030                         /* select the sub context */
1031                         nt_status = gensec_start_mech_by_oid(spnego_state->sub_sec_security,
1032                                                              spnego.negTokenTarg.supportedMech);
1033                         if (!NT_STATUS_IS_OK(nt_status)) {
1034                                 spnego_free_data(&spnego);
1035                                 return nt_status;
1036                         }
1037
1038                         nt_status = gensec_update(spnego_state->sub_sec_security,
1039                                                   out_mem_ctx, ev,
1040                                                   spnego.negTokenTarg.responseToken,
1041                                                   &unwrapped_out);
1042                         spnego_state->neg_oid = talloc_strdup(spnego_state, spnego.negTokenTarg.supportedMech);
1043                 } else if (spnego_state->no_response_expected) {
1044                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1045                                 DEBUG(3,("GENSEC SPNEGO: client GENSEC accepted, but server rejected (bad password?)\n"));
1046                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1047                         } else if (spnego.negTokenTarg.responseToken.length) {
1048                                 DEBUG(2,("GENSEC SPNEGO: client GENSEC accepted, but server continued negotiation!\n"));
1049                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1050                         } else {
1051                                 nt_status = NT_STATUS_OK;
1052                         }
1053                         if (NT_STATUS_IS_OK(nt_status) && spnego.negTokenTarg.mechListMIC.length > 0) {
1054                                 nt_status = gensec_check_packet(spnego_state->sub_sec_security,
1055                                                                 spnego_state->mech_types.data,
1056                                                                 spnego_state->mech_types.length,
1057                                                                 spnego_state->mech_types.data,
1058                                                                 spnego_state->mech_types.length,
1059                                                                 &spnego.negTokenTarg.mechListMIC);
1060                                 if (!NT_STATUS_IS_OK(nt_status)) {
1061                                         DEBUG(2,("GENSEC SPNEGO: failed to verify mechListMIC: %s\n",
1062                                                 nt_errstr(nt_status)));
1063                                 }
1064                         }
1065                 } else {
1066                         bool new_spnego = false;
1067
1068                         nt_status = gensec_update(spnego_state->sub_sec_security,
1069                                                   out_mem_ctx, ev,
1070                                                   spnego.negTokenTarg.responseToken, 
1071                                                   &unwrapped_out);
1072
1073                         if (NT_STATUS_IS_OK(nt_status)
1074                             && spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1075                                 new_spnego = gensec_have_feature(spnego_state->sub_sec_security,
1076                                                                  GENSEC_FEATURE_NEW_SPNEGO);
1077                         }
1078                         if (NT_STATUS_IS_OK(nt_status) && new_spnego) {
1079                                 nt_status = gensec_sign_packet(spnego_state->sub_sec_security,
1080                                                                out_mem_ctx,
1081                                                                spnego_state->mech_types.data,
1082                                                                spnego_state->mech_types.length,
1083                                                                spnego_state->mech_types.data,
1084                                                                spnego_state->mech_types.length,
1085                                                                &mech_list_mic);
1086                                 if (!NT_STATUS_IS_OK(nt_status)) {
1087                                         DEBUG(2,("GENSEC SPNEGO: failed to sign mechListMIC: %s\n",
1088                                                 nt_errstr(nt_status)));
1089                                 }
1090                         }
1091                         if (NT_STATUS_IS_OK(nt_status)) {
1092                                 spnego_state->no_response_expected = true;
1093                         }
1094                 } 
1095
1096                 spnego_free_data(&spnego);
1097
1098                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
1099                         && !NT_STATUS_IS_OK(nt_status)) {
1100                         DEBUG(1, ("SPNEGO(%s) login failed: %s\n", 
1101                                   spnego_state->sub_sec_security->ops->name, 
1102                                   nt_errstr(nt_status)));
1103                         return nt_status;
1104                 }
1105
1106                 if (unwrapped_out.length || mech_list_mic.length) {
1107                         /* compose reply */
1108                         spnego_out.type = SPNEGO_NEG_TOKEN_TARG;
1109                         spnego_out.negTokenTarg.negResult = SPNEGO_NONE_RESULT;
1110                         spnego_out.negTokenTarg.supportedMech = NULL;
1111                         spnego_out.negTokenTarg.responseToken = unwrapped_out;
1112                         spnego_out.negTokenTarg.mechListMIC = mech_list_mic;
1113
1114                         if (spnego_write_data(out_mem_ctx, out, &spnego_out) == -1) {
1115                                 DEBUG(1, ("Failed to write SPNEGO reply to NEG_TOKEN_TARG\n"));
1116                                 return NT_STATUS_INVALID_PARAMETER;
1117                         }
1118
1119                         spnego_state->state_position = SPNEGO_CLIENT_TARG;
1120                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1121                 } else {
1122
1123                         /* all done - server has accepted, and we agree */
1124                         *out = null_data_blob;
1125
1126                         if (spnego.negTokenTarg.negResult != SPNEGO_ACCEPT_COMPLETED) {
1127                                 /* unless of course it did not accept */
1128                                 DEBUG(1,("gensec_update ok but not accepted\n"));
1129                                 nt_status = NT_STATUS_INVALID_PARAMETER;
1130                         }
1131
1132                         spnego_state->state_position = SPNEGO_DONE;
1133                 }
1134
1135                 return nt_status;
1136         }
1137         case SPNEGO_DONE:
1138                 /* We should not be called after we are 'done' */
1139                 return NT_STATUS_INVALID_PARAMETER;
1140         }
1141         return NT_STATUS_INVALID_PARAMETER;
1142 }
1143
1144 static NTSTATUS gensec_spnego_update_in(struct gensec_security *gensec_security,
1145                                         const DATA_BLOB in, DATA_BLOB *full_in)
1146 {
1147         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1148         size_t expected;
1149         NTSTATUS status;
1150         bool ok;
1151
1152         *full_in = data_blob_null;
1153
1154         if (spnego_state->in_needed == 0) {
1155                 size_t size = 0;
1156
1157                 /*
1158                  * try to work out the size of the full
1159                  * input token, it might be fragmented
1160                  */
1161                 status = asn1_peek_full_tag(in,  ASN1_APPLICATION(0), &size);
1162                 if (!NT_STATUS_IS_OK(status) &&
1163                     !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1164                         status = asn1_peek_full_tag(in, ASN1_CONTEXT(1), &size);
1165                 }
1166
1167                 if (NT_STATUS_IS_OK(status) ||
1168                     NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1169                         spnego_state->in_needed = size;
1170                 } else {
1171                         /*
1172                          * If it is not an asn1 message
1173                          * just call the next layer.
1174                          */
1175                         spnego_state->in_needed = in.length;
1176                 }
1177         }
1178
1179         if (spnego_state->in_needed > UINT16_MAX) {
1180                 /*
1181                  * limit the incoming message to 0xFFFF
1182                  * to avoid DoS attacks.
1183                  */
1184                 return NT_STATUS_INVALID_BUFFER_SIZE;
1185         }
1186
1187         if ((spnego_state->in_needed > 0) && (in.length == 0)) {
1188                 /*
1189                  * If we reach this, we know we got at least
1190                  * part of an asn1 message, getting 0 means
1191                  * the remote peer wants us to spin.
1192                  */
1193                 return NT_STATUS_INVALID_PARAMETER;
1194         }
1195
1196         expected = spnego_state->in_needed - spnego_state->in_frag.length;
1197         if (in.length > expected) {
1198                 /*
1199                  * we got more than expected
1200                  */
1201                 return NT_STATUS_INVALID_PARAMETER;
1202         }
1203
1204         if (in.length == spnego_state->in_needed) {
1205                 /*
1206                  * if the in.length contains the full blob
1207                  * we are done.
1208                  *
1209                  * Note: this implies spnego_state->in_frag.length == 0,
1210                  *       but we do not need to check this explicitly
1211                  *       because we already know that we did not get
1212                  *       more than expected.
1213                  */
1214                 *full_in = in;
1215                 return NT_STATUS_OK;
1216         }
1217
1218         ok = data_blob_append(spnego_state, &spnego_state->in_frag,
1219                               in.data, in.length);
1220         if (!ok) {
1221                 return NT_STATUS_NO_MEMORY;
1222         }
1223
1224         if (spnego_state->in_needed > spnego_state->in_frag.length) {
1225                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1226         }
1227
1228         *full_in = spnego_state->in_frag;
1229         return NT_STATUS_OK;
1230 }
1231
1232 static NTSTATUS gensec_spnego_update_out(struct gensec_security *gensec_security,
1233                                          TALLOC_CTX *out_mem_ctx,
1234                                          DATA_BLOB *_out)
1235 {
1236         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1237         DATA_BLOB out = data_blob_null;
1238
1239         *_out = data_blob_null;
1240
1241         if (spnego_state->out_frag.length == 0) {
1242                 return spnego_state->out_status;
1243         }
1244
1245         /*
1246          * There is still more data to be delivered
1247          * to the remote peer.
1248          */
1249
1250         if (spnego_state->out_frag.length <= spnego_state->out_max_length) {
1251                 /*
1252                  * Fast path, we can deliver everything
1253                  */
1254
1255                 *_out = spnego_state->out_frag;
1256                 talloc_steal(out_mem_ctx, _out->data);
1257                 spnego_state->out_frag = data_blob_null;
1258                 return spnego_state->out_status;
1259         }
1260
1261         out = spnego_state->out_frag;
1262
1263         /*
1264          * copy the remaining bytes
1265          */
1266         spnego_state->out_frag = data_blob_talloc(spnego_state,
1267                                         out.data + spnego_state->out_max_length,
1268                                         out.length - spnego_state->out_max_length);
1269         if (spnego_state->out_frag.data == NULL) {
1270                 return NT_STATUS_NO_MEMORY;
1271         }
1272
1273         /*
1274          * truncate the buffer
1275          */
1276         data_blob_realloc(spnego_state, &out, spnego_state->out_max_length);
1277
1278         talloc_steal(out_mem_ctx, out.data);
1279         *_out = out;
1280         return NT_STATUS_MORE_PROCESSING_REQUIRED;
1281 }
1282
1283 static NTSTATUS gensec_spnego_update_wrapper(struct gensec_security *gensec_security,
1284                                              TALLOC_CTX *out_mem_ctx,
1285                                              struct tevent_context *ev,
1286                                              const DATA_BLOB in, DATA_BLOB *out)
1287 {
1288         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1289         DATA_BLOB full_in = data_blob_null;
1290         NTSTATUS status;
1291
1292         *out = data_blob_null;
1293
1294         if (spnego_state->out_frag.length > 0) {
1295                 if (in.length > 0) {
1296                         return NT_STATUS_INVALID_PARAMETER;
1297                 }
1298
1299                 return gensec_spnego_update_out(gensec_security,
1300                                                 out_mem_ctx,
1301                                                 out);
1302         }
1303
1304         status = gensec_spnego_update_in(gensec_security,
1305                                          in, &full_in);
1306         if (!NT_STATUS_IS_OK(status)) {
1307                 return status;
1308         }
1309
1310         status = gensec_spnego_update(gensec_security,
1311                                       spnego_state, ev,
1312                                       full_in,
1313                                       &spnego_state->out_frag);
1314         data_blob_free(&spnego_state->in_frag);
1315         spnego_state->in_needed = 0;
1316         if (!NT_STATUS_IS_OK(status) &&
1317             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1318                 return status;
1319         }
1320
1321         spnego_state->out_status = status;
1322
1323         return gensec_spnego_update_out(gensec_security,
1324                                         out_mem_ctx,
1325                                         out);
1326 }
1327
1328 static void gensec_spnego_want_feature(struct gensec_security *gensec_security,
1329                                        uint32_t feature)
1330 {
1331         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1332
1333         if (!spnego_state || !spnego_state->sub_sec_security) {
1334                 gensec_security->want_features |= feature;
1335                 return;
1336         }
1337
1338         gensec_want_feature(spnego_state->sub_sec_security,
1339                             feature);
1340 }
1341
1342 static bool gensec_spnego_have_feature(struct gensec_security *gensec_security,
1343                                        uint32_t feature) 
1344 {
1345         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1346         if (!spnego_state->sub_sec_security) {
1347                 return false;
1348         }
1349
1350         return gensec_have_feature(spnego_state->sub_sec_security, 
1351                                    feature);
1352 }
1353
1354 static NTTIME gensec_spnego_expire_time(struct gensec_security *gensec_security)
1355 {
1356         struct spnego_state *spnego_state = (struct spnego_state *)gensec_security->private_data;
1357
1358         if (!spnego_state->sub_sec_security) {
1359                 return GENSEC_EXPIRE_TIME_INFINITY;
1360         }
1361
1362         return gensec_expire_time(spnego_state->sub_sec_security);
1363 }
1364
1365 static const char *gensec_spnego_oids[] = { 
1366         GENSEC_OID_SPNEGO,
1367         NULL 
1368 };
1369
1370 static const struct gensec_security_ops gensec_spnego_security_ops = {
1371         .name             = "spnego",
1372         .sasl_name        = "GSS-SPNEGO",
1373         .auth_type        = DCERPC_AUTH_TYPE_SPNEGO,
1374         .oid              = gensec_spnego_oids,
1375         .client_start     = gensec_spnego_client_start,
1376         .server_start     = gensec_spnego_server_start,
1377         .update           = gensec_spnego_update_wrapper,
1378         .seal_packet      = gensec_spnego_seal_packet,
1379         .sign_packet      = gensec_spnego_sign_packet,
1380         .sig_size         = gensec_spnego_sig_size,
1381         .max_wrapped_size = gensec_spnego_max_wrapped_size,
1382         .max_input_size   = gensec_spnego_max_input_size,
1383         .check_packet     = gensec_spnego_check_packet,
1384         .unseal_packet    = gensec_spnego_unseal_packet,
1385         .packet_full_request = gensec_spnego_packet_full_request,
1386         .wrap             = gensec_spnego_wrap,
1387         .unwrap           = gensec_spnego_unwrap,
1388         .wrap_packets     = gensec_spnego_wrap_packets,
1389         .unwrap_packets   = gensec_spnego_unwrap_packets,
1390         .session_key      = gensec_spnego_session_key,
1391         .session_info     = gensec_spnego_session_info,
1392         .want_feature     = gensec_spnego_want_feature,
1393         .have_feature     = gensec_spnego_have_feature,
1394         .expire_time      = gensec_spnego_expire_time,
1395         .enabled          = true,
1396         .priority         = GENSEC_SPNEGO
1397 };
1398
1399 _PUBLIC_ NTSTATUS gensec_spnego_init(void)
1400 {
1401         NTSTATUS ret;
1402         ret = gensec_register(&gensec_spnego_security_ops);
1403         if (!NT_STATUS_IS_OK(ret)) {
1404                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
1405                         gensec_spnego_security_ops.name));
1406                 return ret;
1407         }
1408
1409         return ret;
1410 }