2 Unix SMB/CIFS implementation.
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/>.
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;
28 bool can_delete; /* Set to False in trans state. */
31 struct smb_basic_signing_context {
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) {
49 t = SMB_XMALLOC_P(struct outstanding_packet_lookup);
53 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>.
66 DEBUG(10,("store_sequence_for_reply: stored seq = %u mid = %u\n",
67 (unsigned int)reply_seq_num, (unsigned int)mid ));
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) {
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 ));
82 DLIST_REMOVE(*list, t);
91 static bool set_sequence_can_delete_flag(struct outstanding_packet_lookup **list, uint16 mid, bool can_delete_entry)
93 struct outstanding_packet_lookup *t;
95 for (t = *list; t; t = t->next) {
97 t->can_delete = can_delete_entry;
104 /***********************************************************
105 SMB signing - Common code before we set a new signing implementation
106 ************************************************************/
108 static bool cli_set_smb_signing_common(struct cli_state *cli)
110 if (!cli->sign_info.allow_smb_signing) {
114 if (!cli->sign_info.negotiated_smb_signing
115 && !cli->sign_info.mandatory_signing) {
119 if (cli->sign_info.doing_signing) {
123 if (cli->sign_info.free_signing_context)
124 cli->sign_info.free_signing_context(&cli->sign_info);
126 /* These calls are INCOMPATIBLE with SMB signing */
127 cli->readbraw_supported = False;
128 cli->writebraw_supported = False;
133 /***********************************************************
134 SMB signing - Common code for 'real' implementations
135 ************************************************************/
137 static bool set_smb_signing_real_common(struct smb_sign_info *si)
139 if (si->mandatory_signing) {
140 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
143 si->doing_signing = True;
144 DEBUG(5, ("SMB signing enabled!\n"));
149 static void mark_packet_signed(char *outbuf)
152 flags2 = SVAL(outbuf,smb_flg2);
153 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
154 SSVAL(outbuf,smb_flg2, flags2);
157 /***********************************************************
158 SMB signing - NULL implementation - calculate a MAC to send.
159 ************************************************************/
161 static void null_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
163 /* we can't zero out the sig, as we might be trying to send a
164 session request - which is NBT-level, not SMB level and doesn't
169 /***********************************************************
170 SMB signing - NULL implementation - check a MAC sent by server.
171 ************************************************************/
173 static bool null_check_incoming_message(const char *inbuf,
174 struct smb_sign_info *si,
180 /***********************************************************
181 SMB signing - NULL implementation - free signing context
182 ************************************************************/
184 static void null_free_signing_context(struct smb_sign_info *si)
190 SMB signing - NULL implementation - setup the MAC key.
192 @note Used as an initialisation only - it will not correctly
193 shut down a real signing mechanism
196 static bool null_set_signing(struct smb_sign_info *si)
198 si->signing_context = NULL;
200 si->sign_outgoing_message = null_sign_outgoing_message;
201 si->check_incoming_message = null_check_incoming_message;
202 si->free_signing_context = null_free_signing_context;
208 * Free the signing context
211 static void free_signing_context(struct smb_sign_info *si)
213 if (si->free_signing_context) {
214 si->free_signing_context(si);
215 si->signing_context = NULL;
218 null_set_signing(si);
222 static bool signing_good(const char *inbuf, struct smb_sign_info *si,
223 bool good, uint32 seq, bool must_be_ok)
227 if (!si->doing_signing) {
228 si->doing_signing = True;
231 if (!si->seen_valid) {
232 si->seen_valid = True;
236 if (!si->mandatory_signing && !si->seen_valid) {
241 /* Non-mandatory signing - just turn off if this is the first bad packet.. */
242 DEBUG(5, ("srv_check_incoming_message: signing negotiated but not required and peer\n"
243 "isn't sending correct signatures. Turning off.\n"));
244 si->negotiated_smb_signing = False;
245 si->allow_smb_signing = False;
246 si->doing_signing = False;
247 free_signing_context(si);
249 } else if (!must_be_ok) {
250 /* This packet is known to be unsigned */
253 /* Mandatory signing or bad packet after signing started - fail and disconnect. */
255 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", (unsigned int)seq));
262 /***********************************************************
263 SMB signing - Simple implementation - calculate a MAC on the packet
264 ************************************************************/
266 static void simple_packet_signature(struct smb_basic_signing_context *data,
267 const uchar *buf, uint32 seq_number,
268 unsigned char calc_md5_mac[16])
270 const size_t offset_end_of_sig = (smb_ss_field + 8);
271 unsigned char sequence_buf[8];
272 struct MD5Context md5_ctx;
274 /* JRA - apparently this is incorrect. */
275 unsigned char key_buf[16];
279 * Firstly put the sequence number into the first 4 bytes.
280 * and zero out the next 4 bytes.
282 * We do this here, to avoid modifying the packet.
285 DEBUG(10,("simple_packet_signature: sequence number %u\n", seq_number ));
287 SIVAL(sequence_buf, 0, seq_number);
288 SIVAL(sequence_buf, 4, 0);
290 /* Calculate the 16 byte MAC - but don't alter the data in the
293 This makes for a bit of fussing about, but it's not too bad.
297 /* intialise with the key */
298 MD5Update(&md5_ctx, data->mac_key.data, data->mac_key.length);
300 /* JRA - apparently this is incorrect. */
301 /* NB. When making and verifying SMB signatures, Windows apparently
302 zero-pads the key to 128 bits if it isn't long enough.
303 From Nalin Dahyabhai <nalin@redhat.com> */
304 if (data->mac_key.length < sizeof(key_buf)) {
305 memset(key_buf, 0, sizeof(key_buf));
306 MD5Update(&md5_ctx, key_buf, sizeof(key_buf) - data->mac_key.length);
310 /* copy in the first bit of the SMB header */
311 MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
313 /* copy in the sequence number, instead of the signature */
314 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
316 /* copy in the rest of the packet in, skipping the signature */
317 MD5Update(&md5_ctx, buf + offset_end_of_sig,
318 smb_len(buf) - (offset_end_of_sig - 4));
320 /* calculate the MD5 sig */
321 MD5Final(calc_md5_mac, &md5_ctx);
325 /***********************************************************
326 SMB signing - Client implementation - send the MAC.
327 ************************************************************/
329 static void client_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
331 unsigned char calc_md5_mac[16];
332 struct smb_basic_signing_context *data =
333 (struct smb_basic_signing_context *)si->signing_context;
335 if (!si->doing_signing)
338 /* JRA Paranioa test - we should be able to get rid of this... */
339 if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
340 DEBUG(1, ("client_sign_outgoing_message: Logic error. Can't check signature on short packet! smb_len = %u\n",
345 /* mark the packet as signed - BEFORE we sign it...*/
346 mark_packet_signed(outbuf);
348 simple_packet_signature(data, (const unsigned char *)outbuf,
349 data->send_seq_num, calc_md5_mac);
351 DEBUG(10, ("client_sign_outgoing_message: sent SMB signature of\n"));
352 dump_data(10, calc_md5_mac, 8);
354 memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
356 /* cli->outbuf[smb_ss_field+2]=0;
357 Uncomment this to test if the remote server actually verifies signatures...*/
359 /* Instead of re-introducing the trans_info_conect we
360 used to have here, we use the fact that during a
361 SMBtrans/SMBtrans2/SMBnttrans send that the mid stays
362 constant. This means that calling store_sequence_for_reply()
363 will return False for all trans secondaries, as the mid is already
364 on the stored sequence list. As the send_seqence_number must
365 remain constant for all primary+secondary trans sends, we
366 only increment the send sequence number when we successfully
367 add a new entry to the outstanding sequence list. This means
368 I can isolate the fix here rather than re-adding the trans
369 signing on/off calls in libsmb/clitrans2.c JRA.
372 if (store_sequence_for_reply(&data->outstanding_packet_list, SVAL(outbuf,smb_mid), data->send_seq_num + 1)) {
373 data->send_seq_num += 2;
377 /***********************************************************
378 SMB signing - Client implementation - check a MAC sent by server.
379 ************************************************************/
381 static bool client_check_incoming_message(const char *inbuf,
382 struct smb_sign_info *si,
386 uint32 reply_seq_number;
387 unsigned char calc_md5_mac[16];
388 unsigned char *server_sent_mac;
390 struct smb_basic_signing_context *data =
391 (struct smb_basic_signing_context *)si->signing_context;
393 if (!si->doing_signing)
396 if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
397 DEBUG(1, ("client_check_incoming_message: Can't check signature on short packet! smb_len = %u\n", smb_len(inbuf)));
401 if (!get_sequence_for_reply(&data->outstanding_packet_list, SVAL(inbuf, smb_mid), &reply_seq_number)) {
402 DEBUG(1, ("client_check_incoming_message: received message "
403 "with mid %u with no matching send record.\n", (unsigned int)SVAL(inbuf, smb_mid) ));
407 simple_packet_signature(data, (const unsigned char *)inbuf,
408 reply_seq_number, calc_md5_mac);
410 server_sent_mac = (unsigned char *)&inbuf[smb_ss_field];
411 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
414 DEBUG(5, ("client_check_incoming_message: BAD SIG: wanted SMB signature of\n"));
415 dump_data(5, calc_md5_mac, 8);
417 DEBUG(5, ("client_check_incoming_message: BAD SIG: got SMB signature of\n"));
418 dump_data(5, server_sent_mac, 8);
422 for (i = -5; i < 5; i++) {
423 simple_packet_signature(data, (const unsigned char *)inbuf, reply_seq_number+i, calc_md5_mac);
424 if (memcmp(server_sent_mac, calc_md5_mac, 8) == 0) {
425 DEBUG(0,("client_check_incoming_message: out of seq. seq num %u matches. \
426 We were expecting seq %u\n", reply_seq_number+i, reply_seq_number ));
434 DEBUG(10, ("client_check_incoming_message: seq %u: got good SMB signature of\n", (unsigned int)reply_seq_number));
435 dump_data(10, server_sent_mac, 8);
437 return signing_good(inbuf, si, good, reply_seq_number, must_be_ok);
440 /***********************************************************
441 SMB signing - Simple implementation - free signing context
442 ************************************************************/
444 static void simple_free_signing_context(struct smb_sign_info *si)
446 struct smb_basic_signing_context *data =
447 (struct smb_basic_signing_context *)si->signing_context;
448 struct outstanding_packet_lookup *list;
449 struct outstanding_packet_lookup *next;
451 for (list = data->outstanding_packet_list; list; list = next) {
453 DLIST_REMOVE(data->outstanding_packet_list, list);
457 data_blob_free(&data->mac_key);
459 SAFE_FREE(si->signing_context);
464 /***********************************************************
465 SMB signing - Simple implementation - setup the MAC key.
466 ************************************************************/
468 bool cli_simple_set_signing(struct cli_state *cli,
469 const DATA_BLOB user_session_key,
470 const DATA_BLOB response)
472 struct smb_basic_signing_context *data;
474 if (!user_session_key.length)
477 if (!cli_set_smb_signing_common(cli)) {
481 if (!set_smb_signing_real_common(&cli->sign_info)) {
485 data = SMB_XMALLOC_P(struct smb_basic_signing_context);
486 memset(data, '\0', sizeof(*data));
488 cli->sign_info.signing_context = data;
490 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
492 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
494 DEBUG(10, ("cli_simple_set_signing: user_session_key\n"));
495 dump_data(10, user_session_key.data, user_session_key.length);
497 if (response.length) {
498 memcpy(&data->mac_key.data[user_session_key.length],response.data, response.length);
499 DEBUG(10, ("cli_simple_set_signing: response_data\n"));
500 dump_data(10, response.data, response.length);
502 DEBUG(10, ("cli_simple_set_signing: NULL response_data\n"));
505 dump_data_pw("MAC ssession key is:\n", data->mac_key.data, data->mac_key.length);
507 /* Initialise the sequence number */
508 data->send_seq_num = 0;
510 /* Initialise the list of outstanding packets */
511 data->outstanding_packet_list = NULL;
513 cli->sign_info.sign_outgoing_message = client_sign_outgoing_message;
514 cli->sign_info.check_incoming_message = client_check_incoming_message;
515 cli->sign_info.free_signing_context = simple_free_signing_context;
520 /***********************************************************
521 SMB signing - TEMP implementation - calculate a MAC to send.
522 ************************************************************/
524 static void temp_sign_outgoing_message(char *outbuf, struct smb_sign_info *si)
526 /* mark the packet as signed - BEFORE we sign it...*/
527 mark_packet_signed(outbuf);
529 /* I wonder what BSRSPYL stands for - but this is what MS
531 memcpy(&outbuf[smb_ss_field], "BSRSPYL ", 8);
535 /***********************************************************
536 SMB signing - TEMP implementation - check a MAC sent by server.
537 ************************************************************/
539 static bool temp_check_incoming_message(const char *inbuf,
540 struct smb_sign_info *si, bool foo)
545 /***********************************************************
546 SMB signing - TEMP implementation - free signing context
547 ************************************************************/
549 static void temp_free_signing_context(struct smb_sign_info *si)
554 /***********************************************************
555 SMB signing - NULL implementation - setup the MAC key.
556 ************************************************************/
558 bool cli_null_set_signing(struct cli_state *cli)
560 return null_set_signing(&cli->sign_info);
563 /***********************************************************
564 SMB signing - temp implementation - setup the MAC key.
565 ************************************************************/
567 bool cli_temp_set_signing(struct cli_state *cli)
569 if (!cli_set_smb_signing_common(cli)) {
573 cli->sign_info.signing_context = NULL;
575 cli->sign_info.sign_outgoing_message = temp_sign_outgoing_message;
576 cli->sign_info.check_incoming_message = temp_check_incoming_message;
577 cli->sign_info.free_signing_context = temp_free_signing_context;
582 void cli_free_signing_context(struct cli_state *cli)
584 free_signing_context(&cli->sign_info);
588 * Sign a packet with the current mechanism
591 void cli_calculate_sign_mac(struct cli_state *cli, char *buf)
593 cli->sign_info.sign_outgoing_message(buf, &cli->sign_info);
597 * Check a packet with the current mechanism
598 * @return False if we had an established signing connection
599 * which had a bad checksum, True otherwise.
602 bool cli_check_sign_mac(struct cli_state *cli, char *buf)
604 if (!cli->sign_info.check_incoming_message(buf, &cli->sign_info, True)) {
605 free_signing_context(&cli->sign_info);
611 /***********************************************************
612 Enter trans/trans2/nttrans state.
613 ************************************************************/
615 bool client_set_trans_sign_state_on(struct cli_state *cli, uint16 mid)
617 struct smb_sign_info *si = &cli->sign_info;
618 struct smb_basic_signing_context *data = (struct smb_basic_signing_context *)si->signing_context;
620 if (!si->doing_signing) {
628 if (!set_sequence_can_delete_flag(&data->outstanding_packet_list, mid, False)) {
635 /***********************************************************
636 Leave trans/trans2/nttrans state.
637 ************************************************************/
639 bool client_set_trans_sign_state_off(struct cli_state *cli, uint16 mid)
641 uint32 reply_seq_num;
642 struct smb_sign_info *si = &cli->sign_info;
643 struct smb_basic_signing_context *data = (struct smb_basic_signing_context *)si->signing_context;
645 if (!si->doing_signing) {
653 if (!set_sequence_can_delete_flag(&data->outstanding_packet_list, mid, True)) {
657 /* Now delete the stored mid entry. */
658 if (!get_sequence_for_reply(&data->outstanding_packet_list, mid, &reply_seq_num)) {
665 /***********************************************************
666 Is client signing on ?
667 ************************************************************/
669 bool client_is_signing_on(struct cli_state *cli)
671 struct smb_sign_info *si = &cli->sign_info;
672 return si->doing_signing;