r6458: Split up NTLMSSP into a new directory, and into seperate files for the
[metze/samba/wip.git] / source4 / auth / ntlmssp / ntlmssp_sign.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Version 3.0
4  *  NTLMSSP Signing routines
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-2001
6  *  Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2005
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software Foundation,
20  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #include "includes.h"
24 #include "auth/auth.h"
25 #include "auth/ntlmssp/ntlmssp.h"
26 #include "lib/crypto/crypto.h"
27
28 #define CLI_SIGN "session key to client-to-server signing key magic constant"
29 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
30 #define SRV_SIGN "session key to server-to-client signing key magic constant"
31 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
32
33 /**
34  * Some notes on then NTLM2 code:
35  *
36  * NTLM2 is a AEAD system.  This means that the data encrypted is not
37  * all the data that is signed.  In DCE-RPC case, the headers of the
38  * DCE-RPC packets are also signed.  This prevents some of the
39  * fun-and-games one might have by changing them.
40  *
41  */
42
43 static void calc_ntlmv2_key(TALLOC_CTX *mem_ctx, 
44                             DATA_BLOB *subkey,
45                             DATA_BLOB session_key, 
46                             const char *constant)
47 {
48         struct MD5Context ctx3;
49         *subkey = data_blob_talloc(mem_ctx, NULL, 16);
50         MD5Init(&ctx3);
51         MD5Update(&ctx3, session_key.data, session_key.length);
52         MD5Update(&ctx3, constant, strlen(constant)+1);
53         MD5Final(subkey->data, &ctx3);
54 }
55
56 enum ntlmssp_direction {
57         NTLMSSP_SEND,
58         NTLMSSP_RECEIVE
59 };
60
61 static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
62                                               TALLOC_CTX *sig_mem_ctx, 
63                                               const uint8_t *data, size_t length, 
64                                               const uint8_t *whole_pdu, size_t pdu_length, 
65                                               enum ntlmssp_direction direction,
66                                               DATA_BLOB *sig, BOOL encrypt_sig)
67 {
68         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
69
70                 HMACMD5Context ctx;
71                 uint8_t digest[16];
72                 uint8_t seq_num[4];
73
74                 *sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
75                 if (!sig->data) {
76                         return NT_STATUS_NO_MEMORY;
77                 }
78                         
79                 switch (direction) {
80                 case NTLMSSP_SEND:
81                         SIVAL(seq_num, 0, ntlmssp_state->ntlm2_send_seq_num);
82                         ntlmssp_state->ntlm2_send_seq_num++;
83                         hmac_md5_init_limK_to_64(ntlmssp_state->send_sign_key.data, 
84                                                  ntlmssp_state->send_sign_key.length, &ctx);
85                         break;
86                 case NTLMSSP_RECEIVE:
87                         SIVAL(seq_num, 0, ntlmssp_state->ntlm2_recv_seq_num);
88                         ntlmssp_state->ntlm2_recv_seq_num++;
89                         hmac_md5_init_limK_to_64(ntlmssp_state->recv_sign_key.data, 
90                                                  ntlmssp_state->recv_sign_key.length, &ctx);
91                         break;
92                 }
93                 hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
94                 hmac_md5_update(whole_pdu, pdu_length, &ctx);
95                 hmac_md5_final(digest, &ctx);
96
97                 if (encrypt_sig && ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
98                         switch (direction) {
99                         case NTLMSSP_SEND:
100                                 arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, digest, 8);
101                                 break;
102                         case NTLMSSP_RECEIVE:
103                                 arcfour_crypt_sbox(ntlmssp_state->recv_seal_hash, digest, 8);
104                                 break;
105                         }
106                 }
107
108                 SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
109                 memcpy(sig->data + 4, digest, 8);
110                 memcpy(sig->data + 12, seq_num, 4);
111
112         } else {
113                 uint32_t crc;
114                 crc = crc32_calc_buffer(data, length);
115                 if (!msrpc_gen(sig_mem_ctx, sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlm_seq_num)) {
116                         return NT_STATUS_NO_MEMORY;
117                 }
118                 ntlmssp_state->ntlm_seq_num++;
119
120                 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, sig->data+4, sig->length-4);
121         }
122         dump_data_pw("calculated ntlmssp signature\n", sig->data, sig->length);
123         return NT_STATUS_OK;
124 }
125
126 NTSTATUS gensec_ntlmssp_sign_packet(struct gensec_security *gensec_security, 
127                                            TALLOC_CTX *sig_mem_ctx, 
128                                            const uint8_t *data, size_t length, 
129                                            const uint8_t *whole_pdu, size_t pdu_length, 
130                                            DATA_BLOB *sig)
131 {
132         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
133         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
134
135         if (!ntlmssp_state->session_key.length) {
136                 DEBUG(3, ("NO session key, cannot check sign packet\n"));
137                 return NT_STATUS_NO_USER_SESSION_KEY;
138         }
139         
140         if (!ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) {
141                 DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
142                 return NT_STATUS_INVALID_PARAMETER;
143         }
144         
145         return ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx, 
146                                              data, length, 
147                                              whole_pdu, pdu_length, 
148                                              NTLMSSP_SEND, sig, True);
149 }
150
151 /**
152  * Check the signature of an incoming packet 
153  *
154  */
155
156 NTSTATUS gensec_ntlmssp_check_packet(struct gensec_security *gensec_security, 
157                                      TALLOC_CTX *sig_mem_ctx, 
158                                      const uint8_t *data, size_t length, 
159                                      const uint8_t *whole_pdu, size_t pdu_length, 
160                                      const DATA_BLOB *sig)
161 {
162         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
163         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
164
165         DATA_BLOB local_sig;
166         NTSTATUS nt_status;
167
168         if (!ntlmssp_state->session_key.length) {
169                 DEBUG(3, ("NO session key, cannot check packet signature\n"));
170                 return NT_STATUS_NO_USER_SESSION_KEY;
171         }
172
173         if (sig->length < 8) {
174                 DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n", 
175                           (unsigned long)sig->length));
176         }
177
178         nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx, 
179                                                   data, length, 
180                                                   whole_pdu, pdu_length, 
181                                                   NTLMSSP_RECEIVE, &local_sig, True);
182         
183         if (!NT_STATUS_IS_OK(nt_status)) {
184                 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
185                 return nt_status;
186         }
187
188         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
189                 if (local_sig.length != sig->length ||
190                     memcmp(local_sig.data, 
191                            sig->data, sig->length) != 0) {
192                         DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
193                         dump_data(5, local_sig.data, local_sig.length);
194                         
195                         DEBUG(5, ("BAD SIG: got signature of\n"));
196                         dump_data(5, sig->data, sig->length);
197                         
198                         DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
199                         return NT_STATUS_ACCESS_DENIED;
200                 }
201         } else {
202                 if (local_sig.length != sig->length ||
203                     memcmp(local_sig.data + 8, 
204                            sig->data + 8, sig->length - 8) != 0) {
205                         DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
206                         dump_data(5, local_sig.data, local_sig.length);
207                         
208                         DEBUG(5, ("BAD SIG: got signature of\n"));
209                         dump_data(5, sig->data, sig->length);
210                         
211                         DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
212                         return NT_STATUS_ACCESS_DENIED;
213                 }
214         }
215         dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
216
217         return NT_STATUS_OK;
218 }
219
220
221 /**
222  * Seal data with the NTLMSSP algorithm
223  *
224  */
225
226 NTSTATUS gensec_ntlmssp_seal_packet(struct gensec_security *gensec_security, 
227                                     TALLOC_CTX *sig_mem_ctx, 
228                                     uint8_t *data, size_t length, 
229                                     const uint8_t *whole_pdu, size_t pdu_length, 
230                                     DATA_BLOB *sig)
231 {
232         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
233         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
234         NTSTATUS nt_status;
235         if (!ntlmssp_state->session_key.length) {
236                 DEBUG(3, ("NO session key, cannot seal packet\n"));
237                 return NT_STATUS_NO_USER_SESSION_KEY;
238         }
239
240         if (!ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
241                 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
242                 return NT_STATUS_INVALID_PARAMETER;
243         }
244
245         DEBUG(10,("ntlmssp_seal_data: seal\n"));
246         dump_data_pw("ntlmssp clear data\n", data, length);
247         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
248                 /* The order of these two operations matters - we must first seal the packet,
249                    then seal the sequence number - this is becouse the send_seal_hash is not
250                    constant, but is is rather updated with each iteration */
251                 nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx, 
252                                                           data, length, 
253                                                           whole_pdu, pdu_length, 
254                                                           NTLMSSP_SEND, sig, False);
255                 arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, data, length);
256                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
257                         arcfour_crypt_sbox(ntlmssp_state->send_seal_hash, sig->data+4, 8);
258                 }
259         } else {
260                 uint32_t crc;
261                 crc = crc32_calc_buffer(data, length);
262                 if (!msrpc_gen(sig_mem_ctx, sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, ntlmssp_state->ntlm_seq_num)) {
263                         return NT_STATUS_NO_MEMORY;
264                 }
265
266                 /* The order of these two operations matters - we must
267                    first seal the packet, then seal the sequence
268                    number - this is becouse the ntlmssp_hash is not
269                    constant, but is is rather updated with each
270                    iteration */
271
272                 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, data, length);
273                 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, sig->data+4, sig->length-4);
274                 /* increment counter on send */
275                 ntlmssp_state->ntlm_seq_num++;
276                 nt_status = NT_STATUS_OK;
277         }
278         dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
279         dump_data_pw("ntlmssp sealed data\n", data, length);
280
281
282         return nt_status;
283 }
284
285 /**
286  * Unseal data with the NTLMSSP algorithm
287  *
288  */
289
290 /*
291   wrappers for the ntlmssp_*() functions
292 */
293 NTSTATUS gensec_ntlmssp_unseal_packet(struct gensec_security *gensec_security, 
294                                       TALLOC_CTX *sig_mem_ctx, 
295                                       uint8_t *data, size_t length, 
296                                       const uint8_t *whole_pdu, size_t pdu_length, 
297                                       const DATA_BLOB *sig)
298 {
299         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
300         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp_state->ntlmssp_state;
301         DATA_BLOB local_sig;
302         NTSTATUS nt_status;
303         if (!ntlmssp_state->session_key.length) {
304                 DEBUG(3, ("NO session key, cannot unseal packet\n"));
305                 return NT_STATUS_NO_USER_SESSION_KEY;
306         }
307
308         dump_data_pw("ntlmssp sealed data\n", data, length);
309         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
310                 arcfour_crypt_sbox(ntlmssp_state->recv_seal_hash, data, length);
311
312                 nt_status = ntlmssp_make_packet_signature(ntlmssp_state, sig_mem_ctx, 
313                                                           data, length, 
314                                                           whole_pdu, pdu_length, 
315                                                           NTLMSSP_RECEIVE, &local_sig, True);
316                 if (!NT_STATUS_IS_OK(nt_status)) {
317                         return nt_status;
318                 }
319
320                 if (local_sig.length != sig->length ||
321                     memcmp(local_sig.data, 
322                            sig->data, sig->length) != 0) {
323                         DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
324                         dump_data(5, local_sig.data, local_sig.length);
325                         
326                         DEBUG(5, ("BAD SIG: got signature of\n"));
327                         dump_data(5, sig->data, sig->length);
328                         
329                         DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
330                         return NT_STATUS_ACCESS_DENIED;
331                 }
332
333                 dump_data_pw("ntlmssp clear data\n", data, length);
334                 return NT_STATUS_OK;
335         } else {
336                 arcfour_crypt_sbox(ntlmssp_state->ntlmssp_hash, data, length);
337                 dump_data_pw("ntlmssp clear data\n", data, length);
338                 return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, data, length, whole_pdu, pdu_length, sig);
339         }
340 }
341
342 /**
343    Initialise the state for NTLMSSP signing.
344 */
345 NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state)
346 {
347         uint8_t p24[24];
348         ZERO_STRUCT(p24);
349
350         DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
351         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
352
353         if (!ntlmssp_state->session_key.length) {
354                 DEBUG(3, ("NO session key, cannot intialise signing\n"));
355                 return NT_STATUS_NO_USER_SESSION_KEY;
356         }
357
358         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
359         {
360                 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
361                 const char *send_sign_const;
362                 const char *send_seal_const;
363                 const char *recv_sign_const;
364                 const char *recv_seal_const;
365
366                 switch (ntlmssp_state->role) {
367                 case NTLMSSP_CLIENT:
368                         send_sign_const = CLI_SIGN;
369                         send_seal_const = CLI_SEAL;
370                         recv_sign_const = SRV_SIGN;
371                         recv_seal_const = SRV_SEAL;
372                         break;
373                 case NTLMSSP_SERVER:
374                         send_sign_const = SRV_SIGN;
375                         send_seal_const = SRV_SEAL;
376                         recv_sign_const = CLI_SIGN;
377                         recv_seal_const = CLI_SEAL;
378                         break;
379                 default:
380                         return NT_STATUS_INTERNAL_ERROR;
381                 }
382                 
383                 /**
384                    Weaken NTLMSSP keys to cope with down-level clients, servers and export restrictions.
385                    
386                    We probably should have some parameters to control this, once we get NTLM2 working.
387                 */
388
389
390                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
391                         
392                 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
393                         weak_session_key.length = 6;
394                 } else { /* forty bits */
395                         weak_session_key.length = 5;
396                 }
397                 dump_data_pw("NTLMSSP weakend master key:\n",
398                              weak_session_key.data, 
399                              weak_session_key.length);
400
401                 /* SEND */
402                 calc_ntlmv2_key(ntlmssp_state, 
403                                 &ntlmssp_state->send_sign_key, 
404                                 ntlmssp_state->session_key, send_sign_const);
405                 dump_data_pw("NTLMSSP send sign key:\n",
406                              ntlmssp_state->send_sign_key.data, 
407                              ntlmssp_state->send_sign_key.length);
408                 
409                 calc_ntlmv2_key(ntlmssp_state, 
410                                 &ntlmssp_state->send_seal_key, 
411                                 weak_session_key, send_seal_const);
412                 dump_data_pw("NTLMSSP send seal key:\n",
413                              ntlmssp_state->send_seal_key.data, 
414                              ntlmssp_state->send_seal_key.length);
415
416                 arcfour_init(ntlmssp_state->send_seal_hash, 
417                              &ntlmssp_state->send_seal_key);
418
419                 dump_data_pw("NTLMSSP send sesl hash:\n", 
420                              ntlmssp_state->send_seal_hash, 
421                              sizeof(ntlmssp_state->send_seal_hash));
422
423                 /* RECV */
424                 calc_ntlmv2_key(ntlmssp_state, 
425                                 &ntlmssp_state->recv_sign_key, 
426                                 ntlmssp_state->session_key, recv_sign_const);
427                 dump_data_pw("NTLMSSP recv sign key:\n",
428                              ntlmssp_state->recv_sign_key.data, 
429                              ntlmssp_state->recv_sign_key.length);
430
431                 calc_ntlmv2_key(ntlmssp_state, 
432                                 &ntlmssp_state->recv_seal_key, 
433                                 weak_session_key, recv_seal_const);
434                 dump_data_pw("NTLMSSP recv seal key:\n",
435                              ntlmssp_state->recv_seal_key.data, 
436                              ntlmssp_state->recv_seal_key.length);
437                 arcfour_init(ntlmssp_state->recv_seal_hash, 
438                              &ntlmssp_state->recv_seal_key);
439
440                 dump_data_pw("NTLMSSP receive seal hash:\n", 
441                              ntlmssp_state->recv_seal_hash, 
442                              sizeof(ntlmssp_state->recv_seal_hash));
443         } else {
444                 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
445
446                 arcfour_init(ntlmssp_state->ntlmssp_hash, 
447                              &ntlmssp_state->session_key);
448                 dump_data_pw("NTLMSSP hash:\n", ntlmssp_state->ntlmssp_hash,
449                              sizeof(ntlmssp_state->ntlmssp_hash));
450         }
451
452         ntlmssp_state->ntlm_seq_num = 0;
453         ntlmssp_state->ntlm2_send_seq_num = 0;
454         ntlmssp_state->ntlm2_recv_seq_num = 0;
455
456         return NT_STATUS_OK;
457 }
458
459 size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security) 
460 {
461         return NTLMSSP_SIG_SIZE;
462 }
463
464 NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security, 
465                              TALLOC_CTX *sig_mem_ctx, 
466                              const DATA_BLOB *in, 
467                              DATA_BLOB *out)
468 {
469         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
470         DATA_BLOB sig;
471         NTSTATUS nt_status;
472
473         if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
474
475                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
476                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
477
478                 nt_status = gensec_ntlmssp_seal_packet(gensec_security, sig_mem_ctx, 
479                                                        out->data + NTLMSSP_SIG_SIZE, 
480                                                        out->length - NTLMSSP_SIG_SIZE, 
481                                                        out->data + NTLMSSP_SIG_SIZE, 
482                                                        out->length - NTLMSSP_SIG_SIZE, 
483                                                        &sig);
484
485                 if (NT_STATUS_IS_OK(nt_status)) {
486                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
487                 }
488                 return nt_status;
489
490         } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) 
491                    || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
492
493                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
494                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
495
496                 nt_status = gensec_ntlmssp_sign_packet(gensec_security, sig_mem_ctx, 
497                                                 out->data + NTLMSSP_SIG_SIZE, 
498                                                 out->length - NTLMSSP_SIG_SIZE, 
499                                                 out->data + NTLMSSP_SIG_SIZE, 
500                                                 out->length - NTLMSSP_SIG_SIZE, 
501                                                 &sig);
502
503                 if (NT_STATUS_IS_OK(nt_status)) {
504                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
505                 }
506                 return nt_status;
507
508         } else {
509                 *out = *in;
510                 return NT_STATUS_OK;
511         }
512 }
513
514
515 NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security, 
516                                TALLOC_CTX *sig_mem_ctx, 
517                                const DATA_BLOB *in, 
518                                DATA_BLOB *out)
519 {
520         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
521         DATA_BLOB sig;
522
523         if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
524                 if (in->length < NTLMSSP_SIG_SIZE) {
525                         return NT_STATUS_INVALID_PARAMETER;
526                 }
527                 sig.data = in->data;
528                 sig.length = NTLMSSP_SIG_SIZE;
529
530                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
531                 
532                 return gensec_ntlmssp_unseal_packet(gensec_security, sig_mem_ctx, 
533                                                     out->data, out->length, 
534                                                     out->data, out->length, 
535                                                     &sig);
536                                                   
537         } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) 
538                    || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
539                 if (in->length < NTLMSSP_SIG_SIZE) {
540                         return NT_STATUS_INVALID_PARAMETER;
541                 }
542                 sig.data = in->data;
543                 sig.length = NTLMSSP_SIG_SIZE;
544
545                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
546                 
547                 return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, 
548                                             out->data, out->length, 
549                                             out->data, out->length, 
550                                             &sig);
551         } else {
552                 *out = *in;
553                 return NT_STATUS_OK;
554         }
555 }
556