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