1 /* Copyright (C) Jeremy Allison 2003.
2  * Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 /**
22  * @file smb_signing.c
23  * @brief Unix SMB/CIFS implementation.  SMB Signing Code
24  */
25 
26 /*
27    Modified by Preeti Subramanian <spreeti@secpod.com> for OpenVAS:
28       simple packet signature function argument struct smb_basic_signing_context
29       *data to uint8_t* mac_key and henceforth used mac_key in the
30    implementation
31 */
32 
33 #include "smb_signing.h"
34 
35 void
simple_packet_signature_ntlmssp(uint8_t * mac_key,const uchar * buf,uint32 seq_number,unsigned char * calc_md5_mac)36 simple_packet_signature_ntlmssp (uint8_t *mac_key, const uchar *buf,
37                                  uint32 seq_number, unsigned char *calc_md5_mac)
38 {
39   const size_t offset_end_of_sig = (smb_ss_field + 8);
40   unsigned char sequence_buf[8];
41   struct MD5Context md5_ctx;
42 
43   /*
44    * Firstly put the sequence number into the first 4 bytes.
45    * and zero out the next 4 bytes.
46    *
47    * We do this here, to avoid modifying the packet.
48    */
49 
50   SIVAL (sequence_buf, 0, seq_number);
51   SIVAL (sequence_buf, 4, 0);
52 
53   /* Calculate the 16 byte MAC - but don't alter the data in the
54      incoming packet.
55 
56      This makes for a bit of fussing about, but it's not too bad.
57   */
58   MD5Init (&md5_ctx);
59 
60   /* initialise with the key */
61   MD5Update (&md5_ctx, mac_key, 16);
62 
63   /* copy in the first bit of the SMB header */
64   MD5Update (&md5_ctx, buf + 4, smb_ss_field - 4);
65 
66   /* copy in the sequence number, instead of the signature */
67   MD5Update (&md5_ctx, sequence_buf, sizeof (sequence_buf));
68 
69   /* copy in the rest of the packet in, skipping the signature */
70   MD5Update (&md5_ctx, buf + offset_end_of_sig,
71              smb_len (buf) - (offset_end_of_sig - 4));
72 
73   /* calculate the MD5 sig */
74   MD5Final (calc_md5_mac, &md5_ctx);
75 }
76