Initial revamp of the libsmbclient interface.
[Samba/gebeck_regimport.git] / source3 / libsmb / smb_signing.c
blobbd6d97123d66ae4094fc7f53ac6eaf4be322a6d9
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
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;
30 struct smb_basic_signing_context {
31 DATA_BLOB mac_key;
32 uint32 send_seq_num;
33 struct outstanding_packet_lookup *outstanding_packet_list;
36 static bool store_sequence_for_reply(struct outstanding_packet_lookup **list,
37 uint16 mid, uint32 reply_seq_num)
39 struct outstanding_packet_lookup *t;
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;
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;
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>.
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;
71 static bool get_sequence_for_reply(struct outstanding_packet_lookup **list,
72 uint16 mid, uint32 *reply_seq_num)
74 struct outstanding_packet_lookup *t;
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;
86 return False;
89 /***********************************************************
90 SMB signing - Common code before we set a new signing implementation
91 ************************************************************/
93 static bool cli_set_smb_signing_common(struct cli_state *cli)
95 if (!cli->sign_info.allow_smb_signing) {
96 return False;
99 if (!cli->sign_info.negotiated_smb_signing
100 && !cli->sign_info.mandatory_signing) {
101 return False;
104 if (cli->sign_info.doing_signing) {
105 return False;
108 if (cli->sign_info.free_signing_context)
109 cli->sign_info.free_signing_context(&cli->sign_info);
111 /* These calls are INCOMPATIBLE with SMB signing */
112 cli->readbraw_supported = False;
113 cli->writebraw_supported = False;
115 return True;
118 /***********************************************************
119 SMB signing - Common code for 'real' implementations
120 ************************************************************/
122 static bool set_smb_signing_real_common(struct smb_sign_info *si)
124 if (si->mandatory_signing) {
125 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
128 si->doing_signing = True;
129 DEBUG(5, ("SMB signing enabled!\n"));
131 return True;
134 static void mark_packet_signed(char *outbuf)
136 uint16 flags2;
137 flags2 = SVAL(outbuf,smb_flg2);
138 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
139 SSVAL(outbuf,smb_flg2, flags2);
142 /***********************************************************
143 SMB signing - NULL implementation - calculate a MAC to send.
144 ************************************************************/
146 static void null_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
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;
154 /***********************************************************
155 SMB signing - NULL implementation - check a MAC sent by server.
156 ************************************************************/
158 static bool null_check_incoming_message(const char *inbuf,
159 struct smb_sign_info *si,
160 bool must_be_ok)
162 return True;
165 /***********************************************************
166 SMB signing - NULL implementation - free signing context
167 ************************************************************/
169 static void null_free_signing_context(struct smb_sign_info *si)
171 return;
175 SMB signing - NULL implementation - setup the MAC key.
177 @note Used as an initialisation only - it will not correctly
178 shut down a real signing mechanism
181 static bool null_set_signing(struct smb_sign_info *si)
183 si->signing_context = NULL;
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;
189 return True;
193 * Free the signing context
196 static void free_signing_context(struct smb_sign_info *si)
198 if (si->free_signing_context) {
199 si->free_signing_context(si);
200 si->signing_context = NULL;
203 null_set_signing(si);
207 static bool signing_good(const char *inbuf, struct smb_sign_info *si,
208 bool good, uint32 seq, bool must_be_ok)
210 if (good) {
212 if (!si->doing_signing) {
213 si->doing_signing = True;
216 if (!si->seen_valid) {
217 si->seen_valid = True;
220 } else {
221 if (!si->mandatory_signing && !si->seen_valid) {
223 if (!must_be_ok) {
224 return True;
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;
244 return True;
247 /***********************************************************
248 SMB signing - Simple implementation - calculate a MAC on the packet
249 ************************************************************/
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])
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
264 * Firstly put the sequence number into the first 4 bytes.
265 * and zero out the next 4 bytes.
267 * We do this here, to avoid modifying the packet.
270 DEBUG(10,("simple_packet_signature: sequence number %u\n", seq_number ));
272 SIVAL(sequence_buf, 0, seq_number);
273 SIVAL(sequence_buf, 4, 0);
275 /* Calculate the 16 byte MAC - but don't alter the data in the
276 incoming packet.
278 This makes for a bit of fussing about, but it's not too bad.
280 MD5Init(&md5_ctx);
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);
293 #endif
295 /* copy in the first bit of the SMB header */
296 MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
298 /* copy in the sequence number, instead of the signature */
299 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
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));
305 /* calculate the MD5 sig */
306 MD5Final(calc_md5_mac, &md5_ctx);
310 /***********************************************************
311 SMB signing - Client implementation - send the MAC.
312 ************************************************************/
314 static void client_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
316 unsigned char calc_md5_mac[16];
317 struct smb_basic_signing_context *data =
318 (struct smb_basic_signing_context *)si->signing_context;
320 if (!si->doing_signing)
321 return;
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();
330 /* mark the packet as signed - BEFORE we sign it...*/
331 mark_packet_signed(outbuf);
333 simple_packet_signature(data, (const unsigned char *)outbuf,
334 data->send_seq_num, calc_md5_mac);
336 DEBUG(10, ("client_sign_outgoing_message: sent SMB signature of\n"));
337 dump_data(10, calc_md5_mac, 8);
339 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
341 /* cli->outbuf[smb_ss_field+2]=0;
342 Uncomment this to test if the remote server actually verifies signatures...*/
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.
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;
362 /***********************************************************
363 SMB signing - Client implementation - check a MAC sent by server.
364 ************************************************************/
366 static bool client_check_incoming_message(const char *inbuf,
367 struct smb_sign_info *si,
368 bool must_be_ok)
370 bool good;
371 uint32 reply_seq_number;
372 unsigned char calc_md5_mac[16];
373 unsigned char *server_sent_mac;
375 struct smb_basic_signing_context *data =
376 (struct smb_basic_signing_context *)si->signing_context;
378 if (!si->doing_signing)
379 return True;
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;
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;
392 simple_packet_signature(data, (const unsigned char *)inbuf,
393 reply_seq_number, calc_md5_mac);
395 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
396 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
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);
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 */
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;
416 #endif /* JRATEST */
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);
422 return signing_good(inbuf, si, good, reply_seq_number, must_be_ok);
425 /***********************************************************
426 SMB signing - Simple implementation - free signing context
427 ************************************************************/
429 static void simple_free_signing_context(struct smb_sign_info *si)
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;
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);
442 data_blob_free(&data->mac_key);
444 SAFE_FREE(si->signing_context);
446 return;
449 /***********************************************************
450 SMB signing - Simple implementation - setup the MAC key.
451 ************************************************************/
453 bool cli_simple_set_signing(struct cli_state *cli,
454 const DATA_BLOB user_session_key,
455 const DATA_BLOB response)
457 struct smb_basic_signing_context *data;
459 if (!user_session_key.length)
460 return False;
462 if (!cli_set_smb_signing_common(cli)) {
463 return False;
466 if (!set_smb_signing_real_common(&cli->sign_info)) {
467 return False;
470 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
471 memset(data, '\0', sizeof(*data));
473 cli->sign_info.signing_context = data;
475 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
477 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
479 DEBUG(10, ("cli_simple_set_signing: user_session_key\n"));
480 dump_data(10, user_session_key.data, user_session_key.length);
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"));
490 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
492 /* Initialise the sequence number */
493 data->send_seq_num = 0;
495 /* Initialise the list of outstanding packets */
496 data->outstanding_packet_list = NULL;
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;
502 return True;
505 /***********************************************************
506 SMB signing - TEMP implementation - calculate a MAC to send.
507 ************************************************************/
509 static void temp_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
511 /* mark the packet as signed - BEFORE we sign it...*/
512 mark_packet_signed(outbuf);
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;
520 /***********************************************************
521 SMB signing - TEMP implementation - check a MAC sent by server.
522 ************************************************************/
524 static bool temp_check_incoming_message(const char *inbuf,
525 struct smb_sign_info *si, bool foo)
527 return True;
530 /***********************************************************
531 SMB signing - TEMP implementation - free signing context
532 ************************************************************/
534 static void temp_free_signing_context(struct smb_sign_info *si)
536 return;
539 /***********************************************************
540 SMB signing - NULL implementation - setup the MAC key.
541 ************************************************************/
543 bool cli_null_set_signing(struct cli_state *cli)
545 return null_set_signing(&cli->sign_info);
548 /***********************************************************
549 SMB signing - temp implementation - setup the MAC key.
550 ************************************************************/
552 bool cli_temp_set_signing(struct cli_state *cli)
554 if (!cli_set_smb_signing_common(cli)) {
555 return False;
558 cli->sign_info.signing_context = NULL;
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;
564 return True;
567 void cli_free_signing_context(struct cli_state *cli)
569 free_signing_context(&cli->sign_info);
573 * Sign a packet with the current mechanism
576 void cli_calculate_sign_mac(struct cli_state *cli, char *buf)
578 cli->sign_info.sign_outgoing_message(buf, &cli->sign_info);
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.
587 bool cli_check_sign_mac(struct cli_state *cli, char *buf)
589 if (!cli->sign_info.check_incoming_message(buf, &cli->sign_info, True)) {
590 free_signing_context(&cli->sign_info);
591 return False;
593 return True;
596 /***********************************************************
597 Is client signing on ?
598 ************************************************************/
600 bool client_is_signing_on(struct cli_state *cli)
602 struct smb_sign_info *si = &cli->sign_info;
603 return si->doing_signing;
606 /***********************************************************
607 SMB signing - Server implementation - send the MAC.
608 ************************************************************/
610 static void srv_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
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;
618 if (!si->doing_signing) {
619 return;
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();
629 /* mark the packet as signed - BEFORE we sign it...*/
630 mark_packet_signed(outbuf);
632 mid = SVAL(outbuf, smb_mid);
634 /* See if this is a reply for a deferred packet. */
635 get_sequence_for_reply(&data->outstanding_packet_list, mid, &send_seq_number);
637 simple_packet_signature(data, (const unsigned char *)outbuf, send_seq_number, calc_md5_mac);
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);
642 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
644 /* cli->outbuf[smb_ss_field+2]=0;
645 Uncomment this to test if the remote client actually verifies signatures...*/
648 /***********************************************************
649 SMB signing - Server implementation - check a MAC sent by server.
650 ************************************************************/
652 static bool srv_check_incoming_message(const char *inbuf,
653 struct smb_sign_info *si,
654 bool must_be_ok)
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;
664 if (!si->doing_signing)
665 return True;
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;
672 /* We always increment the sequence number. */
673 data->send_seq_num += 2;
675 saved_seq = reply_seq_number;
676 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number, calc_md5_mac);
678 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
679 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
681 if (!good) {
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);
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);
693 #if 1 /* JRATEST */
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;
706 #endif /* JRATEST */
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);
713 return (signing_good(inbuf, si, good, saved_seq, must_be_ok));
716 /***********************************************************
717 SMB signing - server API's.
718 ************************************************************/
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
731 /***********************************************************
732 Turn signing off or on for oplock break code.
733 ************************************************************/
735 bool srv_oplock_set_signing(bool onoff)
737 bool ret = srv_sign_info.doing_signing;
738 srv_sign_info.doing_signing = onoff;
739 return ret;
742 /***********************************************************
743 Called to validate an incoming packet from the client.
744 ************************************************************/
746 bool srv_check_sign_mac(const char *inbuf, bool must_be_ok)
748 /* Check if it's a non-session message. */
749 if(CVAL(inbuf,0)) {
750 return True;
753 return srv_sign_info.check_incoming_message(inbuf, &srv_sign_info, must_be_ok);
756 /***********************************************************
757 Called to sign an outgoing packet to the client.
758 ************************************************************/
760 void srv_calculate_sign_mac(char *outbuf)
762 /* Check if it's a non-session message. */
763 if(CVAL(outbuf,0)) {
764 return;
767 srv_sign_info.sign_outgoing_message(outbuf, &srv_sign_info);
770 /***********************************************************
771 Called by server to defer an outgoing packet.
772 ************************************************************/
774 void srv_defer_sign_response(uint16 mid)
776 struct smb_basic_signing_context *data;
778 if (!srv_sign_info.doing_signing)
779 return;
781 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
783 if (!data)
784 return;
787 * Ensure we only store this mid reply once...
790 store_sequence_for_reply(&data->outstanding_packet_list, mid,
791 data->send_seq_num-1);
794 /***********************************************************
795 Called to remove sequence records when a deferred packet is
796 cancelled by mid. This should never find one....
797 ************************************************************/
799 void srv_cancel_sign_response(uint16 mid)
801 struct smb_basic_signing_context *data;
802 uint32 dummy_seq;
804 if (!srv_sign_info.doing_signing)
805 return;
807 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
809 if (!data)
810 return;
812 DEBUG(10,("srv_cancel_sign_response: for mid %u\n", (unsigned int)mid ));
814 while (get_sequence_for_reply(&data->outstanding_packet_list, mid, &dummy_seq))
817 /* cancel doesn't send a reply so doesn't burn a sequence number. */
818 data->send_seq_num -= 1;
821 /***********************************************************
822 Called by server negprot when signing has been negotiated.
823 ************************************************************/
825 void srv_set_signing_negotiated(void)
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;
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;
837 /***********************************************************
838 Returns whether signing is active. We can't use sendfile or raw
839 reads/writes if it is.
840 ************************************************************/
842 bool srv_is_signing_active(void)
844 return srv_sign_info.doing_signing;
848 /***********************************************************
849 Returns whether signing is negotiated. We can't use it unless it was
850 in the negprot.
851 ************************************************************/
853 bool srv_is_signing_negotiated(void)
855 return srv_sign_info.negotiated_smb_signing;
858 /***********************************************************
859 Returns whether signing is actually happening
860 ************************************************************/
862 bool srv_signing_started(void)
864 struct smb_basic_signing_context *data;
866 if (!srv_sign_info.doing_signing) {
867 return False;
870 data = (struct smb_basic_signing_context *)srv_sign_info.signing_context;
871 if (!data)
872 return False;
874 if (data->send_seq_num == 0) {
875 return False;
878 return True;
881 /***********************************************************
882 Turn on signing from this packet onwards.
883 ************************************************************/
885 void srv_set_signing(const DATA_BLOB user_session_key, const DATA_BLOB response)
887 struct smb_basic_signing_context *data;
889 if (!user_session_key.length)
890 return;
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;
899 /* Once we've turned on, ignore any more sessionsetups. */
900 if (srv_sign_info.doing_signing) {
901 return;
904 if (srv_sign_info.free_signing_context)
905 srv_sign_info.free_signing_context(&srv_sign_info);
907 srv_sign_info.doing_signing = True;
909 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
910 memset(data, '\0', sizeof(*data));
912 srv_sign_info.signing_context = data;
914 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
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);
920 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
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) ));
926 /* Initialise the sequence number */
927 data->send_seq_num = 0;
929 /* Initialise the list of outstanding packets */
930 data->outstanding_packet_list = NULL;
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;