2 Unix SMB/CIFS implementation.
4 Copyright (C) Jeremy Allison 2002.
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
6 Copyright (C) James J Myers <myersjj@samba.org> 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /***********************************************************
26 SMB signing - Common code before we set a new signing implementation
27 ************************************************************/
28 static BOOL set_smb_signing_common(struct smbcli_transport *transport)
30 if (!(transport->negotiate.sec_mode &
31 (NEGOTIATE_SECURITY_SIGNATURES_REQUIRED|NEGOTIATE_SECURITY_SIGNATURES_ENABLED))) {
35 if (transport->negotiate.sign_info.doing_signing) {
39 if (!transport->negotiate.sign_info.allow_smb_signing) {
43 if (transport->negotiate.sign_info.free_signing_context)
44 transport->negotiate.sign_info
45 .free_signing_context(&transport->negotiate.sign_info);
47 /* These calls are INCOMPATIBLE with SMB signing */
48 transport->negotiate.readbraw_supported = False;
49 transport->negotiate.writebraw_supported = False;
54 static void mark_packet_signed(struct request_buffer *out)
57 flags2 = SVAL(out->hdr, HDR_FLG2);
58 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
59 SSVAL(out->hdr, HDR_FLG2, flags2);
62 static BOOL signing_good(struct smb_signing_context *sign_info,
63 unsigned int seq, BOOL good)
66 if (!sign_info->doing_signing) {
67 sign_info->doing_signing = True;
69 if (!sign_info->seen_valid) {
70 sign_info->seen_valid = True;
73 if (!sign_info->seen_valid) {
74 /* If we have never seen a good packet, just turn it off */
75 DEBUG(5, ("signing_good: signing negotiated but not required and peer\n"
76 "isn't sending correct signatures. Turning off.\n"));
77 sign_info->allow_smb_signing = False;
78 sign_info->doing_signing = False;
79 if (sign_info->free_signing_context)
80 sign_info->free_signing_context(sign_info);
81 smbcli_null_set_signing(sign_info);
84 /* bad packet after signing started - fail and disconnect. */
85 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", seq));
92 void sign_outgoing_message(struct request_buffer *out, DATA_BLOB *mac_key, uint_t seq_num)
94 uint8_t calc_md5_mac[16];
95 struct MD5Context md5_ctx;
97 * Firstly put the sequence number into the first 4 bytes.
98 * and zero out the next 4 bytes.
100 SIVAL(out->hdr, HDR_SS_FIELD, seq_num);
101 SIVAL(out->hdr, HDR_SS_FIELD + 4, 0);
103 /* mark the packet as signed - BEFORE we sign it...*/
104 mark_packet_signed(out);
106 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
108 MD5Update(&md5_ctx, mac_key->data,
111 out->buffer + NBT_HDR_SIZE,
112 out->size - NBT_HDR_SIZE);
113 MD5Final(calc_md5_mac, &md5_ctx);
115 memcpy(&out->hdr[HDR_SS_FIELD], calc_md5_mac, 8);
117 DEBUG(5, ("sign_outgoing_message: SENT SIG (seq: %d): sent SMB signature of\n",
119 dump_data(5, calc_md5_mac, 8);
120 /* req->out.hdr[HDR_SS_FIELD+2]=0;
121 Uncomment this to test if the remote server actually verifies signitures...*/
124 BOOL check_signed_incoming_message(struct request_buffer *in, DATA_BLOB *mac_key, uint_t seq_num)
127 uint8_t calc_md5_mac[16];
128 uint8_t *server_sent_mac;
129 uint8_t sequence_buf[8];
130 struct MD5Context md5_ctx;
131 const size_t offset_end_of_sig = (HDR_SS_FIELD + 8);
133 const int sign_range = 0;
135 /* room enough for the signature? */
136 if (in->size < NBT_HDR_SIZE + HDR_SS_FIELD + 8) {
140 /* its quite bogus to be guessing sequence numbers, but very useful
141 when debugging signing implementations */
142 for (i = 0-sign_range; i <= 0+sign_range; i++) {
144 * Firstly put the sequence number into the first 4 bytes.
145 * and zero out the next 4 bytes.
147 SIVAL(sequence_buf, 0, seq_num + i);
148 SIVAL(sequence_buf, 4, 0);
150 /* get a copy of the server-sent mac */
151 server_sent_mac = &in->hdr[HDR_SS_FIELD];
153 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
155 MD5Update(&md5_ctx, mac_key->data,
157 MD5Update(&md5_ctx, in->hdr, HDR_SS_FIELD);
158 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
160 MD5Update(&md5_ctx, in->hdr + offset_end_of_sig,
161 in->size - NBT_HDR_SIZE - (offset_end_of_sig));
162 MD5Final(calc_md5_mac, &md5_ctx);
164 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
168 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): wanted SMB signature of\n", seq_num + i));
169 dump_data(5, calc_md5_mac, 8);
171 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): got SMB signature of\n", seq_num + i));
172 dump_data(5, server_sent_mac, 8);
174 DEBUG(15, ("check_signed_incoming_message: GOOD SIG (seq: %d): got SMB signature of\n", seq_num + i));
175 dump_data(5, server_sent_mac, 8);
182 if (good && i != 0) {
183 DEBUG(0,("SIGNING OFFSET %d (should be %d)\n", i, seq_num));
188 /***********************************************************
189 SMB signing - Simple implementation - calculate a MAC to send.
190 ************************************************************/
191 static void smbcli_request_simple_sign_outgoing_message(struct smbcli_request *req)
194 /* enable this when packet signing is preventing you working out why valgrind
195 says that data is uninitialised */
196 file_save("pkt.dat", req->out.buffer, req->out.size);
199 req->seq_num = req->transport->negotiate.sign_info.next_seq_num;
201 /* some requests (eg. NTcancel) are one way, and the sequence number
202 should be increased by 1 not 2 */
203 if (req->sign_single_increment) {
204 req->transport->negotiate.sign_info.next_seq_num += 1;
206 req->transport->negotiate.sign_info.next_seq_num += 2;
209 sign_outgoing_message(&req->out,
210 &req->transport->negotiate.sign_info.mac_key,
215 /***********************************************************
216 SMB signing - Simple implementation - check a MAC sent by server.
217 ************************************************************/
218 static BOOL smbcli_request_simple_check_incoming_message(struct smbcli_request *req)
220 BOOL good = check_signed_incoming_message(&req->in,
221 &req->transport->negotiate.sign_info.mac_key,
224 return signing_good(&req->transport->negotiate.sign_info,
225 req->seq_num+1, good);
229 /***********************************************************
230 SMB signing - Simple implementation - free signing context
231 ************************************************************/
232 static void smbcli_transport_simple_free_signing_context(struct smb_signing_context *sign_info)
234 data_blob_free(&sign_info->mac_key);
238 /***********************************************************
239 SMB signing - Simple implementation - setup the MAC key.
240 ************************************************************/
241 BOOL smbcli_simple_set_signing(struct smb_signing_context *sign_info,
242 const DATA_BLOB user_session_key,
243 const DATA_BLOB response)
245 if (sign_info->mandatory_signing) {
246 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
249 DEBUG(5, ("SMB signing enabled!\n"));
251 sign_info->mac_key = data_blob(NULL, response.length + user_session_key.length);
253 memcpy(&sign_info->mac_key.data[0], user_session_key.data, user_session_key.length);
255 if (response.length) {
256 memcpy(&sign_info->mac_key.data[user_session_key.length],response.data, response.length);
259 dump_data_pw("Started Signing with key:\n", sign_info->mac_key.data, sign_info->mac_key.length);
261 /* Initialise the sequence number */
262 sign_info->next_seq_num = 0;
264 sign_info->sign_outgoing_message = smbcli_request_simple_sign_outgoing_message;
265 sign_info->check_incoming_message = smbcli_request_simple_check_incoming_message;
266 sign_info->free_signing_context = smbcli_transport_simple_free_signing_context;
272 /***********************************************************
273 SMB signing - Simple implementation - setup the MAC key.
274 ************************************************************/
275 BOOL smbcli_transport_simple_set_signing(struct smbcli_transport *transport,
276 const DATA_BLOB user_session_key,
277 const DATA_BLOB response)
279 if (!set_smb_signing_common(transport)) {
283 return smbcli_simple_set_signing(&transport->negotiate.sign_info,
289 /***********************************************************
290 SMB signing - NULL implementation - calculate a MAC to send.
291 ************************************************************/
292 static void smbcli_request_null_sign_outgoing_message(struct smbcli_request *req)
294 /* we can't zero out the sig, as we might be trying to send a
295 transport request - which is NBT-level, not SMB level and doesn't
300 /***********************************************************
301 SMB signing - NULL implementation - check a MAC sent by server.
302 ************************************************************/
303 static BOOL smbcli_request_null_check_incoming_message(struct smbcli_request *req)
309 /***********************************************************
310 SMB signing - NULL implementation - free signing context
311 ************************************************************/
312 static void smbcli_null_free_signing_context(struct smb_signing_context *sign_info)
317 SMB signing - NULL implementation - setup the MAC key.
319 @note Used as an initialisation only - it will not correctly
320 shut down a real signing mechanism
322 BOOL smbcli_null_set_signing(struct smb_signing_context *sign_info)
324 sign_info->sign_outgoing_message = smbcli_request_null_sign_outgoing_message;
325 sign_info->check_incoming_message = smbcli_request_null_check_incoming_message;
326 sign_info->free_signing_context = smbcli_null_free_signing_context;
331 /***********************************************************
332 SMB signing - TEMP implementation (send BSRSPYL during SPNEGO) - calculate a MAC to send.
333 ************************************************************/
334 static void smbcli_request_temp_sign_outgoing_message(struct smbcli_request *req)
336 /* mark the packet as signed - BEFORE we sign it...*/
337 mark_packet_signed(&req->out);
339 /* I wonder what BSRSPYL stands for - but this is what MS
341 memcpy((req->out.hdr + HDR_SS_FIELD), "BSRSPYL ", 8);
346 /***********************************************************
347 SMB signing - TEMP implementation - check a MAC sent by server.
348 ************************************************************/
349 static BOOL smbcli_request_temp_check_incoming_message(struct smbcli_request *req)
354 /***********************************************************
355 SMB signing - NULL implementation - free signing context
356 ************************************************************/
357 static void smbcli_temp_free_signing_context(struct smb_signing_context *sign_info)
363 SMB signing - TEMP implementation - setup the MAC key.
365 @note Used as an initialisation only - it will not correctly
366 shut down a real signing mechanism
368 BOOL smbcli_temp_set_signing(struct smbcli_transport *transport)
370 if (!set_smb_signing_common(transport)) {
374 transport->negotiate.sign_info.sign_outgoing_message = smbcli_request_temp_sign_outgoing_message;
375 transport->negotiate.sign_info.check_incoming_message = smbcli_request_temp_check_incoming_message;
376 transport->negotiate.sign_info.free_signing_context = smbcli_temp_free_signing_context;
382 * Free the signing context
384 void smbcli_transport_free_signing_context(struct smb_signing_context *sign_info)
386 if (sign_info->free_signing_context) {
387 sign_info->free_signing_context(sign_info);
390 smbcli_null_set_signing(sign_info);
395 * Sign a packet with the current mechanism
397 void smbcli_request_calculate_sign_mac(struct smbcli_request *req)
399 req->transport->negotiate.sign_info.sign_outgoing_message(req);
404 * Check a packet with the current mechanism
405 * @return False if we had an established signing connection
406 * which had a back checksum, True otherwise
408 BOOL smbcli_request_check_sign_mac(struct smbcli_request *req)
412 if (req->in.size < (HDR_SS_FIELD + 8)) {
415 good = req->transport->negotiate.sign_info.check_incoming_message(req);
418 if (!good && req->transport->negotiate.sign_info.doing_signing) {
426 BOOL smbcli_init_signing(struct smbcli_transport *transport)
428 if (!smbcli_null_set_signing(&transport->negotiate.sign_info)) {
432 switch (lp_client_signing()) {
433 case SMB_SIGNING_OFF:
434 transport->negotiate.sign_info.allow_smb_signing = False;
436 case SMB_SIGNING_SUPPORTED:
437 transport->negotiate.sign_info.allow_smb_signing = True;
439 case SMB_SIGNING_REQUIRED:
440 transport->negotiate.sign_info.allow_smb_signing = True;
441 transport->negotiate.sign_info.mandatory_signing = True;