91509f0fb81d6cb3a10f5b3f3bd8782d6bf8dca3
[samba.git] / source3 / libsmb / smb_signing.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB Signing Code
4    Copyright (C) Jeremy Allison 2003.
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
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
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* Lookup a packet's MID (multiplex id) and figure out it's sequence number */
25 struct outstanding_packet_lookup {
26         uint16 mid;
27         uint32 reply_seq_num;
28         struct outstanding_packet_lookup *prev, *next;
29 };
30
31 /* Store the data for an ongoing trans/trans2/nttrans operation. */
32 struct trans_info_context {
33         uint16 mid;
34         uint32 send_seq_num;
35         uint32 reply_seq_num;
36 };
37
38 struct smb_basic_signing_context {
39         DATA_BLOB mac_key;
40         uint32 send_seq_num;
41         struct trans_info_context *trans_info;
42         struct outstanding_packet_lookup *outstanding_packet_list;
43 };
44
45 static void store_sequence_for_reply(struct outstanding_packet_lookup **list, 
46                                      uint16 mid, uint32 reply_seq_num)
47 {
48         struct outstanding_packet_lookup *t;
49         struct outstanding_packet_lookup *tmp;
50         
51         t = smb_xmalloc(sizeof(*t));
52         ZERO_STRUCTP(t);
53
54         DLIST_ADD_END(*list, t, tmp);
55         t->mid = mid;
56         t->reply_seq_num = reply_seq_num;
57
58         DEBUG(10,("store_sequence_for_reply: stored seq = %u mid = %u\n",
59                         (unsigned int)reply_seq_num, (unsigned int)mid ));
60 }
61
62 static BOOL get_sequence_for_reply(struct outstanding_packet_lookup **list,
63                                    uint16 mid, uint32 *reply_seq_num)
64 {
65         struct outstanding_packet_lookup *t;
66
67         for (t = *list; t; t = t->next) {
68                 if (t->mid == mid) {
69                         *reply_seq_num = t->reply_seq_num;
70                         DEBUG(10,("get_sequence_for_reply: found seq = %u mid = %u\n",
71                                 (unsigned int)t->reply_seq_num, (unsigned int)t->mid ));
72                         DLIST_REMOVE(*list, t);
73                         SAFE_FREE(t);
74                         return True;
75                 }
76         }
77         return False;
78 }
79
80 /***********************************************************
81  SMB signing - Common code before we set a new signing implementation
82 ************************************************************/
83
84 static BOOL cli_set_smb_signing_common(struct cli_state *cli) 
85 {
86         if (!cli->sign_info.negotiated_smb_signing 
87             && !cli->sign_info.mandatory_signing) {
88                 return False;
89         }
90
91         if (cli->sign_info.doing_signing) {
92                 return False;
93         }
94         
95         if (cli->sign_info.free_signing_context)
96                 cli->sign_info.free_signing_context(&cli->sign_info);
97
98         /* These calls are INCOMPATIBLE with SMB signing */
99         cli->readbraw_supported = False;
100         cli->writebraw_supported = False;
101         
102         return True;
103 }
104
105 /***********************************************************
106  SMB signing - Common code for 'real' implementations
107 ************************************************************/
108
109 static BOOL set_smb_signing_real_common(struct smb_sign_info *si)
110 {
111         if (si->mandatory_signing) {
112                 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
113         }
114
115         si->doing_signing = True;
116         DEBUG(5, ("SMB signing enabled!\n"));
117
118         return True;
119 }
120
121 static void mark_packet_signed(char *outbuf)
122 {
123         uint16 flags2;
124         flags2 = SVAL(outbuf,smb_flg2);
125         flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
126         SSVAL(outbuf,smb_flg2, flags2);
127 }
128
129 /***********************************************************
130  SMB signing - NULL implementation - calculate a MAC to send.
131 ************************************************************/
132
133 static void null_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
134 {
135         /* we can't zero out the sig, as we might be trying to send a
136            session request - which is NBT-level, not SMB level and doesn't
137            have the field */
138         return;
139 }
140
141 /***********************************************************
142  SMB signing - NULL implementation - check a MAC sent by server.
143 ************************************************************/
144
145 static BOOL null_check_incoming_message(char *inbuf, struct smb_sign_info *si)
146 {
147         return True;
148 }
149
150 /***********************************************************
151  SMB signing - NULL implementation - free signing context
152 ************************************************************/
153
154 static void null_free_signing_context(struct smb_sign_info *si)
155 {
156         return;
157 }
158
159 /**
160  SMB signing - NULL implementation - setup the MAC key.
161
162  @note Used as an initialisation only - it will not correctly
163        shut down a real signing mechanism
164 */
165
166 static BOOL null_set_signing(struct smb_sign_info *si)
167 {
168         si->signing_context = NULL;
169         
170         si->sign_outgoing_message = null_sign_outgoing_message;
171         si->check_incoming_message = null_check_incoming_message;
172         si->free_signing_context = null_free_signing_context;
173
174         return True;
175 }
176
177 /**
178  * Free the signing context
179  */
180  
181 static void free_signing_context(struct smb_sign_info *si)
182 {
183         if (si->free_signing_context) {
184                 si->free_signing_context(si);
185                 si->signing_context = NULL;
186         }
187
188         null_set_signing(si);
189 }
190
191
192 static BOOL signing_good(char *inbuf, struct smb_sign_info *si, BOOL good, uint32 seq) 
193 {
194         if (good && !si->doing_signing) {
195                 si->doing_signing = True;
196         }
197
198         if (!good) {
199                 if (si->doing_signing) {
200                         struct smb_basic_signing_context *data = si->signing_context;
201
202                         /* W2K sends a bad first signature but the sign engine is on.... JRA. */
203                         if (data->send_seq_num > 1)
204                                 DEBUG(1, ("signing_good: SMB signature check failed on seq %u!\n",
205                                                         (unsigned int)seq ));
206
207                         return False;
208                 } else {
209                         DEBUG(3, ("signing_good: Peer did not sign reply correctly\n"));
210                         free_signing_context(si);
211                         return False;
212                 }
213         }
214         return True;
215 }       
216
217 /***********************************************************
218  SMB signing - Simple implementation - calculate a MAC on the packet
219 ************************************************************/
220
221 static void simple_packet_signature(struct smb_basic_signing_context *data, 
222                                     const uchar *buf, uint32 seq_number, 
223                                     unsigned char calc_md5_mac[16])
224 {
225         const size_t offset_end_of_sig = (smb_ss_field + 8);
226         unsigned char sequence_buf[8];
227         struct MD5Context md5_ctx;
228
229         /*
230          * Firstly put the sequence number into the first 4 bytes.
231          * and zero out the next 4 bytes.
232          *
233          * We do this here, to avoid modifying the packet.
234          */
235
236         DEBUG(10,("simple_packet_signature: sequence number %u\n", seq_number ));
237
238         SIVAL(sequence_buf, 0, seq_number);
239         SIVAL(sequence_buf, 4, 0);
240
241         /* Calculate the 16 byte MAC - but don't alter the data in the
242            incoming packet.
243            
244            This makes for a bit of fussing about, but it's not too bad.
245         */
246         MD5Init(&md5_ctx);
247
248         /* intialise with the key */
249         MD5Update(&md5_ctx, data->mac_key.data, 
250                   data->mac_key.length); 
251
252         /* copy in the first bit of the SMB header */
253         MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
254
255         /* copy in the sequence number, instead of the signature */
256         MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
257
258         /* copy in the rest of the packet in, skipping the signature */
259         MD5Update(&md5_ctx, buf + offset_end_of_sig, 
260                   smb_len(buf) - (offset_end_of_sig - 4));
261
262         /* calculate the MD5 sig */ 
263         MD5Final(calc_md5_mac, &md5_ctx);
264 }
265
266
267 /***********************************************************
268  SMB signing - Client implementation - send the MAC.
269 ************************************************************/
270
271 static void client_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
272 {
273         unsigned char calc_md5_mac[16];
274         struct smb_basic_signing_context *data = si->signing_context;
275         uint32 send_seq_num;
276
277         if (!si->doing_signing)
278                 return;
279
280         /* JRA Paranioa test - we should be able to get rid of this... */
281         if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
282                 DEBUG(1, ("client_sign_outgoing_message: Logic error. Can't check signature on short packet! smb_len = %u\n",
283                                         smb_len(outbuf) ));
284                 abort();
285         }
286
287         /* mark the packet as signed - BEFORE we sign it...*/
288         mark_packet_signed(outbuf);
289
290         if (data->trans_info)
291                 send_seq_num = data->trans_info->send_seq_num;
292         else
293                 send_seq_num = data->send_seq_num;
294
295         simple_packet_signature(data, (const unsigned char *)outbuf, send_seq_num, calc_md5_mac);
296
297         DEBUG(10, ("client_sign_outgoing_message: sent SMB signature of\n"));
298         dump_data(10, (const char *)calc_md5_mac, 8);
299
300         memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
301
302 /*      cli->outbuf[smb_ss_field+2]=0; 
303         Uncomment this to test if the remote server actually verifies signatures...*/
304
305         if (data->trans_info)
306                 return;
307
308         data->send_seq_num++;
309         store_sequence_for_reply(&data->outstanding_packet_list, 
310                                  SVAL(outbuf,smb_mid), data->send_seq_num);
311         data->send_seq_num++;
312 }
313
314 /***********************************************************
315  SMB signing - Client implementation - check a MAC sent by server.
316 ************************************************************/
317
318 static BOOL client_check_incoming_message(char *inbuf, struct smb_sign_info *si)
319 {
320         BOOL good;
321         uint32 reply_seq_number;
322         uint32 saved_seq;
323         unsigned char calc_md5_mac[16];
324         unsigned char *server_sent_mac;
325
326         struct smb_basic_signing_context *data = si->signing_context;
327
328         if (!si->doing_signing)
329                 return True;
330
331         if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
332                 DEBUG(1, ("client_check_incoming_message: Can't check signature on short packet! smb_len = %u\n", smb_len(inbuf)));
333                 return False;
334         }
335
336         if (data->trans_info) {
337                 reply_seq_number = data->trans_info->reply_seq_num;
338         } else if (!get_sequence_for_reply(&data->outstanding_packet_list, 
339                                     SVAL(inbuf, smb_mid), &reply_seq_number)) {
340                 DEBUG(1, ("client_check_incoming_message: failed to get sequence number %u for reply.\n",
341                                         (unsigned int) SVAL(inbuf, smb_mid) ));
342                 return False;
343         }
344
345         saved_seq = reply_seq_number;
346         simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
347
348         server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
349         good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
350         
351         if (!good) {
352                 DEBUG(5, ("client_check_incoming_message: BAD SIG: wanted SMB signature of\n"));
353                 dump_data(5, (const char *)calc_md5_mac, 8);
354                 
355                 DEBUG(5, ("client_check_incoming_message: BAD SIG: got SMB signature of\n"));
356                 dump_data(5, (const char *)server_sent_mac, 8);
357 #if 1 /* JRATEST */
358                 {
359                         int i;
360                         reply_seq_number -= 5;
361                         for (i = 0; i < 10; i++, reply_seq_number++) {
362                                 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
363                                 if (memcmp(server_sent_mac, calc_md5_mac, 8) == 0) {
364                                         DEBUG(0,("client_check_incoming_message: out of seq. seq num %u matches. \
365 We were expecting seq %u\n", reply_seq_number, saved_seq ));
366                                         break;
367                                 }
368                         }
369                 }
370 #endif /* JRATEST */
371
372         } else {
373                 DEBUG(10, ("client_check_incoming_message:: seq %u: got good SMB signature of\n", (unsigned int)reply_seq_number));
374                 dump_data(10, (const char *)server_sent_mac, 8);
375         }
376         return signing_good(inbuf, si, good, saved_seq);
377 }
378
379 /***********************************************************
380  SMB signing - Simple implementation - free signing context
381 ************************************************************/
382
383 static void simple_free_signing_context(struct smb_sign_info *si)
384 {
385         struct smb_basic_signing_context *data = si->signing_context;
386         struct outstanding_packet_lookup *list = data->outstanding_packet_list;
387         
388         while (list) {
389                 struct outstanding_packet_lookup *old_head = list;
390                 DLIST_REMOVE(list, list);
391                 SAFE_FREE(old_head);
392         }
393
394         data_blob_free(&data->mac_key);
395
396         if (data->trans_info)
397                 SAFE_FREE(data->trans_info);
398
399         SAFE_FREE(si->signing_context);
400
401         return;
402 }
403
404 /***********************************************************
405  SMB signing - Simple implementation - setup the MAC key.
406 ************************************************************/
407
408 BOOL cli_simple_set_signing(struct cli_state *cli, const uchar user_session_key[16], const DATA_BLOB response)
409 {
410         struct smb_basic_signing_context *data;
411
412         if (!user_session_key)
413                 return False;
414
415         if (!cli_set_smb_signing_common(cli)) {
416                 return False;
417         }
418
419         if (!set_smb_signing_real_common(&cli->sign_info)) {
420                 return False;
421         }
422
423         data = smb_xmalloc(sizeof(*data));
424         memset(data, '\0', sizeof(*data));
425
426         cli->sign_info.signing_context = data;
427         
428         data->mac_key = data_blob(NULL, response.length + 16);
429
430         memcpy(&data->mac_key.data[0], user_session_key, 16);
431
432         DEBUG(10, ("cli_simple_set_signing: user_session_key\n"));
433         dump_data(10, (const char *)user_session_key, 16);
434
435         if (response.length) {
436                 memcpy(&data->mac_key.data[16],response.data, response.length);
437                 DEBUG(10, ("cli_simple_set_signing: response_data\n"));
438                 dump_data(10, (const char *)response.data, response.length);
439         } else {
440                 DEBUG(10, ("cli_simple_set_signing: NULL response_data\n"));
441         }
442
443         /* Initialise the sequence number */
444         data->send_seq_num = 0;
445
446         /* Initialise the list of outstanding packets */
447         data->outstanding_packet_list = NULL;
448
449         cli->sign_info.sign_outgoing_message = client_sign_outgoing_message;
450         cli->sign_info.check_incoming_message = client_check_incoming_message;
451         cli->sign_info.free_signing_context = simple_free_signing_context;
452
453         return True;
454 }
455
456 /***********************************************************
457  Tell client code we are in a multiple trans reply state.
458 ************************************************************/
459
460 void cli_signing_trans_start(struct cli_state *cli)
461 {
462         struct smb_basic_signing_context *data = cli->sign_info.signing_context;
463
464         if (!cli->sign_info.doing_signing || !data)
465                 return;
466
467         data->trans_info = smb_xmalloc(sizeof(struct trans_info_context));
468         ZERO_STRUCTP(data->trans_info);
469
470         data->trans_info->send_seq_num = data->send_seq_num;
471         data->trans_info->mid = SVAL(cli->outbuf,smb_mid);
472         data->trans_info->reply_seq_num = data->send_seq_num+1;
473
474         DEBUG(10,("cli_signing_trans_start: storing mid = %u, reply_seq_num = %u, send_seq_num = %u \
475 data->send_seq_num = %u\n",
476                         (unsigned int)data->trans_info->mid,
477                         (unsigned int)data->trans_info->reply_seq_num,
478                         (unsigned int)data->trans_info->send_seq_num,
479                         (unsigned int)data->send_seq_num ));
480 }
481
482 /***********************************************************
483  Tell client code we are out of a multiple trans reply state.
484 ************************************************************/
485
486 void cli_signing_trans_stop(struct cli_state *cli)
487 {
488         struct smb_basic_signing_context *data = cli->sign_info.signing_context;
489
490         if (!cli->sign_info.doing_signing || !data)
491                 return;
492
493         SAFE_FREE(data->trans_info);
494         data->trans_info = NULL;
495
496         data->send_seq_num += 2;
497 }
498
499 /***********************************************************
500  SMB signing - TEMP implementation - calculate a MAC to send.
501 ************************************************************/
502
503 static void temp_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
504 {
505         /* mark the packet as signed - BEFORE we sign it...*/
506         mark_packet_signed(outbuf);
507
508         /* I wonder what BSRSPYL stands for - but this is what MS 
509            actually sends! */
510         memcpy(&outbuf[smb_ss_field], "BSRSPYL ", 8);
511         return;
512 }
513
514 /***********************************************************
515  SMB signing - TEMP implementation - check a MAC sent by server.
516 ************************************************************/
517
518 static BOOL temp_check_incoming_message(char *inbuf, struct smb_sign_info *si)
519 {
520         return True;
521 }
522
523 /***********************************************************
524  SMB signing - TEMP implementation - free signing context
525 ************************************************************/
526
527 static void temp_free_signing_context(struct smb_sign_info *si)
528 {
529         return;
530 }
531
532 /***********************************************************
533  SMB signing - NULL implementation - setup the MAC key.
534 ************************************************************/
535
536 BOOL cli_null_set_signing(struct cli_state *cli)
537 {
538         return null_set_signing(&cli->sign_info);
539 }
540
541 /***********************************************************
542  SMB signing - temp implementation - setup the MAC key.
543 ************************************************************/
544
545 BOOL cli_temp_set_signing(struct cli_state *cli)
546 {
547         if (!cli_set_smb_signing_common(cli)) {
548                 return False;
549         }
550
551         cli->sign_info.signing_context = NULL;
552         
553         cli->sign_info.sign_outgoing_message = temp_sign_outgoing_message;
554         cli->sign_info.check_incoming_message = temp_check_incoming_message;
555         cli->sign_info.free_signing_context = temp_free_signing_context;
556
557         return True;
558 }
559
560 void cli_free_signing_context(struct cli_state *cli)
561 {
562         free_signing_context(&cli->sign_info);
563 }
564
565 /**
566  * Sign a packet with the current mechanism
567  */
568  
569 void cli_calculate_sign_mac(struct cli_state *cli)
570 {
571         cli->sign_info.sign_outgoing_message(cli->outbuf, &cli->sign_info);
572 }
573
574 /**
575  * Check a packet with the current mechanism
576  * @return False if we had an established signing connection
577  *         which had a bad checksum, True otherwise.
578  */
579  
580 BOOL cli_check_sign_mac(struct cli_state *cli) 
581 {
582         if (!cli->sign_info.check_incoming_message(cli->inbuf, &cli->sign_info)) {
583                 free_signing_context(&cli->sign_info);  
584                 return False;
585         }
586         return True;
587 }
588
589 /***********************************************************
590  SMB signing - Server implementation - send the MAC.
591 ************************************************************/
592
593 static void srv_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
594 {
595         unsigned char calc_md5_mac[16];
596         struct smb_basic_signing_context *data = si->signing_context;
597         uint32 send_seq_number = data->send_seq_num;
598         BOOL was_deferred_packet = False;
599         uint16 mid;
600
601         if (!si->doing_signing) {
602                 return;
603         }
604
605         /* JRA Paranioa test - we should be able to get rid of this... */
606         if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
607                 DEBUG(1, ("srv_sign_outgoing_message: Logic error. Can't send signature on short packet! smb_len = %u\n",
608                                         smb_len(outbuf) ));
609                 abort();
610         }
611
612         /* mark the packet as signed - BEFORE we sign it...*/
613         mark_packet_signed(outbuf);
614
615         mid = SVAL(outbuf, smb_mid);
616
617         /* See if this is a reply for a deferred packet. */
618         was_deferred_packet = get_sequence_for_reply(&data->outstanding_packet_list, mid, &send_seq_number);
619
620         if (data->trans_info && (data->trans_info->mid == mid)) {
621                 /* This is a reply in a trans stream. Use the sequence
622                  * number associated with the stream mid. */
623                 send_seq_number = data->trans_info->send_seq_num;
624         }
625
626         simple_packet_signature(data, (const unsigned char *)outbuf, send_seq_number, calc_md5_mac);
627
628         DEBUG(10, ("srv_sign_outgoing_message: seq %u: sent SMB signature of\n", (unsigned int)send_seq_number));
629         dump_data(10, (const char *)calc_md5_mac, 8);
630
631         memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
632
633 /*      cli->outbuf[smb_ss_field+2]=0; 
634         Uncomment this to test if the remote client actually verifies signatures...*/
635
636         /* Don't mess with the sequence number for a deferred packet. */
637         if (was_deferred_packet) {
638                 return;
639         }
640
641         if (!data->trans_info) {
642                 /* Always increment if not in a trans stream. */
643                 data->send_seq_num++;
644         } else if ((data->trans_info->send_seq_num == data->send_seq_num) || (data->trans_info->mid != mid)) {
645                 /* Increment if this is the first reply in a trans stream or a
646                  * packet that doesn't belong to this stream (different mid). */
647                 data->send_seq_num++;
648         }
649 }
650
651 /***********************************************************
652  Is an incoming packet an oplock break reply ?
653 ************************************************************/
654
655 static BOOL is_oplock_break(char *inbuf)
656 {
657         if (CVAL(inbuf,smb_com) != SMBlockingX)
658                 return False;
659
660         if (!(CVAL(inbuf,smb_vwv3) & LOCKING_ANDX_OPLOCK_RELEASE))
661                 return False;
662
663         DEBUG(10,("is_oplock_break: Packet is oplock break\n"));
664         return True;
665 }
666
667 /***********************************************************
668  SMB signing - Server implementation - check a MAC sent by server.
669 ************************************************************/
670
671 static BOOL srv_check_incoming_message(char *inbuf, struct smb_sign_info *si)
672 {
673         BOOL good;
674         struct smb_basic_signing_context *data = si->signing_context;
675         uint32 reply_seq_number = data->send_seq_num;
676         uint32 saved_seq;
677         unsigned char calc_md5_mac[16];
678         unsigned char *server_sent_mac;
679         uint mid;
680
681         if (!si->doing_signing)
682                 return True;
683
684         if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
685                 DEBUG(1, ("srv_check_incoming_message: Can't check signature on short packet! smb_len = %u\n", smb_len(inbuf)));
686                 return False;
687         }
688
689         mid = SVAL(inbuf, smb_mid);
690
691         /* Is this part of a trans stream ? */
692         if (data->trans_info && (data->trans_info->mid == mid)) {
693                 /* If so we don't increment the sequence. */
694                 reply_seq_number = data->trans_info->reply_seq_num;
695         } else {
696                 /* We always increment the sequence number. */
697                 data->send_seq_num++;
698
699                 /* If we get an asynchronous oplock break reply and there
700                  * isn't a reply pending we need to re-sync the sequence
701                  * number.
702                  */
703                 if (is_oplock_break(inbuf)) {
704                         DEBUG(10,("srv_check_incoming_message: oplock break at seq num %u\n", data->send_seq_num));
705                         data->send_seq_num++;
706                 }
707         }
708
709         saved_seq = reply_seq_number;
710         simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
711
712         server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
713         good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
714         
715         if (!good) {
716
717                 DEBUG(5, ("srv_check_incoming_message: BAD SIG: seq %u wanted SMB signature of\n",
718                                         (unsigned int)saved_seq));
719                 dump_data(5, (const char *)calc_md5_mac, 8);
720                 
721                 DEBUG(5, ("srv_check_incoming_message: BAD SIG: seq %u got SMB signature of\n",
722                                         (unsigned int)saved_seq));
723                 dump_data(5, (const char *)server_sent_mac, 8);
724
725 #if 1 /* JRATEST */
726                 {
727                         int i;
728                         reply_seq_number -= 5;
729                         for (i = 0; i < 10; i++, reply_seq_number++) {
730                                 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
731                                 if (memcmp(server_sent_mac, calc_md5_mac, 8) == 0) {
732                                         DEBUG(0,("srv_check_incoming_message: out of seq. seq num %u matches. \
733 We were expecting seq %u\n", reply_seq_number, saved_seq ));
734                                         break;
735                                 }
736                         }
737                 }
738 #endif /* JRATEST */
739
740         } else {
741                 DEBUG(10, ("srv_check_incoming_message: seq %u: (current is %u) got good SMB signature of\n", (unsigned int)reply_seq_number, (unsigned int)data->send_seq_num));
742                 dump_data(10, (const char *)server_sent_mac, 8);
743         }
744         return signing_good(inbuf, si, good, saved_seq);
745 }
746
747 /***********************************************************
748  SMB signing - server API's.
749 ************************************************************/
750
751 static struct smb_sign_info srv_sign_info = {
752         null_sign_outgoing_message,
753         null_check_incoming_message,
754         null_free_signing_context,
755         NULL,
756         False,
757         False,
758         False,
759         False
760 };
761
762 /***********************************************************
763  Turn signing off or on for oplock break code.
764 ************************************************************/
765
766 BOOL srv_oplock_set_signing(BOOL onoff)
767 {
768         BOOL ret = srv_sign_info.doing_signing;
769         srv_sign_info.doing_signing = onoff;
770         return ret;
771 }
772
773 /***********************************************************
774  Called to validate an incoming packet from the client.
775 ************************************************************/
776
777 BOOL srv_check_sign_mac(char *inbuf)
778 {
779         /* Check if it's a session keepalive. */
780         if(CVAL(inbuf,0) == SMBkeepalive)
781                 return True;
782
783         return srv_sign_info.check_incoming_message(inbuf, &srv_sign_info);
784 }
785
786 /***********************************************************
787  Called to sign an outgoing packet to the client.
788 ************************************************************/
789
790 void srv_calculate_sign_mac(char *outbuf)
791 {
792         /* Check if it's a session keepalive. */
793         /* JRA Paranioa test - do we ever generate these in the server ? */
794         if(CVAL(outbuf,0) == SMBkeepalive)
795                 return;
796
797         srv_sign_info.sign_outgoing_message(outbuf, &srv_sign_info);
798 }
799
800 /***********************************************************
801  Called by server to defer an outgoing packet.
802 ************************************************************/
803
804 void srv_defer_sign_response(uint16 mid)
805 {
806         struct smb_basic_signing_context *data;
807
808         if (!srv_sign_info.doing_signing)
809                 return;
810
811         data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
812
813         if (!data)
814                 return;
815
816         store_sequence_for_reply(&data->outstanding_packet_list, 
817                                  mid, data->send_seq_num);
818         data->send_seq_num++;
819 }
820
821 /***********************************************************
822  Called to remove sequence records when a deferred packet is
823  cancelled by mid. This should never find one....
824 ************************************************************/
825
826 void srv_cancel_sign_response(uint16 mid)
827 {
828         struct smb_basic_signing_context *data;
829         uint32 dummy_seq;
830
831         if (!srv_sign_info.doing_signing)
832                 return;
833
834         data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
835
836         if (!data)
837                 return;
838
839         DEBUG(10,("srv_cancel_sign_response: for mid %u\n", (unsigned int)mid ));
840
841         while (get_sequence_for_reply(&data->outstanding_packet_list, mid, &dummy_seq))
842                 ;
843 }
844
845 /***********************************************************
846  Called by server negprot when signing has been negotiated.
847 ************************************************************/
848
849 void srv_set_signing_negotiated(void)
850 {
851         srv_sign_info.allow_smb_signing = True;
852         srv_sign_info.negotiated_smb_signing = True;
853         if (lp_server_signing() == Required)
854                 srv_sign_info.mandatory_signing = True;
855
856         srv_sign_info.sign_outgoing_message = temp_sign_outgoing_message;
857         srv_sign_info.check_incoming_message = temp_check_incoming_message;
858         srv_sign_info.free_signing_context = temp_free_signing_context;
859 }
860
861 /***********************************************************
862  Returns whether signing is active. We can't use sendfile or raw
863  reads/writes if it is.
864 ************************************************************/
865
866 BOOL srv_is_signing_active(void)
867 {
868         return srv_sign_info.doing_signing;
869 }
870
871 /***********************************************************
872  Tell server code we are in a multiple trans reply state.
873 ************************************************************/
874
875 void srv_signing_trans_start(uint16 mid)
876 {
877         struct smb_basic_signing_context *data;
878
879         if (!srv_sign_info.doing_signing)
880                 return;
881
882         data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
883         if (!data)
884                 return;
885
886         data->trans_info = smb_xmalloc(sizeof(struct trans_info_context));
887         ZERO_STRUCTP(data->trans_info);
888
889         data->trans_info->reply_seq_num = data->send_seq_num-1;
890         data->trans_info->mid = mid;
891         data->trans_info->send_seq_num = data->send_seq_num;
892
893         DEBUG(10,("srv_signing_trans_start: storing mid = %u, reply_seq_num = %u, send_seq_num = %u \
894 data->send_seq_num = %u\n",
895                         (unsigned int)mid,
896                         (unsigned int)data->trans_info->reply_seq_num,
897                         (unsigned int)data->trans_info->send_seq_num,
898                         (unsigned int)data->send_seq_num ));
899 }
900
901 /***********************************************************
902  Tell server code we are out of a multiple trans reply state.
903 ************************************************************/
904
905 void srv_signing_trans_stop(void)
906 {
907         struct smb_basic_signing_context *data;
908
909         if (!srv_sign_info.doing_signing)
910                 return;
911
912         data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
913         if (!data || !data->trans_info)
914                 return;
915
916         DEBUG(10,("srv_signing_trans_stop: removing mid = %u, reply_seq_num = %u, send_seq_num = %u \
917 data->send_seq_num = %u\n",
918                         (unsigned int)data->trans_info->mid,
919                         (unsigned int)data->trans_info->reply_seq_num,
920                         (unsigned int)data->trans_info->send_seq_num,
921                         (unsigned int)data->send_seq_num ));
922
923         SAFE_FREE(data->trans_info);
924         data->trans_info = NULL;
925 }
926
927 /***********************************************************
928  Turn on signing from this packet onwards. 
929 ************************************************************/
930
931 void srv_set_signing(const uchar user_session_key[16], const DATA_BLOB response)
932 {
933         struct smb_basic_signing_context *data;
934
935         if (!user_session_key)
936                 return;
937
938         if (!srv_sign_info.negotiated_smb_signing && !srv_sign_info.mandatory_signing) {
939                 DEBUG(5,("srv_set_signing: signing negotiated = %u, mandatory_signing = %u. Not allowing smb signing.\n",
940                         (unsigned int)srv_sign_info.negotiated_smb_signing,
941                         (unsigned int)srv_sign_info.mandatory_signing ));
942                 return;
943         }
944
945         /* Once we've turned on, ignore any more sessionsetups. */
946         if (srv_sign_info.doing_signing) {
947                 return;
948         }
949         
950         if (srv_sign_info.free_signing_context)
951                 srv_sign_info.free_signing_context(&srv_sign_info);
952         
953         srv_sign_info.doing_signing = True;
954
955         data = smb_xmalloc(sizeof(*data));
956         memset(data, '\0', sizeof(*data));
957
958         srv_sign_info.signing_context = data;
959         
960         data->mac_key = data_blob(NULL, response.length + 16);
961
962         memcpy(&data->mac_key.data[0], user_session_key, 16);
963         if (response.length)
964                 memcpy(&data->mac_key.data[16],response.data, response.length);
965
966         /* Initialise the sequence number */
967         data->send_seq_num = 0;
968
969         /* Initialise the list of outstanding packets */
970         data->outstanding_packet_list = NULL;
971
972         srv_sign_info.sign_outgoing_message = srv_sign_outgoing_message;
973         srv_sign_info.check_incoming_message = srv_check_incoming_message;
974         srv_sign_info.free_signing_context = simple_free_signing_context;
975 }