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 struct smb_basic_signing_context {
27 uint32_t next_seq_num;
30 /***********************************************************
31 SMB signing - Common code before we set a new signing implementation
32 ************************************************************/
33 static BOOL set_smb_signing_common(struct cli_transport *transport)
35 if (!(transport->negotiate.sec_mode &
36 (NEGOTIATE_SECURITY_SIGNATURES_REQUIRED|NEGOTIATE_SECURITY_SIGNATURES_ENABLED))) {
40 if (transport->negotiate.sign_info.doing_signing) {
44 if (!transport->negotiate.sign_info.allow_smb_signing) {
48 if (transport->negotiate.sign_info.free_signing_context)
49 transport->negotiate.sign_info.free_signing_context(transport);
51 /* These calls are INCOMPATIBLE with SMB signing */
52 transport->negotiate.readbraw_supported = False;
53 transport->negotiate.writebraw_supported = False;
58 /***********************************************************
59 SMB signing - Common code for 'real' implementations
60 ************************************************************/
61 static BOOL set_smb_signing_real_common(struct cli_transport *transport)
63 if (transport->negotiate.sign_info.mandatory_signing) {
64 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
67 DEBUG(5, ("SMB signing enabled!\n"));
72 static void mark_packet_signed(struct cli_request *req)
75 flags2 = SVAL(req->out.hdr, HDR_FLG2);
76 flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
77 SSVAL(req->out.hdr, HDR_FLG2, flags2);
80 static BOOL signing_good(struct cli_request *req, int seq, BOOL good)
83 if (!req->transport->negotiate.sign_info.doing_signing) {
84 req->transport->negotiate.sign_info.doing_signing = True;
86 if (!req->transport->negotiate.sign_info.seen_valid) {
87 req->transport->negotiate.sign_info.seen_valid = True;
90 if (!req->transport->negotiate.sign_info.mandatory_signing && !req->transport->negotiate.sign_info.seen_valid) {
92 /* Non-mandatory signing - just turn off if this is the first bad packet.. */
93 DEBUG(5, ("signing_good: signing negotiated but not required and peer\n"
94 "isn't sending correct signatures. Turning off.\n"));
95 req->transport->negotiate.sign_info.negotiated_smb_signing = False;
96 req->transport->negotiate.sign_info.allow_smb_signing = False;
97 req->transport->negotiate.sign_info.doing_signing = False;
98 if (req->transport->negotiate.sign_info.free_signing_context)
99 req->transport->negotiate.sign_info.free_signing_context(req->transport);
100 cli_null_set_signing(req->transport);
103 /* Mandatory signing or bad packet after signing started - fail and disconnect. */
105 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", (unsigned int)seq));
112 /***********************************************************
113 SMB signing - Simple implementation - calculate a MAC to send.
114 ************************************************************/
115 static void cli_request_simple_sign_outgoing_message(struct cli_request *req)
117 uint8_t calc_md5_mac[16];
118 struct MD5Context md5_ctx;
119 struct smb_basic_signing_context *data = req->transport->negotiate.sign_info.signing_context;
122 /* enable this when packet signing is preventing you working out why valgrind
123 says that data is uninitialised */
124 file_save("pkt.dat", req->out.buffer, req->out.size);
127 req->seq_num = data->next_seq_num;
129 /* some requests (eg. NTcancel) are one way, and the sequence number
130 should be increased by 1 not 2 */
131 if (req->one_way_request) {
132 data->next_seq_num += 1;
134 data->next_seq_num += 2;
138 * Firstly put the sequence number into the first 4 bytes.
139 * and zero out the next 4 bytes.
141 SIVAL(req->out.hdr, HDR_SS_FIELD, req->seq_num);
142 SIVAL(req->out.hdr, HDR_SS_FIELD + 4, 0);
144 /* mark the packet as signed - BEFORE we sign it...*/
145 mark_packet_signed(req);
147 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
149 MD5Update(&md5_ctx, data->mac_key.data,
150 data->mac_key.length);
152 req->out.buffer + NBT_HDR_SIZE,
153 req->out.size - NBT_HDR_SIZE);
154 MD5Final(calc_md5_mac, &md5_ctx);
156 memcpy(&req->out.hdr[HDR_SS_FIELD], calc_md5_mac, 8);
158 DEBUG(5, ("cli_request_simple_sign_outgoing_message: SENT SIG (seq: %d, next %d): sent SMB signature of\n",
159 req->seq_num, data->next_seq_num));
160 dump_data(5, calc_md5_mac, 8);
161 /* req->out.hdr[HDR_SS_FIELD+2]=0;
162 Uncomment this to test if the remote server actually verifies signitures...*/
166 /***********************************************************
167 SMB signing - Simple implementation - check a MAC sent by server.
168 ************************************************************/
169 static BOOL cli_request_simple_check_incoming_message(struct cli_request *req)
172 uint8_t calc_md5_mac[16];
173 uint8_t server_sent_mac[8];
174 uint8_t sequence_buf[8];
175 struct MD5Context md5_ctx;
176 struct smb_basic_signing_context *data = req->transport->negotiate.sign_info.signing_context;
177 const size_t offset_end_of_sig = (HDR_SS_FIELD + 8);
179 const int sign_range = 0;
181 /* its quite bogus to be guessing sequence numbers, but very useful
182 when debugging signing implementations */
183 for (i = 1-sign_range; i <= 1+sign_range; i++) {
185 * Firstly put the sequence number into the first 4 bytes.
186 * and zero out the next 4 bytes.
188 SIVAL(sequence_buf, 0, req->seq_num+i);
189 SIVAL(sequence_buf, 4, 0);
191 /* get a copy of the server-sent mac */
192 memcpy(server_sent_mac, &req->in.hdr[HDR_SS_FIELD], sizeof(server_sent_mac));
194 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
196 MD5Update(&md5_ctx, data->mac_key.data,
197 data->mac_key.length);
198 MD5Update(&md5_ctx, req->in.hdr, HDR_SS_FIELD);
199 MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
201 MD5Update(&md5_ctx, req->in.hdr + offset_end_of_sig,
202 req->in.size - NBT_HDR_SIZE - (offset_end_of_sig));
203 MD5Final(calc_md5_mac, &md5_ctx);
205 good = (memcmp(server_sent_mac, calc_md5_mac, 8) == 0);
209 DEBUG(5, ("cli_request_simple_check_incoming_message: BAD SIG (seq: %d): wanted SMB signature of\n", req->seq_num + i));
210 dump_data(5, calc_md5_mac, 8);
212 DEBUG(5, ("cli_request_simple_check_incoming_message: BAD SIG (seq: %d): got SMB signature of\n", req->seq_num + i));
213 dump_data(5, server_sent_mac, 8);
215 DEBUG(15, ("cli_request_simple_check_incoming_message: GOOD SIG (seq: %d): got SMB signature of\n", req->seq_num + i));
216 dump_data(5, server_sent_mac, 8);
223 if (good && i != 1) {
224 DEBUG(0,("SIGNING OFFSET %d (should be %d)\n", i, req->seq_num+1));
227 return signing_good(req, req->seq_num+1, good);
231 /***********************************************************
232 SMB signing - Simple implementation - free signing context
233 ************************************************************/
234 static void cli_transport_simple_free_signing_context(struct cli_transport *transport)
236 struct smb_basic_signing_context *data = transport->negotiate.sign_info.signing_context;
238 data_blob_free(&data->mac_key);
239 SAFE_FREE(transport->negotiate.sign_info.signing_context);
245 /***********************************************************
246 SMB signing - Simple implementation - setup the MAC key.
247 ************************************************************/
248 BOOL cli_transport_simple_set_signing(struct cli_transport *transport,
249 const DATA_BLOB user_session_key,
250 const DATA_BLOB response,
253 struct smb_basic_signing_context *data;
255 if (!set_smb_signing_common(transport)) {
259 if (!set_smb_signing_real_common(transport)) {
263 data = smb_xmalloc(sizeof(*data));
264 transport->negotiate.sign_info.signing_context = data;
266 data->mac_key = data_blob(NULL, response.length + user_session_key.length);
268 memcpy(&data->mac_key.data[0], user_session_key.data, user_session_key.length);
270 if (response.length) {
271 memcpy(&data->mac_key.data[user_session_key.length],response.data, response.length);
274 /* Initialise the sequence number */
275 data->next_seq_num = seq_num;
277 transport->negotiate.sign_info.sign_outgoing_message = cli_request_simple_sign_outgoing_message;
278 transport->negotiate.sign_info.check_incoming_message = cli_request_simple_check_incoming_message;
279 transport->negotiate.sign_info.free_signing_context = cli_transport_simple_free_signing_context;
285 /***********************************************************
286 SMB signing - NULL implementation - calculate a MAC to send.
287 ************************************************************/
288 static void cli_request_null_sign_outgoing_message(struct cli_request *req)
290 /* we can't zero out the sig, as we might be trying to send a
291 transport request - which is NBT-level, not SMB level and doesn't
296 /***********************************************************
297 SMB signing - NULL implementation - check a MAC sent by server.
298 ************************************************************/
299 static BOOL cli_request_null_check_incoming_message(struct cli_request *req)
305 /***********************************************************
306 SMB signing - NULL implementation - free signing context
307 ************************************************************/
308 static void cli_null_free_signing_context(struct cli_transport *transport)
313 SMB signing - NULL implementation - setup the MAC key.
315 @note Used as an initialisation only - it will not correctly
316 shut down a real signing mechanism
318 BOOL cli_null_set_signing(struct cli_transport *transport)
320 transport->negotiate.sign_info.signing_context = NULL;
322 transport->negotiate.sign_info.sign_outgoing_message = cli_request_null_sign_outgoing_message;
323 transport->negotiate.sign_info.check_incoming_message = cli_request_null_check_incoming_message;
324 transport->negotiate.sign_info.free_signing_context = cli_null_free_signing_context;
329 /***********************************************************
330 SMB signing - TEMP implementation - calculate a MAC to send.
331 ************************************************************/
332 static void cli_request_temp_sign_outgoing_message(struct cli_request *req)
334 /* mark the packet as signed - BEFORE we sign it...*/
335 mark_packet_signed(req);
337 /* I wonder what BSRSPYL stands for - but this is what MS
339 memcpy((req->out.hdr + HDR_SS_FIELD), "BSRSPYL ", 8);
343 /***********************************************************
344 SMB signing - TEMP implementation - check a MAC sent by server.
345 ************************************************************/
346 static BOOL cli_request_temp_check_incoming_message(struct cli_request *req)
351 /***********************************************************
352 SMB signing - NULL implementation - free signing context
353 ************************************************************/
354 static void cli_temp_free_signing_context(struct cli_transport *transport)
360 SMB signing - TEMP implementation - setup the MAC key.
362 @note Used as an initialisation only - it will not correctly
363 shut down a real signing mechanism
365 BOOL cli_temp_set_signing(struct cli_transport *transport)
367 if (!set_smb_signing_common(transport)) {
371 transport->negotiate.sign_info.signing_context = NULL;
373 transport->negotiate.sign_info.sign_outgoing_message = cli_request_temp_sign_outgoing_message;
374 transport->negotiate.sign_info.check_incoming_message = cli_request_temp_check_incoming_message;
375 transport->negotiate.sign_info.free_signing_context = cli_temp_free_signing_context;
381 * Free the signing context
383 void cli_transport_free_signing_context(struct cli_transport *transport)
385 if (transport->negotiate.sign_info.free_signing_context) {
386 transport->negotiate.sign_info.free_signing_context(transport);
389 cli_null_set_signing(transport);
394 * Sign a packet with the current mechanism
396 void cli_request_calculate_sign_mac(struct cli_request *req)
398 req->transport->negotiate.sign_info.sign_outgoing_message(req);
403 * Check a packet with the current mechanism
404 * @return False if we had an established signing connection
405 * which had a back checksum, True otherwise
407 BOOL cli_request_check_sign_mac(struct cli_request *req)
411 if (req->in.size < (HDR_SS_FIELD + 8)) {
414 good = req->transport->negotiate.sign_info.check_incoming_message(req);
417 if (!good && req->transport->negotiate.sign_info.doing_signing) {
425 BOOL cli_init_signing(struct cli_transport *transport)
427 if (!cli_null_set_signing(transport)) {
431 switch (lp_client_signing()) {
432 case SMB_SIGNING_OFF:
433 transport->negotiate.sign_info.allow_smb_signing = False;
435 case SMB_SIGNING_SUPPORTED:
436 transport->negotiate.sign_info.allow_smb_signing = True;
438 case SMB_SIGNING_REQUIRED:
439 transport->negotiate.sign_info.allow_smb_signing = True;
440 transport->negotiate.sign_info.mandatory_signing = True;