Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / libsmb / smb_signing.c
blobe59254b16f3d49f69256a7a278a3688f01413ba5
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
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.
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.
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.
22 #include "includes.h"
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 BOOL can_delete; /* Set to False in trans state. */
32 struct smb_basic_signing_context {
33 DATA_BLOB mac_key;
34 uint32 send_seq_num;
35 struct outstanding_packet_lookup *outstanding_packet_list;
38 static BOOL store_sequence_for_reply(struct outstanding_packet_lookup **list,
39 uint16 mid, uint32 reply_seq_num)
41 struct outstanding_packet_lookup *t;
43 /* Ensure we only add a mid once. */
44 for (t = *list; t; t = t->next) {
45 if (t->mid == mid) {
46 return False;
50 t = SMB_XMALLOC_P(struct outstanding_packet_lookup);
51 ZERO_STRUCTP(t);
53 t->mid = mid;
54 t->reply_seq_num = reply_seq_num;
55 t->can_delete = True;
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>.
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;
72 static BOOL get_sequence_for_reply(struct outstanding_packet_lookup **list,
73 uint16 mid, uint32 *reply_seq_num)
75 struct outstanding_packet_lookup *t;
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 if (t->can_delete) {
83 DLIST_REMOVE(*list, t);
84 SAFE_FREE(t);
86 return True;
89 return False;
92 static BOOL set_sequence_can_delete_flag(struct outstanding_packet_lookup **list, uint16 mid, BOOL can_delete_entry)
94 struct outstanding_packet_lookup *t;
96 for (t = *list; t; t = t->next) {
97 if (t->mid == mid) {
98 t->can_delete = can_delete_entry;
99 return True;
102 return False;
105 /***********************************************************
106 SMB signing - Common code before we set a new signing implementation
107 ************************************************************/
109 static BOOL cli_set_smb_signing_common(struct cli_state *cli)
111 if (!cli->sign_info.allow_smb_signing) {
112 return False;
115 if (!cli->sign_info.negotiated_smb_signing
116 && !cli->sign_info.mandatory_signing) {
117 return False;
120 if (cli->sign_info.doing_signing) {
121 return False;
124 if (cli->sign_info.free_signing_context)
125 cli->sign_info.free_signing_context(&cli->sign_info);
127 /* These calls are INCOMPATIBLE with SMB signing */
128 cli->readbraw_supported = False;
129 cli->writebraw_supported = False;
131 return True;
134 /***********************************************************
135 SMB signing - Common code for 'real' implementations
136 ************************************************************/
138 static BOOL set_smb_signing_real_common(struct smb_sign_info *si)
140 if (si->mandatory_signing) {
141 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
144 si->doing_signing = True;
145 DEBUG(5, ("SMB signing enabled!\n"));
147 return True;
150 static void mark_packet_signed(char *outbuf)
152 uint16 flags2;
153 flags2 = SVAL(outbuf,smb_flg2);
154 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
155 SSVAL(outbuf,smb_flg2, flags2);
158 /***********************************************************
159 SMB signing - NULL implementation - calculate a MAC to send.
160 ************************************************************/
162 static void null_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
164 /* we can't zero out the sig, as we might be trying to send a
165 session request - which is NBT-level, not SMB level and doesn't
166 have the field */
167 return;
170 /***********************************************************
171 SMB signing - NULL implementation - check a MAC sent by server.
172 ************************************************************/
174 static BOOL null_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
176 return True;
179 /***********************************************************
180 SMB signing - NULL implementation - free signing context
181 ************************************************************/
183 static void null_free_signing_context(struct smb_sign_info *si)
185 return;
189 SMB signing - NULL implementation - setup the MAC key.
191 @note Used as an initialisation only - it will not correctly
192 shut down a real signing mechanism
195 static BOOL null_set_signing(struct smb_sign_info *si)
197 si->signing_context = NULL;
199 si->sign_outgoing_message = null_sign_outgoing_message;
200 si->check_incoming_message = null_check_incoming_message;
201 si->free_signing_context = null_free_signing_context;
203 return True;
207 * Free the signing context
210 static void free_signing_context(struct smb_sign_info *si)
212 if (si->free_signing_context) {
213 si->free_signing_context(si);
214 si->signing_context = NULL;
217 null_set_signing(si);
221 static BOOL signing_good(char *inbuf, struct smb_sign_info *si, BOOL good, uint32 seq, BOOL must_be_ok)
223 if (good) {
225 if (!si->doing_signing) {
226 si->doing_signing = True;
229 if (!si->seen_valid) {
230 si->seen_valid = True;
233 } else {
234 if (!si->mandatory_signing && !si->seen_valid) {
236 if (!must_be_ok) {
237 return True;
239 /* Non-mandatory signing - just turn off if this is the first bad packet.. */
240 DEBUG(5, ("srv_check_incoming_message: signing negotiated but not required and peer\n"
241 "isn't sending correct signatures. Turning off.\n"));
242 si->negotiated_smb_signing = False;
243 si->allow_smb_signing = False;
244 si->doing_signing = False;
245 free_signing_context(si);
246 return True;
247 } else if (!must_be_ok) {
248 /* This packet is known to be unsigned */
249 return True;
250 } else {
251 /* Mandatory signing or bad packet after signing started - fail and disconnect. */
252 if (seq)
253 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", (unsigned int)seq));
254 return False;
257 return True;
260 /***********************************************************
261 SMB signing - Simple implementation - calculate a MAC on the packet
262 ************************************************************/
264 static void simple_packet_signature(struct smb_basic_signing_context *data,
265 const uchar *buf, uint32 seq_number,
266 unsigned char calc_md5_mac[16])
268 const size_t offset_end_of_sig = (smb_ss_field + 8);
269 unsigned char sequence_buf[8];
270 struct MD5Context md5_ctx;
271 #if 0
272 /* JRA - apparently this is incorrect. */
273 unsigned char key_buf[16];
274 #endif
277 * Firstly put the sequence number into the first 4 bytes.
278 * and zero out the next 4 bytes.
280 * We do this here, to avoid modifying the packet.
283 DEBUG(10,("simple_packet_signature: sequence number %u\n", seq_number ));
285 SIVAL(sequence_buf, 0, seq_number);
286 SIVAL(sequence_buf, 4, 0);
288 /* Calculate the 16 byte MAC - but don't alter the data in the
289 incoming packet.
291 This makes for a bit of fussing about, but it's not too bad.
293 MD5Init(&md5_ctx);
295 /* intialise with the key */
296 MD5Update(&md5_ctx, data->mac_key.data, data->mac_key.length);
297 #if 0
298 /* JRA - apparently this is incorrect. */
299 /* NB. When making and verifying SMB signatures, Windows apparently
300 zero-pads the key to 128 bits if it isn't long enough.
301 From Nalin Dahyabhai <nalin@redhat.com> */
302 if (data->mac_key.length < sizeof(key_buf)) {
303 memset(key_buf, 0, sizeof(key_buf));
304 MD5Update(&md5_ctx, key_buf, sizeof(key_buf) - data->mac_key.length);
306 #endif
308 /* copy in the first bit of the SMB header */
309 MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
311 /* copy in the sequence number, instead of the signature */
312 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
314 /* copy in the rest of the packet in, skipping the signature */
315 MD5Update(&md5_ctx, buf + offset_end_of_sig,
316 smb_len(buf) - (offset_end_of_sig - 4));
318 /* calculate the MD5 sig */
319 MD5Final(calc_md5_mac, &md5_ctx);
323 /***********************************************************
324 SMB signing - Client implementation - send the MAC.
325 ************************************************************/
327 static void client_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
329 unsigned char calc_md5_mac[16];
330 struct smb_basic_signing_context *data = si->signing_context;
332 if (!si->doing_signing)
333 return;
335 /* JRA Paranioa test - we should be able to get rid of this... */
336 if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
337 DEBUG(1, ("client_sign_outgoing_message: Logic error. Can't check signature on short packet! smb_len = %u\n",
338 smb_len(outbuf) ));
339 abort();
342 /* mark the packet as signed - BEFORE we sign it...*/
343 mark_packet_signed(outbuf);
345 simple_packet_signature(data, (const unsigned char *)outbuf,
346 data->send_seq_num, calc_md5_mac);
348 DEBUG(10, ("client_sign_outgoing_message: sent SMB signature of\n"));
349 dump_data(10, (const char *)calc_md5_mac, 8);
351 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
353 /* cli->outbuf[smb_ss_field+2]=0;
354 Uncomment this to test if the remote server actually verifies signatures...*/
356 /* Instead of re-introducing the trans_info_conect we
357 used to have here, we use the fact that during a
358 SMBtrans/SMBtrans2/SMBnttrans send that the mid stays
359 constant. This means that calling store_sequence_for_reply()
360 will return False for all trans secondaries, as the mid is already
361 on the stored sequence list. As the send_seqence_number must
362 remain constant for all primary+secondary trans sends, we
363 only increment the send sequence number when we successfully
364 add a new entry to the outstanding sequence list. This means
365 I can isolate the fix here rather than re-adding the trans
366 signing on/off calls in libsmb/clitrans2.c JRA.
369 if (store_sequence_for_reply(&data->outstanding_packet_list, SVAL(outbuf,smb_mid), data->send_seq_num + 1)) {
370 data->send_seq_num += 2;
374 /***********************************************************
375 SMB signing - Client implementation - check a MAC sent by server.
376 ************************************************************/
378 static BOOL client_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
380 BOOL good;
381 uint32 reply_seq_number;
382 unsigned char calc_md5_mac[16];
383 unsigned char *server_sent_mac;
385 struct smb_basic_signing_context *data = si->signing_context;
387 if (!si->doing_signing)
388 return True;
390 if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
391 DEBUG(1, ("client_check_incoming_message: Can't check signature on short packet! smb_len = %u\n", smb_len(inbuf)));
392 return False;
395 if (!get_sequence_for_reply(&data->outstanding_packet_list, SVAL(inbuf, smb_mid), &reply_seq_number)) {
396 DEBUG(1, ("client_check_incoming_message: received message "
397 "with mid %u with no matching send record.\n", (unsigned int)SVAL(inbuf, smb_mid) ));
398 return False;
401 simple_packet_signature(data, (const unsigned char *)inbuf,
402 reply_seq_number, calc_md5_mac);
404 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
405 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
407 if (!good) {
408 DEBUG(5, ("client_check_incoming_message: BAD SIG: wanted SMB signature of\n"));
409 dump_data(5, (const char *)calc_md5_mac, 8);
411 DEBUG(5, ("client_check_incoming_message: BAD SIG: got SMB signature of\n"));
412 dump_data(5, (const char *)server_sent_mac, 8);
413 #if 1 /* JRATEST */
415 int i;
416 for (i = -5; i < 5; i++) {
417 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number+i, calc_md5_mac);
418 if (memcmp(server_sent_mac, calc_md5_mac, 8) == 0) {
419 DEBUG(0,("client_check_incoming_message: out of seq. seq num %u matches. \
420 We were expecting seq %u\n", reply_seq_number+i, reply_seq_number ));
421 break;
425 #endif /* JRATEST */
427 } else {
428 DEBUG(10, ("client_check_incoming_message: seq %u: got good SMB signature of\n", (unsigned int)reply_seq_number));
429 dump_data(10, (const char *)server_sent_mac, 8);
431 return signing_good(inbuf, si, good, reply_seq_number, must_be_ok);
434 /***********************************************************
435 SMB signing - Simple implementation - free signing context
436 ************************************************************/
438 static void simple_free_signing_context(struct smb_sign_info *si)
440 struct smb_basic_signing_context *data = si->signing_context;
441 struct outstanding_packet_lookup *list;
442 struct outstanding_packet_lookup *next;
444 for (list = data->outstanding_packet_list; list; list = next) {
445 next = list->next;
446 DLIST_REMOVE(data->outstanding_packet_list, list);
447 SAFE_FREE(list);
450 data_blob_free(&data->mac_key);
452 SAFE_FREE(si->signing_context);
454 return;
457 /***********************************************************
458 SMB signing - Simple implementation - setup the MAC key.
459 ************************************************************/
461 BOOL cli_simple_set_signing(struct cli_state *cli,
462 const DATA_BLOB user_session_key,
463 const DATA_BLOB response)
465 struct smb_basic_signing_context *data;
467 if (!user_session_key.length)
468 return False;
470 if (!cli_set_smb_signing_common(cli)) {
471 return False;
474 if (!set_smb_signing_real_common(&cli->sign_info)) {
475 return False;
478 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
479 memset(data, '\0', sizeof(*data));
481 cli->sign_info.signing_context = data;
483 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
485 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
487 DEBUG(10, ("cli_simple_set_signing: user_session_key\n"));
488 dump_data(10, (const char *)user_session_key.data, user_session_key.length);
490 if (response.length) {
491 memcpy(&data->mac_key.data[user_session_key.length],response.data, response.length);
492 DEBUG(10, ("cli_simple_set_signing: response_data\n"));
493 dump_data(10, (const char *)response.data, response.length);
494 } else {
495 DEBUG(10, ("cli_simple_set_signing: NULL response_data\n"));
498 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
500 /* Initialise the sequence number */
501 data->send_seq_num = 0;
503 /* Initialise the list of outstanding packets */
504 data->outstanding_packet_list = NULL;
506 cli->sign_info.sign_outgoing_message = client_sign_outgoing_message;
507 cli->sign_info.check_incoming_message = client_check_incoming_message;
508 cli->sign_info.free_signing_context = simple_free_signing_context;
510 return True;
513 /***********************************************************
514 SMB signing - TEMP implementation - calculate a MAC to send.
515 ************************************************************/
517 static void temp_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
519 /* mark the packet as signed - BEFORE we sign it...*/
520 mark_packet_signed(outbuf);
522 /* I wonder what BSRSPYL stands for - but this is what MS
523 actually sends! */
524 memcpy(&outbuf[smb_ss_field], "BSRSPYL ", 8);
525 return;
528 /***********************************************************
529 SMB signing - TEMP implementation - check a MAC sent by server.
530 ************************************************************/
532 static BOOL temp_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL foo)
534 return True;
537 /***********************************************************
538 SMB signing - TEMP implementation - free signing context
539 ************************************************************/
541 static void temp_free_signing_context(struct smb_sign_info *si)
543 return;
546 /***********************************************************
547 SMB signing - NULL implementation - setup the MAC key.
548 ************************************************************/
550 BOOL cli_null_set_signing(struct cli_state *cli)
552 return null_set_signing(&cli->sign_info);
555 /***********************************************************
556 SMB signing - temp implementation - setup the MAC key.
557 ************************************************************/
559 BOOL cli_temp_set_signing(struct cli_state *cli)
561 if (!cli_set_smb_signing_common(cli)) {
562 return False;
565 cli->sign_info.signing_context = NULL;
567 cli->sign_info.sign_outgoing_message = temp_sign_outgoing_message;
568 cli->sign_info.check_incoming_message = temp_check_incoming_message;
569 cli->sign_info.free_signing_context = temp_free_signing_context;
571 return True;
574 void cli_free_signing_context(struct cli_state *cli)
576 free_signing_context(&cli->sign_info);
580 * Sign a packet with the current mechanism
583 void cli_calculate_sign_mac(struct cli_state *cli)
585 cli->sign_info.sign_outgoing_message(cli->outbuf, &cli->sign_info);
589 * Check a packet with the current mechanism
590 * @return False if we had an established signing connection
591 * which had a bad checksum, True otherwise.
594 BOOL cli_check_sign_mac(struct cli_state *cli)
596 if (!cli->sign_info.check_incoming_message(cli->inbuf, &cli->sign_info, True)) {
597 free_signing_context(&cli->sign_info);
598 return False;
600 return True;
603 /***********************************************************
604 Enter trans/trans2/nttrans state.
605 ************************************************************/
607 BOOL client_set_trans_sign_state_on(struct cli_state *cli, uint16 mid)
609 struct smb_sign_info *si = &cli->sign_info;
610 struct smb_basic_signing_context *data = (struct smb_basic_signing_context *)si->signing_context;
612 if (!si->doing_signing) {
613 return True;
616 if (!data) {
617 return False;
620 if (!set_sequence_can_delete_flag(&data->outstanding_packet_list, mid, False)) {
621 return False;
624 return True;
627 /***********************************************************
628 Leave trans/trans2/nttrans state.
629 ************************************************************/
631 BOOL client_set_trans_sign_state_off(struct cli_state *cli, uint16 mid)
633 uint32 reply_seq_num;
634 struct smb_sign_info *si = &cli->sign_info;
635 struct smb_basic_signing_context *data = (struct smb_basic_signing_context *)si->signing_context;
637 if (!si->doing_signing) {
638 return True;
641 if (!data) {
642 return False;
645 if (!set_sequence_can_delete_flag(&data->outstanding_packet_list, mid, True)) {
646 return False;
649 /* Now delete the stored mid entry. */
650 if (!get_sequence_for_reply(&data->outstanding_packet_list, mid, &reply_seq_num)) {
651 return False;
654 return True;
657 /***********************************************************
658 SMB signing - Server implementation - send the MAC.
659 ************************************************************/
661 static void srv_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
663 unsigned char calc_md5_mac[16];
664 struct smb_basic_signing_context *data = si->signing_context;
665 uint32 send_seq_number = data->send_seq_num-1;
666 uint16 mid;
668 if (!si->doing_signing) {
669 return;
672 /* JRA Paranioa test - we should be able to get rid of this... */
673 if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
674 DEBUG(1, ("srv_sign_outgoing_message: Logic error. Can't send signature on short packet! smb_len = %u\n",
675 smb_len(outbuf) ));
676 abort();
679 /* mark the packet as signed - BEFORE we sign it...*/
680 mark_packet_signed(outbuf);
682 mid = SVAL(outbuf, smb_mid);
684 /* See if this is a reply for a deferred packet. */
685 get_sequence_for_reply(&data->outstanding_packet_list, mid, &send_seq_number);
687 simple_packet_signature(data, (const unsigned char *)outbuf, send_seq_number, calc_md5_mac);
689 DEBUG(10, ("srv_sign_outgoing_message: seq %u: sent SMB signature of\n", (unsigned int)send_seq_number));
690 dump_data(10, (const char *)calc_md5_mac, 8);
692 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
694 /* cli->outbuf[smb_ss_field+2]=0;
695 Uncomment this to test if the remote client actually verifies signatures...*/
698 /***********************************************************
699 SMB signing - Server implementation - check a MAC sent by server.
700 ************************************************************/
702 static BOOL srv_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
704 BOOL good;
705 struct smb_basic_signing_context *data = si->signing_context;
706 uint32 reply_seq_number = data->send_seq_num;
707 uint32 saved_seq;
708 unsigned char calc_md5_mac[16];
709 unsigned char *server_sent_mac;
711 if (!si->doing_signing)
712 return True;
714 if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
715 DEBUG(1, ("srv_check_incoming_message: Can't check signature on short packet! smb_len = %u\n", smb_len(inbuf)));
716 return False;
719 /* We always increment the sequence number. */
720 data->send_seq_num += 2;
722 saved_seq = reply_seq_number;
723 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
725 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
726 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
728 if (!good) {
730 if (saved_seq) {
731 DEBUG(0, ("srv_check_incoming_message: BAD SIG: seq %u wanted SMB signature of\n",
732 (unsigned int)saved_seq));
733 dump_data(5, (const char *)calc_md5_mac, 8);
735 DEBUG(0, ("srv_check_incoming_message: BAD SIG: seq %u got SMB signature of\n",
736 (unsigned int)reply_seq_number));
737 dump_data(5, (const char *)server_sent_mac, 8);
740 #if 1 /* JRATEST */
742 int i;
743 reply_seq_number -= 5;
744 for (i = 0; i < 10; i++, reply_seq_number++) {
745 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
746 if (memcmp(server_sent_mac, calc_md5_mac, 8) == 0) {
747 DEBUG(0,("srv_check_incoming_message: out of seq. seq num %u matches. \
748 We were expecting seq %u\n", reply_seq_number, saved_seq ));
749 break;
753 #endif /* JRATEST */
755 } else {
756 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));
757 dump_data(10, (const char *)server_sent_mac, 8);
760 return (signing_good(inbuf, si, good, saved_seq, must_be_ok));
763 /***********************************************************
764 SMB signing - server API's.
765 ************************************************************/
767 static struct smb_sign_info srv_sign_info = {
768 null_sign_outgoing_message,
769 null_check_incoming_message,
770 null_free_signing_context,
771 NULL,
772 False,
773 False,
774 False,
775 False
778 /***********************************************************
779 Turn signing off or on for oplock break code.
780 ************************************************************/
782 BOOL srv_oplock_set_signing(BOOL onoff)
784 BOOL ret = srv_sign_info.doing_signing;
785 srv_sign_info.doing_signing = onoff;
786 return ret;
789 /***********************************************************
790 Called to validate an incoming packet from the client.
791 ************************************************************/
793 BOOL srv_check_sign_mac(char *inbuf, BOOL must_be_ok)
795 /* Check if it's a session keepalive. */
796 if(CVAL(inbuf,0) == SMBkeepalive)
797 return True;
799 return srv_sign_info.check_incoming_message(inbuf, &srv_sign_info, must_be_ok);
802 /***********************************************************
803 Called to sign an outgoing packet to the client.
804 ************************************************************/
806 void srv_calculate_sign_mac(char *outbuf)
808 /* Check if it's a session keepalive. */
809 /* JRA Paranioa test - do we ever generate these in the server ? */
810 if(CVAL(outbuf,0) == SMBkeepalive)
811 return;
813 srv_sign_info.sign_outgoing_message(outbuf, &srv_sign_info);
816 /***********************************************************
817 Called by server to defer an outgoing packet.
818 ************************************************************/
820 void srv_defer_sign_response(uint16 mid)
822 struct smb_basic_signing_context *data;
824 if (!srv_sign_info.doing_signing)
825 return;
827 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
829 if (!data)
830 return;
833 * Ensure we only store this mid reply once...
836 store_sequence_for_reply(&data->outstanding_packet_list, mid,
837 data->send_seq_num-1);
840 /***********************************************************
841 Called to remove sequence records when a deferred packet is
842 cancelled by mid. This should never find one....
843 ************************************************************/
845 void srv_cancel_sign_response(uint16 mid)
847 struct smb_basic_signing_context *data;
848 uint32 dummy_seq;
850 if (!srv_sign_info.doing_signing)
851 return;
853 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
855 if (!data)
856 return;
858 DEBUG(10,("srv_cancel_sign_response: for mid %u\n", (unsigned int)mid ));
860 while (get_sequence_for_reply(&data->outstanding_packet_list, mid, &dummy_seq))
863 /* cancel doesn't send a reply so doesn't burn a sequence number. */
864 data->send_seq_num -= 1;
867 /***********************************************************
868 Called by server negprot when signing has been negotiated.
869 ************************************************************/
871 void srv_set_signing_negotiated(void)
873 srv_sign_info.allow_smb_signing = True;
874 srv_sign_info.negotiated_smb_signing = True;
875 if (lp_server_signing() == Required)
876 srv_sign_info.mandatory_signing = True;
878 srv_sign_info.sign_outgoing_message = temp_sign_outgoing_message;
879 srv_sign_info.check_incoming_message = temp_check_incoming_message;
880 srv_sign_info.free_signing_context = temp_free_signing_context;
883 /***********************************************************
884 Returns whether signing is active. We can't use sendfile or raw
885 reads/writes if it is.
886 ************************************************************/
888 BOOL srv_is_signing_active(void)
890 return srv_sign_info.doing_signing;
894 /***********************************************************
895 Returns whether signing is negotiated. We can't use it unless it was
896 in the negprot.
897 ************************************************************/
899 BOOL srv_is_signing_negotiated(void)
901 return srv_sign_info.negotiated_smb_signing;
904 /***********************************************************
905 Returns whether signing is actually happening
906 ************************************************************/
908 BOOL srv_signing_started(void)
910 struct smb_basic_signing_context *data;
912 if (!srv_sign_info.doing_signing) {
913 return False;
916 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
917 if (!data)
918 return False;
920 if (data->send_seq_num == 0) {
921 return False;
924 return True;
927 /***********************************************************
928 Turn on signing from this packet onwards.
929 ************************************************************/
931 void srv_set_signing(const DATA_BLOB user_session_key, const DATA_BLOB response)
933 struct smb_basic_signing_context *data;
935 if (!user_session_key.length)
936 return;
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;
945 /* Once we've turned on, ignore any more sessionsetups. */
946 if (srv_sign_info.doing_signing) {
947 return;
950 if (srv_sign_info.free_signing_context)
951 srv_sign_info.free_signing_context(&srv_sign_info);
953 srv_sign_info.doing_signing = True;
955 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
956 memset(data, '\0', sizeof(*data));
958 srv_sign_info.signing_context = data;
960 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
962 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
963 if (response.length)
964 memcpy(&data->mac_key.data[user_session_key.length],response.data, response.length);
966 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
968 DEBUG(3,("srv_set_signing: turning on SMB signing: signing negotiated = %s, mandatory_signing = %s.\n",
969 BOOLSTR(srv_sign_info.negotiated_smb_signing),
970 BOOLSTR(srv_sign_info.mandatory_signing) ));
972 /* Initialise the sequence number */
973 data->send_seq_num = 0;
975 /* Initialise the list of outstanding packets */
976 data->outstanding_packet_list = NULL;
978 srv_sign_info.sign_outgoing_message = srv_sign_outgoing_message;
979 srv_sign_info.check_incoming_message = srv_check_incoming_message;
980 srv_sign_info.free_signing_context = simple_free_signing_context;