r6462: Move the arcfour sbox state into it's own structure, and allocate it
[idra/samba.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                 DATA_BLOB send_seal_key;
367                 DATA_BLOB recv_seal_key;
368
369                 switch (ntlmssp_state->role) {
370                 case NTLMSSP_CLIENT:
371                         send_sign_const = CLI_SIGN;
372                         send_seal_const = CLI_SEAL;
373                         recv_sign_const = SRV_SIGN;
374                         recv_seal_const = SRV_SEAL;
375                         break;
376                 case NTLMSSP_SERVER:
377                         send_sign_const = SRV_SIGN;
378                         send_seal_const = SRV_SEAL;
379                         recv_sign_const = CLI_SIGN;
380                         recv_seal_const = CLI_SEAL;
381                         break;
382                 default:
383                         return NT_STATUS_INTERNAL_ERROR;
384                 }
385                 
386                 ntlmssp_state->send_seal_hash = talloc(ntlmssp_state, struct arcfour_state);
387                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->send_seal_hash);
388                 ntlmssp_state->recv_seal_hash = talloc(ntlmssp_state, struct arcfour_state);
389                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->recv_seal_hash);
390
391                 /**
392                    Weaken NTLMSSP keys to cope with down-level clients, servers and export restrictions.
393                    
394                    We probably should have some parameters to control this, once we get NTLM2 working.
395                 */
396
397
398                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
399                         
400                 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
401                         weak_session_key.length = 6;
402                 } else { /* forty bits */
403                         weak_session_key.length = 5;
404                 }
405                 dump_data_pw("NTLMSSP weakend master key:\n",
406                              weak_session_key.data, 
407                              weak_session_key.length);
408
409                 /* SEND */
410                 calc_ntlmv2_key(ntlmssp_state, 
411                                 &ntlmssp_state->send_sign_key, 
412                                 ntlmssp_state->session_key, send_sign_const);
413                 dump_data_pw("NTLMSSP send sign key:\n",
414                              ntlmssp_state->send_sign_key.data, 
415                              ntlmssp_state->send_sign_key.length);
416                 
417                 calc_ntlmv2_key(ntlmssp_state, 
418                                 &send_seal_key, 
419                                 weak_session_key, send_seal_const);
420                 dump_data_pw("NTLMSSP send seal key:\n",
421                              send_seal_key.data, 
422                              send_seal_key.length);
423
424                 arcfour_init(ntlmssp_state->send_seal_hash, 
425                              &send_seal_key);
426
427                 dump_data_pw("NTLMSSP send sesl hash:\n", 
428                              ntlmssp_state->send_seal_hash->sbox, 
429                              sizeof(ntlmssp_state->send_seal_hash->sbox));
430
431                 /* RECV */
432                 calc_ntlmv2_key(ntlmssp_state, 
433                                 &ntlmssp_state->recv_sign_key, 
434                                 ntlmssp_state->session_key, recv_sign_const);
435                 dump_data_pw("NTLMSSP recv sign key:\n",
436                              ntlmssp_state->recv_sign_key.data, 
437                              ntlmssp_state->recv_sign_key.length);
438
439                 calc_ntlmv2_key(ntlmssp_state, 
440                                 &recv_seal_key, 
441                                 weak_session_key, recv_seal_const);
442                 dump_data_pw("NTLMSSP recv seal key:\n",
443                              recv_seal_key.data, 
444                              recv_seal_key.length);
445                 arcfour_init(ntlmssp_state->recv_seal_hash, 
446                              &recv_seal_key);
447
448                 dump_data_pw("NTLMSSP receive seal hash:\n", 
449                              ntlmssp_state->recv_seal_hash->sbox, 
450                              sizeof(ntlmssp_state->recv_seal_hash->sbox));
451         } else {
452                 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
453
454                 ntlmssp_state->ntlmssp_hash = talloc(ntlmssp_state, struct arcfour_state);
455                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->ntlmssp_hash);
456
457                 arcfour_init(ntlmssp_state->ntlmssp_hash, 
458                              &ntlmssp_state->session_key);
459                 dump_data_pw("NTLMSSP hash:\n", ntlmssp_state->ntlmssp_hash->sbox,
460                              sizeof(ntlmssp_state->ntlmssp_hash->sbox));
461         }
462
463         ntlmssp_state->ntlm_seq_num = 0;
464         ntlmssp_state->ntlm2_send_seq_num = 0;
465         ntlmssp_state->ntlm2_recv_seq_num = 0;
466
467         return NT_STATUS_OK;
468 }
469
470 size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security) 
471 {
472         return NTLMSSP_SIG_SIZE;
473 }
474
475 NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security, 
476                              TALLOC_CTX *sig_mem_ctx, 
477                              const DATA_BLOB *in, 
478                              DATA_BLOB *out)
479 {
480         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
481         DATA_BLOB sig;
482         NTSTATUS nt_status;
483
484         if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
485
486                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
487                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
488
489                 nt_status = gensec_ntlmssp_seal_packet(gensec_security, sig_mem_ctx, 
490                                                        out->data + NTLMSSP_SIG_SIZE, 
491                                                        out->length - NTLMSSP_SIG_SIZE, 
492                                                        out->data + NTLMSSP_SIG_SIZE, 
493                                                        out->length - NTLMSSP_SIG_SIZE, 
494                                                        &sig);
495
496                 if (NT_STATUS_IS_OK(nt_status)) {
497                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
498                 }
499                 return nt_status;
500
501         } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) 
502                    || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
503
504                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
505                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
506
507                 nt_status = gensec_ntlmssp_sign_packet(gensec_security, sig_mem_ctx, 
508                                                 out->data + NTLMSSP_SIG_SIZE, 
509                                                 out->length - NTLMSSP_SIG_SIZE, 
510                                                 out->data + NTLMSSP_SIG_SIZE, 
511                                                 out->length - NTLMSSP_SIG_SIZE, 
512                                                 &sig);
513
514                 if (NT_STATUS_IS_OK(nt_status)) {
515                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
516                 }
517                 return nt_status;
518
519         } else {
520                 *out = *in;
521                 return NT_STATUS_OK;
522         }
523 }
524
525
526 NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security, 
527                                TALLOC_CTX *sig_mem_ctx, 
528                                const DATA_BLOB *in, 
529                                DATA_BLOB *out)
530 {
531         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
532         DATA_BLOB sig;
533
534         if (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
535                 if (in->length < NTLMSSP_SIG_SIZE) {
536                         return NT_STATUS_INVALID_PARAMETER;
537                 }
538                 sig.data = in->data;
539                 sig.length = NTLMSSP_SIG_SIZE;
540
541                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
542                 
543                 return gensec_ntlmssp_unseal_packet(gensec_security, sig_mem_ctx, 
544                                                     out->data, out->length, 
545                                                     out->data, out->length, 
546                                                     &sig);
547                                                   
548         } else if ((gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN) 
549                    || (gensec_ntlmssp_state->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
550                 if (in->length < NTLMSSP_SIG_SIZE) {
551                         return NT_STATUS_INVALID_PARAMETER;
552                 }
553                 sig.data = in->data;
554                 sig.length = NTLMSSP_SIG_SIZE;
555
556                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
557                 
558                 return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, 
559                                             out->data, out->length, 
560                                             out->data, out->length, 
561                                             &sig);
562         } else {
563                 *out = *in;
564                 return NT_STATUS_OK;
565         }
566 }
567