s4: ran minimal_includes.pl on source4/auth/ntlmssp
[ira/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 3 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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "auth/ntlmssp/ntlmssp.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../lib/crypto/crypto.h"
26 #include "auth/gensec/gensec.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 the 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, (const uint8_t *)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 gensec_ntlmssp_state *gensec_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 (gensec_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, gensec_ntlmssp_state->crypt.ntlm2.send_seq_num);
82                         gensec_ntlmssp_state->crypt.ntlm2.send_seq_num++;
83                         hmac_md5_init_limK_to_64(gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.data, 
84                                                  gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.length, &ctx);
85                         break;
86                 case NTLMSSP_RECEIVE:
87                         SIVAL(seq_num, 0, gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num);
88                         gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num++;
89                         hmac_md5_init_limK_to_64(gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data, 
90                                                  gensec_ntlmssp_state->crypt.ntlm2.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 && gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
98                         switch (direction) {
99                         case NTLMSSP_SEND:
100                                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, digest, 8);
101                                 break;
102                         case NTLMSSP_RECEIVE:
103                                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state, 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                 DEBUG(10, ("NTLM2: created signature over %llu bytes of input:\n", (unsigned long long)pdu_length));
113                 dump_data(11, sig->data, sig->length);
114                         
115         } else {
116                 uint32_t crc;
117                 crc = crc32_calc_buffer(data, length);
118                 if (!msrpc_gen(sig_mem_ctx, 
119                                sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, gensec_ntlmssp_state->crypt.ntlm.seq_num)) {
120                         return NT_STATUS_NO_MEMORY;
121                 }
122                 gensec_ntlmssp_state->crypt.ntlm.seq_num++;
123
124                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, sig->data+4, sig->length-4);
125
126                 DEBUG(10, ("NTLM1: created signature over %llu bytes of input:\n", (unsigned long long)length));
127                 dump_data(11, sig->data, sig->length);
128         }
129         return NT_STATUS_OK;
130 }
131
132 /* TODO: make this non-public */
133 NTSTATUS gensec_ntlmssp_sign_packet(struct gensec_security *gensec_security, 
134                                     TALLOC_CTX *sig_mem_ctx, 
135                                     const uint8_t *data, size_t length, 
136                                     const uint8_t *whole_pdu, size_t pdu_length, 
137                                     DATA_BLOB *sig)
138 {
139         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
140
141         return ntlmssp_make_packet_signature(gensec_ntlmssp_state, sig_mem_ctx, 
142                                              data, length, 
143                                              whole_pdu, pdu_length, 
144                                              NTLMSSP_SEND, sig, true);
145 }
146
147 /**
148  * Check the signature of an incoming packet 
149  *
150  */
151
152 NTSTATUS gensec_ntlmssp_check_packet(struct gensec_security *gensec_security, 
153                                      TALLOC_CTX *sig_mem_ctx, 
154                                      const uint8_t *data, size_t length, 
155                                      const uint8_t *whole_pdu, size_t pdu_length, 
156                                      const DATA_BLOB *sig)
157 {
158         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
159
160         DATA_BLOB local_sig;
161         NTSTATUS nt_status;
162
163         if (!gensec_ntlmssp_state->session_key.length) {
164                 DEBUG(3, ("NO session key, cannot check packet signature\n"));
165                 return NT_STATUS_NO_USER_SESSION_KEY;
166         }
167
168         nt_status = ntlmssp_make_packet_signature(gensec_ntlmssp_state, sig_mem_ctx, 
169                                                   data, length, 
170                                                   whole_pdu, pdu_length, 
171                                                   NTLMSSP_RECEIVE, &local_sig, true);
172         
173         if (!NT_STATUS_IS_OK(nt_status)) {
174                 DEBUG(0, ("NTLMSSP packet check failed with %s\n", nt_errstr(nt_status)));
175                 return nt_status;
176         }
177
178         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
179                 if (local_sig.length != sig->length ||
180                     memcmp(local_sig.data, 
181                            sig->data, sig->length) != 0) {
182                         DEBUG(5, ("BAD SIG NTLM2: wanted signature over %llu bytes of input:\n", (unsigned long long)pdu_length));
183                         dump_data(5, local_sig.data, local_sig.length);
184                         
185                         DEBUG(5, ("BAD SIG: got signature over %llu bytes of input:\n", (unsigned long long)pdu_length));
186                         dump_data(5, sig->data, sig->length);
187                         
188                         DEBUG(1, ("NTLMSSP NTLM2 packet check failed due to invalid signature on %llu bytes of input!\n", (unsigned long long)pdu_length));
189                         return NT_STATUS_ACCESS_DENIED;
190                 }
191         } else {
192                 if (local_sig.length != sig->length ||
193                     memcmp(local_sig.data + 8, 
194                            sig->data + 8, sig->length - 8) != 0) {
195                         DEBUG(5, ("BAD SIG NTLM1: wanted signature of %llu bytes of input:\n", (unsigned long long)length));
196                         dump_data(5, local_sig.data, local_sig.length);
197                         
198                         DEBUG(5, ("BAD SIG: got signature of %llu bytes of input:\n", (unsigned long long)length));
199                         dump_data(5, sig->data, sig->length);
200                         
201                         DEBUG(1, ("NTLMSSP NTLM1 packet check failed due to invalid signature on %llu bytes of input:\n", (unsigned long long)length));
202                         return NT_STATUS_ACCESS_DENIED;
203                 }
204         }
205         dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
206
207         return NT_STATUS_OK;
208 }
209
210
211 /**
212  * Seal data with the NTLMSSP algorithm
213  *
214  */
215
216 NTSTATUS gensec_ntlmssp_seal_packet(struct gensec_security *gensec_security, 
217                                     TALLOC_CTX *sig_mem_ctx, 
218                                     uint8_t *data, size_t length, 
219                                     const uint8_t *whole_pdu, size_t pdu_length, 
220                                     DATA_BLOB *sig)
221 {
222         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
223         NTSTATUS nt_status;
224         if (!gensec_ntlmssp_state->session_key.length) {
225                 DEBUG(3, ("NO session key, cannot seal packet\n"));
226                 return NT_STATUS_NO_USER_SESSION_KEY;
227         }
228
229         DEBUG(10,("ntlmssp_seal_data: seal\n"));
230         dump_data_pw("ntlmssp clear data\n", data, length);
231         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
232                 /* The order of these two operations matters - we must first seal the packet,
233                    then seal the sequence number - this is becouse the send_seal_hash is not
234                    constant, but is is rather updated with each iteration */
235                 nt_status = ntlmssp_make_packet_signature(gensec_ntlmssp_state, sig_mem_ctx, 
236                                                           data, length, 
237                                                           whole_pdu, pdu_length, 
238                                                           NTLMSSP_SEND, sig, false);
239                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, data, length);
240                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
241                         arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, sig->data+4, 8);
242                 }
243         } else {
244                 uint32_t crc;
245                 crc = crc32_calc_buffer(data, length);
246                 if (!msrpc_gen(sig_mem_ctx, 
247                                sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, gensec_ntlmssp_state->crypt.ntlm.seq_num)) {
248                         return NT_STATUS_NO_MEMORY;
249                 }
250
251                 /* The order of these two operations matters - we must
252                    first seal the packet, then seal the sequence
253                    number - this is becouse the ntlmssp_hash is not
254                    constant, but is is rather updated with each
255                    iteration */
256
257                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, data, length);
258                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, sig->data+4, sig->length-4);
259                 /* increment counter on send */
260                 gensec_ntlmssp_state->crypt.ntlm.seq_num++;
261                 nt_status = NT_STATUS_OK;
262         }
263         dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
264         dump_data_pw("ntlmssp sealed data\n", data, length);
265
266
267         return nt_status;
268 }
269
270 /**
271  * Unseal data with the NTLMSSP algorithm
272  *
273  */
274
275 /*
276   wrappers for the ntlmssp_*() functions
277 */
278 NTSTATUS gensec_ntlmssp_unseal_packet(struct gensec_security *gensec_security, 
279                                       TALLOC_CTX *sig_mem_ctx, 
280                                       uint8_t *data, size_t length, 
281                                       const uint8_t *whole_pdu, size_t pdu_length, 
282                                       const DATA_BLOB *sig)
283 {
284         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
285         if (!gensec_ntlmssp_state->session_key.length) {
286                 DEBUG(3, ("NO session key, cannot unseal packet\n"));
287                 return NT_STATUS_NO_USER_SESSION_KEY;
288         }
289
290         dump_data_pw("ntlmssp sealed data\n", data, length);
291         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
292                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state, data, length);
293         } else {
294                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, data, length);
295         }
296         dump_data_pw("ntlmssp clear data\n", data, length);
297         return gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, data, length, whole_pdu, pdu_length, sig);
298 }
299
300 /**
301    Initialise the state for NTLMSSP signing.
302 */
303 /* TODO: make this non-public */
304 NTSTATUS ntlmssp_sign_init(struct gensec_ntlmssp_state *gensec_ntlmssp_state)
305 {
306         TALLOC_CTX *mem_ctx = talloc_new(gensec_ntlmssp_state);
307
308         if (!mem_ctx) {
309                 return NT_STATUS_NO_MEMORY;
310         }
311
312         DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
313         debug_ntlmssp_flags(gensec_ntlmssp_state->neg_flags);
314
315         if (gensec_ntlmssp_state->session_key.length < 8) {
316                 talloc_free(mem_ctx);
317                 DEBUG(3, ("NO session key, cannot intialise signing\n"));
318                 return NT_STATUS_NO_USER_SESSION_KEY;
319         }
320
321         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
322         {
323                 DATA_BLOB weak_session_key = gensec_ntlmssp_state->session_key;
324                 const char *send_sign_const;
325                 const char *send_seal_const;
326                 const char *recv_sign_const;
327                 const char *recv_seal_const;
328
329                 DATA_BLOB send_seal_key;
330                 DATA_BLOB recv_seal_key;
331
332                 switch (gensec_ntlmssp_state->role) {
333                 case NTLMSSP_CLIENT:
334                         send_sign_const = CLI_SIGN;
335                         send_seal_const = CLI_SEAL;
336                         recv_sign_const = SRV_SIGN;
337                         recv_seal_const = SRV_SEAL;
338                         break;
339                 case NTLMSSP_SERVER:
340                         send_sign_const = SRV_SIGN;
341                         send_seal_const = SRV_SEAL;
342                         recv_sign_const = CLI_SIGN;
343                         recv_seal_const = CLI_SEAL;
344                         break;
345                 default:
346                         talloc_free(mem_ctx);
347                         return NT_STATUS_INTERNAL_ERROR;
348                 }
349                 
350                 gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
351                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state);
352                 gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
353                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state);
354
355                 /**
356                    Weaken NTLMSSP keys to cope with down-level
357                    clients, servers and export restrictions.
358                    
359                    We probably should have some parameters to control
360                    this, once we get NTLM2 working.
361                 */
362
363                 /* Key weakening was not performed on the master key
364                  * for NTLM2 (in ntlmssp_weaken_keys()), but must be
365                  * done on the encryption subkeys only.  That is why
366                  * we don't have this code for the ntlmv1 case.
367                  */
368
369                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
370                         
371                 } else if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
372                         weak_session_key.length = 7;
373                 } else { /* forty bits */
374                         weak_session_key.length = 5;
375                 }
376                 dump_data_pw("NTLMSSP weakend master key:\n",
377                              weak_session_key.data, 
378                              weak_session_key.length);
379
380                 /* SEND: sign key */
381                 calc_ntlmv2_key(gensec_ntlmssp_state, 
382                                 &gensec_ntlmssp_state->crypt.ntlm2.send_sign_key, 
383                                 gensec_ntlmssp_state->session_key, send_sign_const);
384                 dump_data_pw("NTLMSSP send sign key:\n",
385                              gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.data, 
386                              gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.length);
387                 
388                 /* SEND: seal ARCFOUR pad */
389                 calc_ntlmv2_key(mem_ctx, 
390                                 &send_seal_key, 
391                                 weak_session_key, send_seal_const);
392                 dump_data_pw("NTLMSSP send seal key:\n",
393                              send_seal_key.data, 
394                              send_seal_key.length);
395                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, 
396                              &send_seal_key);
397                 dump_data_pw("NTLMSSP send sesl hash:\n", 
398                              gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state->sbox, 
399                              sizeof(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state->sbox));
400
401                 /* RECV: sign key */
402                 calc_ntlmv2_key(gensec_ntlmssp_state, 
403                                 &gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key, 
404                                 gensec_ntlmssp_state->session_key, recv_sign_const);
405                 dump_data_pw("NTLMSSP recv sign key:\n",
406                              gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data, 
407                              gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.length);
408
409                 /* RECV: seal ARCFOUR pad */
410                 calc_ntlmv2_key(mem_ctx, 
411                                 &recv_seal_key, 
412                                 weak_session_key, recv_seal_const);
413                 dump_data_pw("NTLMSSP recv seal key:\n",
414                              recv_seal_key.data, 
415                              recv_seal_key.length);
416                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state, 
417                              &recv_seal_key);
418                 dump_data_pw("NTLMSSP receive seal hash:\n", 
419                              gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state->sbox, 
420                              sizeof(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state->sbox));
421
422                 gensec_ntlmssp_state->crypt.ntlm2.send_seq_num = 0;
423                 gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num = 0;
424
425         } else {
426                 DATA_BLOB weak_session_key = ntlmssp_weakend_key(gensec_ntlmssp_state, mem_ctx);
427                 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
428
429                 gensec_ntlmssp_state->crypt.ntlm.arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
430                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm.arcfour_state);
431
432                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, 
433                              &weak_session_key);
434                 dump_data_pw("NTLMSSP hash:\n", gensec_ntlmssp_state->crypt.ntlm.arcfour_state->sbox,
435                              sizeof(gensec_ntlmssp_state->crypt.ntlm.arcfour_state->sbox));
436
437                 gensec_ntlmssp_state->crypt.ntlm.seq_num = 0;
438         }
439
440         talloc_free(mem_ctx);
441         return NT_STATUS_OK;
442 }
443
444 size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security, size_t data_size) 
445 {
446         return NTLMSSP_SIG_SIZE;
447 }
448
449 NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security, 
450                              TALLOC_CTX *sig_mem_ctx, 
451                              const DATA_BLOB *in, 
452                              DATA_BLOB *out)
453 {
454         DATA_BLOB sig;
455         NTSTATUS nt_status;
456
457         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
458
459                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
460                 if (!out->data) {
461                         return NT_STATUS_NO_MEMORY;
462                 }
463                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
464                 
465                 nt_status = gensec_ntlmssp_seal_packet(gensec_security, sig_mem_ctx, 
466                                                        out->data + NTLMSSP_SIG_SIZE, 
467                                                        out->length - NTLMSSP_SIG_SIZE, 
468                                                        out->data + NTLMSSP_SIG_SIZE, 
469                                                        out->length - NTLMSSP_SIG_SIZE, 
470                                                        &sig);
471                 
472                 if (NT_STATUS_IS_OK(nt_status)) {
473                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
474                 }
475                 return nt_status;
476
477         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
478
479                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
480                 if (!out->data) {
481                         return NT_STATUS_NO_MEMORY;
482                 }
483                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
484
485                 nt_status = gensec_ntlmssp_sign_packet(gensec_security, sig_mem_ctx, 
486                                                        out->data + NTLMSSP_SIG_SIZE, 
487                                                        out->length - NTLMSSP_SIG_SIZE, 
488                                                        out->data + NTLMSSP_SIG_SIZE, 
489                                                        out->length - NTLMSSP_SIG_SIZE, 
490                                                        &sig);
491
492                 if (NT_STATUS_IS_OK(nt_status)) {
493                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
494                 }
495                 return nt_status;
496
497         } else {
498                 *out = *in;
499                 return NT_STATUS_OK;
500         }
501 }
502
503
504 NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security, 
505                                TALLOC_CTX *sig_mem_ctx, 
506                                const DATA_BLOB *in, 
507                                DATA_BLOB *out)
508 {
509         DATA_BLOB sig;
510
511         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
512                 if (in->length < NTLMSSP_SIG_SIZE) {
513                         return NT_STATUS_INVALID_PARAMETER;
514                 }
515                 sig.data = in->data;
516                 sig.length = NTLMSSP_SIG_SIZE;
517
518                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
519                 
520                 return gensec_ntlmssp_unseal_packet(gensec_security, sig_mem_ctx, 
521                                                     out->data, out->length, 
522                                                     out->data, out->length, 
523                                                     &sig);
524                                                   
525         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
526                 struct gensec_ntlmssp_state *gensec_ntlmssp_state =
527                 (struct gensec_ntlmssp_state *)gensec_security->private_data;
528                 NTSTATUS status;
529                 uint32_t ntlm_seqnum;
530                 struct arcfour_state ntlm_state;
531                 uint32_t ntlm2_seqnum_r;
532                 uint8_t ntlm2_key_r[16];
533                 struct arcfour_state ntlm2_state_r;
534
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                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
541
542                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
543                         ntlm2_seqnum_r = gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num;
544                         ntlm2_state_r = *gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state;
545                         memcpy(ntlm2_key_r,
546                                gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data,
547                                16);
548                 } else {
549                         ntlm_seqnum = gensec_ntlmssp_state->crypt.ntlm.seq_num;
550                         ntlm_state = *gensec_ntlmssp_state->crypt.ntlm.arcfour_state;
551                 }
552
553                 status = gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx,
554                                                      out->data, out->length,
555                                                      out->data, out->length,
556                                                      &sig);
557                 if (!NT_STATUS_IS_OK(status)) {
558                         NTSTATUS check_status = status;
559                         /*
560                          * The Windows LDAP libraries seems to have a bug
561                          * and always use sealing even if only signing was
562                          * negotiated. So we need to fallback.
563                          */
564
565                         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
566                                 gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num = ntlm2_seqnum_r;
567                                 *gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state = ntlm2_state_r;
568                                 memcpy(gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data,
569                                        ntlm2_key_r, 16);
570                         } else {
571                                 gensec_ntlmssp_state->crypt.ntlm.seq_num = ntlm_seqnum;
572                                 *gensec_ntlmssp_state->crypt.ntlm.arcfour_state = ntlm_state;
573                         }
574
575                         status = gensec_ntlmssp_unseal_packet(gensec_security,
576                                                               sig_mem_ctx,
577                                                               out->data,
578                                                               out->length,
579                                                               out->data,
580                                                               out->length,
581                                                               &sig);
582                         if (NT_STATUS_IS_OK(status)) {
583                                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
584                         } else {
585                                 status = check_status;
586                         }
587                 }
588                 return status;
589         } else {
590                 *out = *in;
591                 return NT_STATUS_OK;
592         }
593 }
594