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