Updated with sepcifics of how we determine sendfile.
[Samba.git] / source / libsmb / smb_signing.c
blob6768c2accc6bee7af30b78f4f962e392305da505
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;
31 struct smb_basic_signing_context {
32 DATA_BLOB mac_key;
33 uint32 send_seq_num;
34 struct outstanding_packet_lookup *outstanding_packet_list;
37 static BOOL store_sequence_for_reply(struct outstanding_packet_lookup **list,
38 uint16 mid, uint32 reply_seq_num)
40 struct outstanding_packet_lookup *t;
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;
51 t = SMB_XMALLOC_P(struct outstanding_packet_lookup);
52 ZERO_STRUCTP(t);
54 t->mid = mid;
55 t->reply_seq_num = reply_seq_num;
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 DLIST_REMOVE(*list, t);
83 SAFE_FREE(t);
84 return True;
87 return False;
90 /***********************************************************
91 SMB signing - Common code before we set a new signing implementation
92 ************************************************************/
94 static BOOL cli_set_smb_signing_common(struct cli_state *cli)
96 if (!cli->sign_info.allow_smb_signing) {
97 return False;
100 if (!cli->sign_info.negotiated_smb_signing
101 && !cli->sign_info.mandatory_signing) {
102 return False;
105 if (cli->sign_info.doing_signing) {
106 return False;
109 if (cli->sign_info.free_signing_context)
110 cli->sign_info.free_signing_context(&cli->sign_info);
112 /* These calls are INCOMPATIBLE with SMB signing */
113 cli->readbraw_supported = False;
114 cli->writebraw_supported = False;
116 return True;
119 /***********************************************************
120 SMB signing - Common code for 'real' implementations
121 ************************************************************/
123 static BOOL set_smb_signing_real_common(struct smb_sign_info *si)
125 if (si->mandatory_signing) {
126 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
129 si->doing_signing = True;
130 DEBUG(5, ("SMB signing enabled!\n"));
132 return True;
135 static void mark_packet_signed(char *outbuf)
137 uint16 flags2;
138 flags2 = SVAL(outbuf,smb_flg2);
139 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
140 SSVAL(outbuf,smb_flg2, flags2);
143 /***********************************************************
144 SMB signing - NULL implementation - calculate a MAC to send.
145 ************************************************************/
147 static void null_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
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;
155 /***********************************************************
156 SMB signing - NULL implementation - check a MAC sent by server.
157 ************************************************************/
159 static BOOL null_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
161 return True;
164 /***********************************************************
165 SMB signing - NULL implementation - free signing context
166 ************************************************************/
168 static void null_free_signing_context(struct smb_sign_info *si)
170 return;
174 SMB signing - NULL implementation - setup the MAC key.
176 @note Used as an initialisation only - it will not correctly
177 shut down a real signing mechanism
180 static BOOL null_set_signing(struct smb_sign_info *si)
182 si->signing_context = NULL;
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;
188 return True;
192 * Free the signing context
195 static void free_signing_context(struct smb_sign_info *si)
197 if (si->free_signing_context) {
198 si->free_signing_context(si);
199 si->signing_context = NULL;
202 null_set_signing(si);
206 static BOOL signing_good(char *inbuf, struct smb_sign_info *si, BOOL good, uint32 seq, BOOL must_be_ok)
208 if (good) {
210 if (!si->doing_signing) {
211 si->doing_signing = True;
214 if (!si->seen_valid) {
215 si->seen_valid = True;
218 } else {
219 if (!si->mandatory_signing && !si->seen_valid) {
221 if (!must_be_ok) {
222 return True;
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;
242 return True;
245 /***********************************************************
246 SMB signing - Simple implementation - calculate a MAC on the packet
247 ************************************************************/
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])
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
262 * Firstly put the sequence number into the first 4 bytes.
263 * and zero out the next 4 bytes.
265 * We do this here, to avoid modifying the packet.
268 DEBUG(10,("simple_packet_signature: sequence number %u\n", seq_number ));
270 SIVAL(sequence_buf, 0, seq_number);
271 SIVAL(sequence_buf, 4, 0);
273 /* Calculate the 16 byte MAC - but don't alter the data in the
274 incoming packet.
276 This makes for a bit of fussing about, but it's not too bad.
278 MD5Init(&md5_ctx);
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);
291 #endif
293 /* copy in the first bit of the SMB header */
294 MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
296 /* copy in the sequence number, instead of the signature */
297 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
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));
303 /* calculate the MD5 sig */
304 MD5Final(calc_md5_mac, &md5_ctx);
308 /***********************************************************
309 SMB signing - Client implementation - send the MAC.
310 ************************************************************/
312 static void client_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
314 unsigned char calc_md5_mac[16];
315 struct smb_basic_signing_context *data =
316 (struct smb_basic_signing_context *)si->signing_context;
318 if (!si->doing_signing)
319 return;
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();
328 /* mark the packet as signed - BEFORE we sign it...*/
329 mark_packet_signed(outbuf);
331 simple_packet_signature(data, (const unsigned char *)outbuf,
332 data->send_seq_num, calc_md5_mac);
334 DEBUG(10, ("client_sign_outgoing_message: sent SMB signature of\n"));
335 dump_data(10, (const char *)calc_md5_mac, 8);
337 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
339 /* cli->outbuf[smb_ss_field+2]=0;
340 Uncomment this to test if the remote server actually verifies signatures...*/
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.
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;
360 /***********************************************************
361 SMB signing - Client implementation - check a MAC sent by server.
362 ************************************************************/
364 static BOOL client_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
366 BOOL good;
367 uint32 reply_seq_number;
368 unsigned char calc_md5_mac[16];
369 unsigned char *server_sent_mac;
371 struct smb_basic_signing_context *data =
372 (struct smb_basic_signing_context *)si->signing_context;
374 if (!si->doing_signing)
375 return True;
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;
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;
388 simple_packet_signature(data, (const unsigned char *)inbuf,
389 reply_seq_number, calc_md5_mac);
391 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
392 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
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);
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 */
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;
412 #endif /* JRATEST */
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);
418 return signing_good(inbuf, si, good, reply_seq_number, must_be_ok);
421 /***********************************************************
422 SMB signing - Simple implementation - free signing context
423 ************************************************************/
425 static void simple_free_signing_context(struct smb_sign_info *si)
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;
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);
438 data_blob_free(&data->mac_key);
440 SAFE_FREE(si->signing_context);
442 return;
445 /***********************************************************
446 SMB signing - Simple implementation - setup the MAC key.
447 ************************************************************/
449 BOOL cli_simple_set_signing(struct cli_state *cli,
450 const DATA_BLOB user_session_key,
451 const DATA_BLOB response)
453 struct smb_basic_signing_context *data;
455 if (!user_session_key.length)
456 return False;
458 if (!cli_set_smb_signing_common(cli)) {
459 return False;
462 if (!set_smb_signing_real_common(&cli->sign_info)) {
463 return False;
466 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
467 memset(data, '\0', sizeof(*data));
469 cli->sign_info.signing_context = data;
471 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
473 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
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);
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"));
486 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
488 /* Initialise the sequence number */
489 data->send_seq_num = 0;
491 /* Initialise the list of outstanding packets */
492 data->outstanding_packet_list = NULL;
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;
498 return True;
501 /***********************************************************
502 SMB signing - TEMP implementation - calculate a MAC to send.
503 ************************************************************/
505 static void temp_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
507 /* mark the packet as signed - BEFORE we sign it...*/
508 mark_packet_signed(outbuf);
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;
516 /***********************************************************
517 SMB signing - TEMP implementation - check a MAC sent by server.
518 ************************************************************/
520 static BOOL temp_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL foo)
522 return True;
525 /***********************************************************
526 SMB signing - TEMP implementation - free signing context
527 ************************************************************/
529 static void temp_free_signing_context(struct smb_sign_info *si)
531 return;
534 /***********************************************************
535 SMB signing - NULL implementation - setup the MAC key.
536 ************************************************************/
538 BOOL cli_null_set_signing(struct cli_state *cli)
540 return null_set_signing(&cli->sign_info);
543 /***********************************************************
544 SMB signing - temp implementation - setup the MAC key.
545 ************************************************************/
547 BOOL cli_temp_set_signing(struct cli_state *cli)
549 if (!cli_set_smb_signing_common(cli)) {
550 return False;
553 cli->sign_info.signing_context = NULL;
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;
559 return True;
562 void cli_free_signing_context(struct cli_state *cli)
564 free_signing_context(&cli->sign_info);
568 * Sign a packet with the current mechanism
571 void cli_calculate_sign_mac(struct cli_state *cli)
573 cli->sign_info.sign_outgoing_message(cli->outbuf, &cli->sign_info);
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.
582 BOOL cli_check_sign_mac(struct cli_state *cli)
584 if (!cli->sign_info.check_incoming_message(cli->inbuf, &cli->sign_info, True)) {
585 free_signing_context(&cli->sign_info);
586 return False;
588 return True;
591 /***********************************************************
592 SMB signing - Server implementation - send the MAC.
593 ************************************************************/
595 static void srv_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
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;
603 if (!si->doing_signing) {
604 return;
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();
614 /* mark the packet as signed - BEFORE we sign it...*/
615 mark_packet_signed(outbuf);
617 mid = SVAL(outbuf, smb_mid);
619 /* See if this is a reply for a deferred packet. */
620 get_sequence_for_reply(&data->outstanding_packet_list, mid, &send_seq_number);
622 simple_packet_signature(data, (const unsigned char *)outbuf, send_seq_number, calc_md5_mac);
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);
627 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
629 /* cli->outbuf[smb_ss_field+2]=0;
630 Uncomment this to test if the remote client actually verifies signatures...*/
633 /***********************************************************
634 SMB signing - Server implementation - check a MAC sent by server.
635 ************************************************************/
637 static BOOL srv_check_incoming_message(char *inbuf, struct smb_sign_info *si, BOOL must_be_ok)
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;
647 if (!si->doing_signing)
648 return True;
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;
655 /* We always increment the sequence number. */
656 data->send_seq_num += 2;
658 saved_seq = reply_seq_number;
659 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
661 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
662 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
664 if (!good) {
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);
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);
676 #if 1 /* JRATEST */
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;
689 #endif /* JRATEST */
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);
696 return (signing_good(inbuf, si, good, saved_seq, must_be_ok));
699 /***********************************************************
700 SMB signing - server API's.
701 ************************************************************/
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
714 /***********************************************************
715 Turn signing off or on for oplock break code.
716 ************************************************************/
718 BOOL srv_oplock_set_signing(BOOL onoff)
720 BOOL ret = srv_sign_info.doing_signing;
721 srv_sign_info.doing_signing = onoff;
722 return ret;
725 /***********************************************************
726 Called to validate an incoming packet from the client.
727 ************************************************************/
729 BOOL srv_check_sign_mac(char *inbuf, BOOL must_be_ok)
731 /* Check if it's a session keepalive. */
732 if(CVAL(inbuf,0) == SMBkeepalive)
733 return True;
735 return srv_sign_info.check_incoming_message(inbuf, &srv_sign_info, must_be_ok);
738 /***********************************************************
739 Called to sign an outgoing packet to the client.
740 ************************************************************/
742 void srv_calculate_sign_mac(char *outbuf)
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;
749 srv_sign_info.sign_outgoing_message(outbuf, &srv_sign_info);
752 /***********************************************************
753 Called by server to defer an outgoing packet.
754 ************************************************************/
756 void srv_defer_sign_response(uint16 mid)
758 struct smb_basic_signing_context *data;
760 if (!srv_sign_info.doing_signing)
761 return;
763 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
765 if (!data)
766 return;
769 * Ensure we only store this mid reply once...
772 store_sequence_for_reply(&data->outstanding_packet_list, mid,
773 data->send_seq_num-1);
776 /***********************************************************
777 Called to remove sequence records when a deferred packet is
778 cancelled by mid. This should never find one....
779 ************************************************************/
781 void srv_cancel_sign_response(uint16 mid)
783 struct smb_basic_signing_context *data;
784 uint32 dummy_seq;
786 if (!srv_sign_info.doing_signing)
787 return;
789 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
791 if (!data)
792 return;
794 DEBUG(10,("srv_cancel_sign_response: for mid %u\n", (unsigned int)mid ));
796 while (get_sequence_for_reply(&data->outstanding_packet_list, mid, &dummy_seq))
799 /* cancel doesn't send a reply so doesn't burn a sequence number. */
800 data->send_seq_num -= 1;
803 /***********************************************************
804 Called by server negprot when signing has been negotiated.
805 ************************************************************/
807 void srv_set_signing_negotiated(void)
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;
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;
819 /***********************************************************
820 Returns whether signing is active. We can't use sendfile or raw
821 reads/writes if it is.
822 ************************************************************/
824 BOOL srv_is_signing_active(void)
826 return srv_sign_info.doing_signing;
830 /***********************************************************
831 Returns whether signing is negotiated. We can't use it unless it was
832 in the negprot.
833 ************************************************************/
835 BOOL srv_is_signing_negotiated(void)
837 return srv_sign_info.negotiated_smb_signing;
840 /***********************************************************
841 Returns whether signing is actually happening
842 ************************************************************/
844 BOOL srv_signing_started(void)
846 struct smb_basic_signing_context *data;
848 if (!srv_sign_info.doing_signing) {
849 return False;
852 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
853 if (!data)
854 return False;
856 if (data->send_seq_num == 0) {
857 return False;
860 return True;
863 /***********************************************************
864 Turn on signing from this packet onwards.
865 ************************************************************/
867 void srv_set_signing(const DATA_BLOB user_session_key, const DATA_BLOB response)
869 struct smb_basic_signing_context *data;
871 if (!user_session_key.length)
872 return;
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;
881 /* Once we've turned on, ignore any more sessionsetups. */
882 if (srv_sign_info.doing_signing) {
883 return;
886 if (srv_sign_info.free_signing_context)
887 srv_sign_info.free_signing_context(&srv_sign_info);
889 srv_sign_info.doing_signing = True;
891 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
892 memset(data, '\0', sizeof(*data));
894 srv_sign_info.signing_context = data;
896 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
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);
902 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
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) ));
908 /* Initialise the sequence number */
909 data->send_seq_num = 0;
911 /* Initialise the list of outstanding packets */
912 data->outstanding_packet_list = NULL;
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;