9e0d80f788243b2711e105617ffc6b7eee740fb6
[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 sig creation 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
183                         DEBUG(10, ("BAD SIG NTLM2: wanted signature over %llu bytes of input:\n", (unsigned long long)pdu_length));
184                         dump_data(10, local_sig.data, local_sig.length);
185                         
186                         DEBUG(10, ("BAD SIG: got signature over %llu bytes of input:\n", (unsigned long long)pdu_length));
187                         dump_data(10, sig->data, sig->length);
188                         
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(10, ("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(10, ("BAD SIG: got signature of %llu bytes of input:\n", (unsigned long long)length));
199                         dump_data(10, sig->data, sig->length);
200                         
201                         return NT_STATUS_ACCESS_DENIED;
202                 }
203         }
204         dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
205
206         return NT_STATUS_OK;
207 }
208
209
210 /**
211  * Seal data with the NTLMSSP algorithm
212  *
213  */
214
215 NTSTATUS gensec_ntlmssp_seal_packet(struct gensec_security *gensec_security, 
216                                     TALLOC_CTX *sig_mem_ctx, 
217                                     uint8_t *data, size_t length, 
218                                     const uint8_t *whole_pdu, size_t pdu_length, 
219                                     DATA_BLOB *sig)
220 {
221         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
222         NTSTATUS nt_status;
223         if (!gensec_ntlmssp_state->session_key.length) {
224                 DEBUG(3, ("NO session key, cannot seal packet\n"));
225                 return NT_STATUS_NO_USER_SESSION_KEY;
226         }
227
228         DEBUG(10,("ntlmssp_seal_data: seal\n"));
229         dump_data_pw("ntlmssp clear data\n", data, length);
230         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
231                 /* The order of these two operations matters - we must first seal the packet,
232                    then seal the sequence number - this is becouse the send_seal_hash is not
233                    constant, but is is rather updated with each iteration */
234                 nt_status = ntlmssp_make_packet_signature(gensec_ntlmssp_state, sig_mem_ctx, 
235                                                           data, length, 
236                                                           whole_pdu, pdu_length, 
237                                                           NTLMSSP_SEND, sig, false);
238                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, data, length);
239                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
240                         arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, sig->data+4, 8);
241                 }
242         } else {
243                 uint32_t crc;
244                 crc = crc32_calc_buffer(data, length);
245                 if (!msrpc_gen(sig_mem_ctx, 
246                                sig, "dddd", NTLMSSP_SIGN_VERSION, 0, crc, gensec_ntlmssp_state->crypt.ntlm.seq_num)) {
247                         return NT_STATUS_NO_MEMORY;
248                 }
249
250                 /* The order of these two operations matters - we must
251                    first seal the packet, then seal the sequence
252                    number - this is becouse the ntlmssp_hash is not
253                    constant, but is is rather updated with each
254                    iteration */
255
256                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, data, length);
257                 arcfour_crypt_sbox(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, sig->data+4, sig->length-4);
258                 /* increment counter on send */
259                 gensec_ntlmssp_state->crypt.ntlm.seq_num++;
260                 nt_status = NT_STATUS_OK;
261         }
262         dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
263         dump_data_pw("ntlmssp sealed data\n", data, length);
264
265
266         return nt_status;
267 }
268
269 /**
270  * Unseal data with the NTLMSSP algorithm
271  *
272  */
273
274 /*
275   wrappers for the ntlmssp_*() functions
276 */
277 NTSTATUS gensec_ntlmssp_unseal_packet(struct gensec_security *gensec_security, 
278                                       TALLOC_CTX *sig_mem_ctx, 
279                                       uint8_t *data, size_t length, 
280                                       const uint8_t *whole_pdu, size_t pdu_length, 
281                                       const DATA_BLOB *sig)
282 {
283         NTSTATUS status;
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         status = gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx, data, length, whole_pdu, pdu_length, sig);
298
299         if (!NT_STATUS_IS_OK(status)) {
300                 DEBUG(1, ("NTLMSSP packet check for unseal failed due to invalid signature on %llu bytes of input:\n", (unsigned long long)length));
301         }
302         return status;
303 }
304
305 /**
306    Initialise the state for NTLMSSP signing.
307 */
308 /* TODO: make this non-public */
309 NTSTATUS ntlmssp_sign_init(struct gensec_ntlmssp_state *gensec_ntlmssp_state)
310 {
311         TALLOC_CTX *mem_ctx = talloc_new(gensec_ntlmssp_state);
312
313         if (!mem_ctx) {
314                 return NT_STATUS_NO_MEMORY;
315         }
316
317         DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
318         debug_ntlmssp_flags(gensec_ntlmssp_state->neg_flags);
319
320         if (gensec_ntlmssp_state->session_key.length < 8) {
321                 talloc_free(mem_ctx);
322                 DEBUG(3, ("NO session key, cannot intialise signing\n"));
323                 return NT_STATUS_NO_USER_SESSION_KEY;
324         }
325
326         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2)
327         {
328                 DATA_BLOB weak_session_key = gensec_ntlmssp_state->session_key;
329                 const char *send_sign_const;
330                 const char *send_seal_const;
331                 const char *recv_sign_const;
332                 const char *recv_seal_const;
333
334                 DATA_BLOB send_seal_key;
335                 DATA_BLOB recv_seal_key;
336
337                 switch (gensec_ntlmssp_state->role) {
338                 case NTLMSSP_CLIENT:
339                         send_sign_const = CLI_SIGN;
340                         send_seal_const = CLI_SEAL;
341                         recv_sign_const = SRV_SIGN;
342                         recv_seal_const = SRV_SEAL;
343                         break;
344                 case NTLMSSP_SERVER:
345                         send_sign_const = SRV_SIGN;
346                         send_seal_const = SRV_SEAL;
347                         recv_sign_const = CLI_SIGN;
348                         recv_seal_const = CLI_SEAL;
349                         break;
350                 default:
351                         talloc_free(mem_ctx);
352                         return NT_STATUS_INTERNAL_ERROR;
353                 }
354                 
355                 gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
356                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state);
357                 gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
358                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state);
359
360                 /**
361                    Weaken NTLMSSP keys to cope with down-level
362                    clients, servers and export restrictions.
363                    
364                    We probably should have some parameters to control
365                    this, once we get NTLM2 working.
366                 */
367
368                 /* Key weakening was not performed on the master key
369                  * for NTLM2 (in ntlmssp_weaken_keys()), but must be
370                  * done on the encryption subkeys only.  That is why
371                  * we don't have this code for the ntlmv1 case.
372                  */
373
374                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
375                         
376                 } else if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
377                         weak_session_key.length = 7;
378                 } else { /* forty bits */
379                         weak_session_key.length = 5;
380                 }
381                 dump_data_pw("NTLMSSP weakend master key:\n",
382                              weak_session_key.data, 
383                              weak_session_key.length);
384
385                 /* SEND: sign key */
386                 calc_ntlmv2_key(gensec_ntlmssp_state, 
387                                 &gensec_ntlmssp_state->crypt.ntlm2.send_sign_key, 
388                                 gensec_ntlmssp_state->session_key, send_sign_const);
389                 dump_data_pw("NTLMSSP send sign key:\n",
390                              gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.data, 
391                              gensec_ntlmssp_state->crypt.ntlm2.send_sign_key.length);
392                 
393                 /* SEND: seal ARCFOUR pad */
394                 calc_ntlmv2_key(mem_ctx, 
395                                 &send_seal_key, 
396                                 weak_session_key, send_seal_const);
397                 dump_data_pw("NTLMSSP send seal key:\n",
398                              send_seal_key.data, 
399                              send_seal_key.length);
400                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state, 
401                              &send_seal_key);
402                 dump_data_pw("NTLMSSP send sesl hash:\n", 
403                              gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state->sbox, 
404                              sizeof(gensec_ntlmssp_state->crypt.ntlm2.send_seal_arcfour_state->sbox));
405
406                 /* RECV: sign key */
407                 calc_ntlmv2_key(gensec_ntlmssp_state, 
408                                 &gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key, 
409                                 gensec_ntlmssp_state->session_key, recv_sign_const);
410                 dump_data_pw("NTLMSSP recv sign key:\n",
411                              gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data, 
412                              gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.length);
413
414                 /* RECV: seal ARCFOUR pad */
415                 calc_ntlmv2_key(mem_ctx, 
416                                 &recv_seal_key, 
417                                 weak_session_key, recv_seal_const);
418                 dump_data_pw("NTLMSSP recv seal key:\n",
419                              recv_seal_key.data, 
420                              recv_seal_key.length);
421                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state, 
422                              &recv_seal_key);
423                 dump_data_pw("NTLMSSP receive seal hash:\n", 
424                              gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state->sbox, 
425                              sizeof(gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state->sbox));
426
427                 gensec_ntlmssp_state->crypt.ntlm2.send_seq_num = 0;
428                 gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num = 0;
429
430         } else {
431                 DATA_BLOB weak_session_key = ntlmssp_weakend_key(gensec_ntlmssp_state, mem_ctx);
432                 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
433
434                 gensec_ntlmssp_state->crypt.ntlm.arcfour_state = talloc(gensec_ntlmssp_state, struct arcfour_state);
435                 NT_STATUS_HAVE_NO_MEMORY(gensec_ntlmssp_state->crypt.ntlm.arcfour_state);
436
437                 arcfour_init(gensec_ntlmssp_state->crypt.ntlm.arcfour_state, 
438                              &weak_session_key);
439                 dump_data_pw("NTLMSSP hash:\n", gensec_ntlmssp_state->crypt.ntlm.arcfour_state->sbox,
440                              sizeof(gensec_ntlmssp_state->crypt.ntlm.arcfour_state->sbox));
441
442                 gensec_ntlmssp_state->crypt.ntlm.seq_num = 0;
443         }
444
445         talloc_free(mem_ctx);
446         return NT_STATUS_OK;
447 }
448
449 size_t gensec_ntlmssp_sig_size(struct gensec_security *gensec_security, size_t data_size) 
450 {
451         return NTLMSSP_SIG_SIZE;
452 }
453
454 NTSTATUS gensec_ntlmssp_wrap(struct gensec_security *gensec_security, 
455                              TALLOC_CTX *sig_mem_ctx, 
456                              const DATA_BLOB *in, 
457                              DATA_BLOB *out)
458 {
459         DATA_BLOB sig;
460         NTSTATUS nt_status;
461
462         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
463
464                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
465                 if (!out->data) {
466                         return NT_STATUS_NO_MEMORY;
467                 }
468                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
469                 
470                 nt_status = gensec_ntlmssp_seal_packet(gensec_security, sig_mem_ctx, 
471                                                        out->data + NTLMSSP_SIG_SIZE, 
472                                                        out->length - NTLMSSP_SIG_SIZE, 
473                                                        out->data + NTLMSSP_SIG_SIZE, 
474                                                        out->length - NTLMSSP_SIG_SIZE, 
475                                                        &sig);
476                 
477                 if (NT_STATUS_IS_OK(nt_status)) {
478                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
479                 }
480                 return nt_status;
481
482         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
483
484                 *out = data_blob_talloc(sig_mem_ctx, NULL, in->length + NTLMSSP_SIG_SIZE);
485                 if (!out->data) {
486                         return NT_STATUS_NO_MEMORY;
487                 }
488                 memcpy(out->data + NTLMSSP_SIG_SIZE, in->data, in->length);
489
490                 nt_status = gensec_ntlmssp_sign_packet(gensec_security, sig_mem_ctx, 
491                                                        out->data + NTLMSSP_SIG_SIZE, 
492                                                        out->length - NTLMSSP_SIG_SIZE, 
493                                                        out->data + NTLMSSP_SIG_SIZE, 
494                                                        out->length - NTLMSSP_SIG_SIZE, 
495                                                        &sig);
496
497                 if (NT_STATUS_IS_OK(nt_status)) {
498                         memcpy(out->data, sig.data, NTLMSSP_SIG_SIZE);
499                 }
500                 return nt_status;
501
502         } else {
503                 *out = *in;
504                 return NT_STATUS_OK;
505         }
506 }
507
508
509 NTSTATUS gensec_ntlmssp_unwrap(struct gensec_security *gensec_security, 
510                                TALLOC_CTX *sig_mem_ctx, 
511                                const DATA_BLOB *in, 
512                                DATA_BLOB *out)
513 {
514         DATA_BLOB sig;
515
516         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
517                 if (in->length < NTLMSSP_SIG_SIZE) {
518                         return NT_STATUS_INVALID_PARAMETER;
519                 }
520                 sig.data = in->data;
521                 sig.length = NTLMSSP_SIG_SIZE;
522
523                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
524                 
525                 return gensec_ntlmssp_unseal_packet(gensec_security, sig_mem_ctx, 
526                                                     out->data, out->length, 
527                                                     out->data, out->length, 
528                                                     &sig);
529                                                   
530         } else if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
531                 struct gensec_ntlmssp_state *gensec_ntlmssp_state =
532                 (struct gensec_ntlmssp_state *)gensec_security->private_data;
533                 NTSTATUS status;
534                 uint32_t ntlm_seqnum;
535                 struct arcfour_state ntlm_state;
536                 uint32_t ntlm2_seqnum_r;
537                 uint8_t ntlm2_key_r[16];
538                 struct arcfour_state ntlm2_state_r;
539
540                 if (in->length < NTLMSSP_SIG_SIZE) {
541                         return NT_STATUS_INVALID_PARAMETER;
542                 }
543                 sig.data = in->data;
544                 sig.length = NTLMSSP_SIG_SIZE;
545                 *out = data_blob_talloc(sig_mem_ctx, in->data + NTLMSSP_SIG_SIZE, in->length - NTLMSSP_SIG_SIZE);
546
547                 if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
548                         ntlm2_seqnum_r = gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num;
549                         ntlm2_state_r = *gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state;
550                         memcpy(ntlm2_key_r,
551                                gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data,
552                                16);
553                 } else {
554                         ntlm_seqnum = gensec_ntlmssp_state->crypt.ntlm.seq_num;
555                         ntlm_state = *gensec_ntlmssp_state->crypt.ntlm.arcfour_state;
556                 }
557
558                 status = gensec_ntlmssp_check_packet(gensec_security, sig_mem_ctx,
559                                                      out->data, out->length,
560                                                      out->data, out->length,
561                                                      &sig);
562                 if (!NT_STATUS_IS_OK(status)) {
563                         NTSTATUS check_status = status;
564                         /*
565                          * The Windows LDAP libraries seems to have a bug
566                          * and always use sealing even if only signing was
567                          * negotiated. So we need to fallback.
568                          */
569
570                         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
571                                 gensec_ntlmssp_state->crypt.ntlm2.recv_seq_num = ntlm2_seqnum_r;
572                                 *gensec_ntlmssp_state->crypt.ntlm2.recv_seal_arcfour_state = ntlm2_state_r;
573                                 memcpy(gensec_ntlmssp_state->crypt.ntlm2.recv_sign_key.data,
574                                        ntlm2_key_r, 16);
575                         } else {
576                                 gensec_ntlmssp_state->crypt.ntlm.seq_num = ntlm_seqnum;
577                                 *gensec_ntlmssp_state->crypt.ntlm.arcfour_state = ntlm_state;
578                         }
579
580                         status = gensec_ntlmssp_unseal_packet(gensec_security,
581                                                               sig_mem_ctx,
582                                                               out->data,
583                                                               out->length,
584                                                               out->data,
585                                                               out->length,
586                                                               &sig);
587                         if (NT_STATUS_IS_OK(status)) {
588                                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
589                         } else {
590                                 status = check_status;
591                         }
592                 }
593
594                 if (!NT_STATUS_IS_OK(status)) {
595                         DEBUG(1, ("NTLMSSP packet check for unwrap failed due to invalid signature\n"));
596                 }
597                 return status;
598         } else {
599                 *out = *in;
600                 return NT_STATUS_OK;
601         }
602 }
603