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