61b52c6a6174391e724d475576c1af4c0be61c7a
[amitay/samba.git] / libcli / auth / 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 3 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, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "../libcli/auth/ntlmssp.h"
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../lib/crypto/md5.h"
25 #include "../lib/crypto/arcfour.h"
26 #include "../lib/crypto/hmacmd5.h"
27 #include "../lib/crypto/crc32.h"
28 #include "../libcli/auth/ntlmssp_private.h"
29
30 #define CLI_SIGN "session key to client-to-server signing key magic constant"
31 #define CLI_SEAL "session key to client-to-server sealing key magic constant"
32 #define SRV_SIGN "session key to server-to-client signing key magic constant"
33 #define SRV_SEAL "session key to server-to-client sealing key magic constant"
34
35 /**
36  * Some notes on the NTLM2 code:
37  *
38  * NTLM2 is a AEAD system.  This means that the data encrypted is not
39  * all the data that is signed.  In DCE-RPC case, the headers of the
40  * DCE-RPC packets are also signed.  This prevents some of the
41  * fun-and-games one might have by changing them.
42  *
43  */
44
45 static void dump_arc4_state(const char *description,
46                             struct arcfour_state *state)
47 {
48         dump_data_pw(description, state->sbox, sizeof(state->sbox));
49 }
50
51 static void calc_ntlmv2_key(uint8_t subkey[16],
52                             DATA_BLOB session_key,
53                             const char *constant)
54 {
55         struct MD5Context ctx3;
56         MD5Init(&ctx3);
57         MD5Update(&ctx3, session_key.data, session_key.length);
58         MD5Update(&ctx3, (const uint8_t *)constant, strlen(constant)+1);
59         MD5Final(subkey, &ctx3);
60 }
61
62 enum ntlmssp_direction {
63         NTLMSSP_SEND,
64         NTLMSSP_RECEIVE
65 };
66
67 struct ntlmssp_crypt_direction {
68         uint32_t seq_num;
69         uint8_t sign_key[16];
70         struct arcfour_state seal_state;
71 };
72
73 union ntlmssp_crypt_state {
74         /* NTLM */
75         struct ntlmssp_crypt_direction ntlm;
76
77         /* NTLM2 */
78         struct {
79                 struct ntlmssp_crypt_direction sending;
80                 struct ntlmssp_crypt_direction receiving;
81         } ntlm2;
82 };
83
84 static NTSTATUS ntlmssp_make_packet_signature(struct ntlmssp_state *ntlmssp_state,
85                                               TALLOC_CTX *sig_mem_ctx,
86                                               const uint8_t *data, size_t length,
87                                               const uint8_t *whole_pdu, size_t pdu_length,
88                                               enum ntlmssp_direction direction,
89                                               DATA_BLOB *sig, bool encrypt_sig)
90 {
91         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
92                 HMACMD5Context ctx;
93                 uint8_t digest[16];
94                 uint8_t seq_num[4];
95
96                 *sig = data_blob_talloc(sig_mem_ctx, NULL, NTLMSSP_SIG_SIZE);
97                 if (!sig->data) {
98                         return NT_STATUS_NO_MEMORY;
99                 }
100
101                 switch (direction) {
102                 case NTLMSSP_SEND:
103                         DEBUG(100,("ntlmssp_make_packet_signature: SEND seq = %u, len = %u, pdu_len = %u\n",
104                                 ntlmssp_state->crypt->ntlm2.sending.seq_num,
105                                 (unsigned int)length,
106                                 (unsigned int)pdu_length));
107
108                         SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.sending.seq_num);
109                         ntlmssp_state->crypt->ntlm2.sending.seq_num++;
110                         hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.sending.sign_key, 16, &ctx);
111                         break;
112                 case NTLMSSP_RECEIVE:
113
114                         DEBUG(100,("ntlmssp_make_packet_signature: RECV seq = %u, len = %u, pdu_len = %u\n",
115                                 ntlmssp_state->crypt->ntlm2.receiving.seq_num,
116                                 (unsigned int)length,
117                                 (unsigned int)pdu_length));
118
119                         SIVAL(seq_num, 0, ntlmssp_state->crypt->ntlm2.receiving.seq_num);
120                         ntlmssp_state->crypt->ntlm2.receiving.seq_num++;
121                         hmac_md5_init_limK_to_64(ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16, &ctx);
122                         break;
123                 }
124
125                 dump_data_pw("pdu data ", whole_pdu, pdu_length);
126
127                 hmac_md5_update(seq_num, sizeof(seq_num), &ctx);
128                 hmac_md5_update(whole_pdu, pdu_length, &ctx);
129                 hmac_md5_final(digest, &ctx);
130
131                 if (encrypt_sig && (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
132                         switch (direction) {
133                         case NTLMSSP_SEND:
134                                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
135                                                    digest, 8);
136                                 break;
137                         case NTLMSSP_RECEIVE:
138                                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
139                                                    digest, 8);
140                                 break;
141                         }
142                 }
143
144                 SIVAL(sig->data, 0, NTLMSSP_SIGN_VERSION);
145                 memcpy(sig->data + 4, digest, 8);
146                 memcpy(sig->data + 12, seq_num, 4);
147
148                 dump_data_pw("ntlmssp v2 sig ", sig->data, sig->length);
149
150         } else {
151                 bool ok;
152                 uint32_t crc;
153
154                 crc = crc32_calc_buffer(data, length);
155
156                 ok = msrpc_gen(sig_mem_ctx,
157                                sig, "dddd",
158                                NTLMSSP_SIGN_VERSION, 0, crc,
159                                ntlmssp_state->crypt->ntlm.seq_num);
160                 if (!ok) {
161                         return NT_STATUS_NO_MEMORY;
162                 }
163
164                 ntlmssp_state->crypt->ntlm.seq_num++;
165
166                 dump_arc4_state("ntlmssp hash: \n",
167                                 &ntlmssp_state->crypt->ntlm.seal_state);
168                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
169                                    sig->data+4, sig->length-4);
170         }
171         return NT_STATUS_OK;
172 }
173
174 NTSTATUS ntlmssp_sign_packet(struct ntlmssp_state *ntlmssp_state,
175                              TALLOC_CTX *sig_mem_ctx,
176                              const uint8_t *data, size_t length,
177                              const uint8_t *whole_pdu, size_t pdu_length,
178                              DATA_BLOB *sig)
179 {
180         NTSTATUS nt_status;
181
182         if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
183                 DEBUG(3, ("NTLMSSP Signing not negotiated - cannot sign packet!\n"));
184                 return NT_STATUS_INVALID_PARAMETER;
185         }
186
187         if (!ntlmssp_state->session_key.length) {
188                 DEBUG(3, ("NO session key, cannot check sign packet\n"));
189                 return NT_STATUS_NO_USER_SESSION_KEY;
190         }
191
192         nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
193                                                   sig_mem_ctx,
194                                                   data, length,
195                                                   whole_pdu, pdu_length,
196                                                   NTLMSSP_SEND, sig, true);
197
198         return nt_status;
199 }
200
201 /**
202  * Check the signature of an incoming packet
203  * @note caller *must* check that the signature is the size it expects
204  *
205  */
206
207 NTSTATUS ntlmssp_check_packet(struct ntlmssp_state *ntlmssp_state,
208                               const uint8_t *data, size_t length,
209                               const uint8_t *whole_pdu, size_t pdu_length,
210                               const DATA_BLOB *sig)
211 {
212         DATA_BLOB local_sig;
213         NTSTATUS nt_status;
214         TALLOC_CTX *tmp_ctx;
215
216         if (!ntlmssp_state->session_key.length) {
217                 DEBUG(3, ("NO session key, cannot check packet signature\n"));
218                 return NT_STATUS_NO_USER_SESSION_KEY;
219         }
220
221         if (sig->length < 8) {
222                 DEBUG(0, ("NTLMSSP packet check failed due to short signature (%lu bytes)!\n",
223                           (unsigned long)sig->length));
224         }
225
226         tmp_ctx = talloc_new(ntlmssp_state);
227         if (!tmp_ctx) {
228                 return NT_STATUS_NO_MEMORY;
229         }
230
231         nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
232                                                   tmp_ctx,
233                                                   data, length,
234                                                   whole_pdu, pdu_length,
235                                                   NTLMSSP_RECEIVE,
236                                                   &local_sig, true);
237
238         if (!NT_STATUS_IS_OK(nt_status)) {
239                 DEBUG(0,("NTLMSSP packet sig creation failed with %s\n",
240                          nt_errstr(nt_status)));
241                 talloc_free(tmp_ctx);
242                 return nt_status;
243         }
244
245         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
246                 if (local_sig.length != sig->length ||
247                     memcmp(local_sig.data, sig->data, sig->length) != 0) {
248                         DEBUG(5, ("BAD SIG NTLM2: wanted signature of\n"));
249                         dump_data(5, local_sig.data, local_sig.length);
250
251                         DEBUG(5, ("BAD SIG: got signature of\n"));
252                         dump_data(5, sig->data, sig->length);
253
254                         DEBUG(0, ("NTLMSSP NTLM2 packet check failed due to invalid signature!\n"));
255                         talloc_free(tmp_ctx);
256                         return NT_STATUS_ACCESS_DENIED;
257                 }
258         } else {
259                 if (local_sig.length != sig->length ||
260                     memcmp(local_sig.data + 8, sig->data + 8, sig->length - 8) != 0) {
261                         DEBUG(5, ("BAD SIG NTLM1: wanted signature of\n"));
262                         dump_data(5, local_sig.data, local_sig.length);
263
264                         DEBUG(5, ("BAD SIG: got signature of\n"));
265                         dump_data(5, sig->data, sig->length);
266
267                         DEBUG(0, ("NTLMSSP NTLM1 packet check failed due to invalid signature!\n"));
268                         talloc_free(tmp_ctx);
269                         return NT_STATUS_ACCESS_DENIED;
270                 }
271         }
272         dump_data_pw("checked ntlmssp signature\n", sig->data, sig->length);
273         DEBUG(10,("ntlmssp_check_packet: NTLMSSP signature OK !\n"));
274
275         talloc_free(tmp_ctx);
276         return NT_STATUS_OK;
277 }
278
279 /**
280  * Seal data with the NTLMSSP algorithm
281  *
282  */
283
284 NTSTATUS ntlmssp_seal_packet(struct ntlmssp_state *ntlmssp_state,
285                              TALLOC_CTX *sig_mem_ctx,
286                              uint8_t *data, size_t length,
287                              const uint8_t *whole_pdu, size_t pdu_length,
288                              DATA_BLOB *sig)
289 {
290         if (!(ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
291                 DEBUG(3, ("NTLMSSP Sealing not negotiated - cannot seal packet!\n"));
292                 return NT_STATUS_INVALID_PARAMETER;
293         }
294
295         if (!ntlmssp_state->session_key.length) {
296                 DEBUG(3, ("NO session key, cannot seal packet\n"));
297                 return NT_STATUS_NO_USER_SESSION_KEY;
298         }
299
300         DEBUG(10,("ntlmssp_seal_data: seal\n"));
301         dump_data_pw("ntlmssp clear data\n", data, length);
302         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
303                 NTSTATUS nt_status;
304                 /*
305                  * The order of these two operations matters - we
306                  * must first seal the packet, then seal the
307                  * sequence number - this is because the
308                  * send_seal_hash is not constant, but is is rather
309                  * updated with each iteration
310                  */
311                 nt_status = ntlmssp_make_packet_signature(ntlmssp_state,
312                                                           sig_mem_ctx,
313                                                           data, length,
314                                                           whole_pdu, pdu_length,
315                                                           NTLMSSP_SEND,
316                                                           sig, false);
317                 if (!NT_STATUS_IS_OK(nt_status)) {
318                         return nt_status;
319                 }
320
321                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
322                                    data, length);
323                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
324                         arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
325                                            sig->data+4, 8);
326                 }
327         } else {
328                 bool ok;
329                 uint32_t crc;
330
331                 crc = crc32_calc_buffer(data, length);
332
333                 ok = msrpc_gen(sig_mem_ctx,
334                                sig, "dddd",
335                                NTLMSSP_SIGN_VERSION, 0, crc,
336                                ntlmssp_state->crypt->ntlm.seq_num);
337                 if (!ok) {
338                         return NT_STATUS_NO_MEMORY;
339                 }
340
341                 /*
342                  * The order of these two operations matters - we
343                  * must first seal the packet, then seal the
344                  * sequence number - this is because the ntlmv1_arc4_state
345                  * is not constant, but is is rather updated with
346                  * each iteration
347                  */
348
349                 dump_arc4_state("ntlmv1 arc4 state:\n",
350                                 &ntlmssp_state->crypt->ntlm.seal_state);
351                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
352                                    data, length);
353
354                 dump_arc4_state("ntlmv1 arc4 state:\n",
355                                 &ntlmssp_state->crypt->ntlm.seal_state);
356
357                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
358                                    sig->data+4, sig->length-4);
359
360                 ntlmssp_state->crypt->ntlm.seq_num++;
361         }
362         dump_data_pw("ntlmssp signature\n", sig->data, sig->length);
363         dump_data_pw("ntlmssp sealed data\n", data, length);
364
365         return NT_STATUS_OK;
366 }
367
368 /**
369  * Unseal data with the NTLMSSP algorithm
370  *
371  */
372
373 NTSTATUS ntlmssp_unseal_packet(struct ntlmssp_state *ntlmssp_state,
374                                uint8_t *data, size_t length,
375                                const uint8_t *whole_pdu, size_t pdu_length,
376                                const DATA_BLOB *sig)
377 {
378         NTSTATUS status;
379         if (!ntlmssp_state->session_key.length) {
380                 DEBUG(3, ("NO session key, cannot unseal packet\n"));
381                 return NT_STATUS_NO_USER_SESSION_KEY;
382         }
383
384         DEBUG(10,("ntlmssp_unseal_packet: seal\n"));
385         dump_data_pw("ntlmssp sealed data\n", data, length);
386
387         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
388                 /* First unseal the data. */
389                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
390                                    data, length);
391                 dump_data_pw("ntlmv2 clear data\n", data, length);
392         } else {
393                 arcfour_crypt_sbox(&ntlmssp_state->crypt->ntlm.seal_state,
394                                    data, length);
395                 dump_data_pw("ntlmv1 clear data\n", data, length);
396         }
397         status = ntlmssp_check_packet(ntlmssp_state,
398                                       data, length,
399                                       whole_pdu, pdu_length,
400                                       sig);
401
402         if (!NT_STATUS_IS_OK(status)) {
403                 DEBUG(1,("NTLMSSP packet check for unseal failed due to invalid signature on %llu bytes of input:\n",
404                          (unsigned long long)length));
405         }
406         return status;
407 }
408
409 /**
410    Initialise the state for NTLMSSP signing.
411 */
412 NTSTATUS ntlmssp_sign_init(struct ntlmssp_state *ntlmssp_state)
413 {
414         DEBUG(3, ("NTLMSSP Sign/Seal - Initialising with flags:\n"));
415         debug_ntlmssp_flags(ntlmssp_state->neg_flags);
416
417         if (ntlmssp_state->session_key.length < 8) {
418                 DEBUG(3, ("NO session key, cannot intialise signing\n"));
419                 return NT_STATUS_NO_USER_SESSION_KEY;
420         }
421
422         ntlmssp_state->crypt = talloc_zero(ntlmssp_state,
423                                            union ntlmssp_crypt_state);
424         if (ntlmssp_state->crypt == NULL) {
425                 return NT_STATUS_NO_MEMORY;
426         }
427
428         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
429                 DATA_BLOB weak_session_key = ntlmssp_state->session_key;
430                 const char *send_sign_const;
431                 const char *send_seal_const;
432                 const char *recv_sign_const;
433                 const char *recv_seal_const;
434                 uint8_t send_seal_key[16];
435                 DATA_BLOB send_seal_blob = data_blob_const(send_seal_key,
436                                            sizeof(send_seal_key));
437                 uint8_t recv_seal_key[16];
438                 DATA_BLOB recv_seal_blob = data_blob_const(recv_seal_key,
439                                            sizeof(recv_seal_key));
440
441                 switch (ntlmssp_state->role) {
442                 case NTLMSSP_CLIENT:
443                         send_sign_const = CLI_SIGN;
444                         send_seal_const = CLI_SEAL;
445                         recv_sign_const = SRV_SIGN;
446                         recv_seal_const = SRV_SEAL;
447                         break;
448                 case NTLMSSP_SERVER:
449                         send_sign_const = SRV_SIGN;
450                         send_seal_const = SRV_SEAL;
451                         recv_sign_const = CLI_SIGN;
452                         recv_seal_const = CLI_SEAL;
453                         break;
454                 default:
455                         return NT_STATUS_INTERNAL_ERROR;
456                 }
457
458                 /*
459                  * Weaken NTLMSSP keys to cope with down-level
460                  * clients, servers and export restrictions.
461                  *
462                  * We probably should have some parameters to
463                  * control this, once we get NTLM2 working.
464                  *
465                  * Key weakening was not performed on the master key
466                  * for NTLM2, but must be done on the encryption subkeys only.
467                  */
468
469                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_128) {
470                         /* nothing to do */
471                 } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
472                         weak_session_key.length = 7;
473                 } else { /* forty bits */
474                         weak_session_key.length = 5;
475                 }
476
477                 dump_data_pw("NTLMSSP weakend master key:\n",
478                              weak_session_key.data,
479                              weak_session_key.length);
480
481                 /* SEND: sign key */
482                 calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.sending.sign_key,
483                                 ntlmssp_state->session_key, send_sign_const);
484                 dump_data_pw("NTLMSSP send sign key:\n",
485                              ntlmssp_state->crypt->ntlm2.sending.sign_key, 16);
486
487                 /* SEND: seal ARCFOUR pad */
488                 calc_ntlmv2_key(send_seal_key,
489                                 weak_session_key, send_seal_const);
490                 dump_data_pw("NTLMSSP send seal key:\n", send_seal_key, 16);
491
492                 arcfour_init(&ntlmssp_state->crypt->ntlm2.sending.seal_state,
493                              &send_seal_blob);
494
495                 dump_arc4_state("NTLMSSP send seal arc4 state:\n",
496                                 &ntlmssp_state->crypt->ntlm2.sending.seal_state);
497
498                 /* SEND: seq num */
499                 ntlmssp_state->crypt->ntlm2.sending.seq_num = 0;
500
501                 /* RECV: sign key */
502                 calc_ntlmv2_key(ntlmssp_state->crypt->ntlm2.receiving.sign_key,
503                                 ntlmssp_state->session_key, recv_sign_const);
504                 dump_data_pw("NTLMSSP recv sign key:\n",
505                              ntlmssp_state->crypt->ntlm2.receiving.sign_key, 16);
506
507                 /* RECV: seal ARCFOUR pad */
508                 calc_ntlmv2_key(recv_seal_key,
509                                 weak_session_key, recv_seal_const);
510                 dump_data_pw("NTLMSSP recv seal key:\n", recv_seal_key, 16);
511
512                 arcfour_init(&ntlmssp_state->crypt->ntlm2.receiving.seal_state,
513                              &recv_seal_blob);
514
515                 dump_arc4_state("NTLMSSP recv seal arc4 state:\n",
516                                 &ntlmssp_state->crypt->ntlm2.receiving.seal_state);
517
518                 /* RECV: seq num */
519                 ntlmssp_state->crypt->ntlm2.receiving.seq_num = 0;
520         } else {
521                 uint8_t weak_session_key[8];
522                 DATA_BLOB seal_session_key = ntlmssp_state->session_key;
523                 bool do_weak = false;
524
525                 DEBUG(5, ("NTLMSSP Sign/Seal - using NTLM1\n"));
526
527                 /*
528                  * Key weakening not performed on the master key for NTLM2
529                  * and does not occour for NTLM1. Therefore we only need
530                  * to do this for the LM_KEY.
531                  */
532                 if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
533                         do_weak = true;
534                 }
535
536                 /*
537                  * Nothing to weaken.
538                  * We certainly don't want to 'extend' the length...
539                  */
540                 if (seal_session_key.length < 16) {
541                         /* TODO: is this really correct? */
542                         do_weak = false;
543                 }
544
545                 if (do_weak) {
546                         memcpy(weak_session_key, seal_session_key.data, 8);
547                         seal_session_key = data_blob_const(weak_session_key, 8);
548
549                         /*
550                          * LM key doesn't support 128 bit crypto, so this is
551                          * the best we can do. If you negotiate 128 bit, but
552                          * not 56, you end up with 40 bit...
553                          */
554                         if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
555                                 weak_session_key[7] = 0xa0;
556                         } else { /* forty bits */
557                                 weak_session_key[5] = 0xe5;
558                                 weak_session_key[6] = 0x38;
559                                 weak_session_key[7] = 0xb0;
560                         }
561                 }
562
563                 arcfour_init(&ntlmssp_state->crypt->ntlm.seal_state,
564                              &seal_session_key);
565
566                 dump_arc4_state("NTLMv1 arc4 state:\n",
567                                 &ntlmssp_state->crypt->ntlm.seal_state);
568
569                 ntlmssp_state->crypt->ntlm.seq_num = 0;
570         }
571
572         return NT_STATUS_OK;
573 }