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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "../lib/crypto/crypto.h"
27 #include <gnutls/gnutls.h>
28 #include <gnutls/crypto.h>
30 /***********************************************************
31 SMB signing - Common code before we set a new signing implementation
32 ************************************************************/
33 bool set_smb_signing_common(struct smb_signing_context
*sign_info
)
35 if (sign_info
->doing_signing
) {
36 DEBUG(5, ("SMB Signing already in progress, so we don't start it again\n"));
40 if (!sign_info
->allow_smb_signing
) {
41 DEBUG(5, ("SMB Signing has been locally disabled\n"));
48 void mark_packet_signed(struct smb_request_buffer
*out
)
51 flags2
= SVAL(out
->hdr
, HDR_FLG2
);
52 flags2
|= FLAGS2_SMB_SECURITY_SIGNATURES
;
53 SSVAL(out
->hdr
, HDR_FLG2
, flags2
);
56 bool signing_good(struct smb_signing_context
*sign_info
,
57 unsigned int seq
, bool good
)
60 if (!sign_info
->doing_signing
) {
61 DEBUG(5, ("Seen valid packet, so turning signing on\n"));
62 sign_info
->doing_signing
= true;
64 if (!sign_info
->seen_valid
) {
65 DEBUG(5, ("Seen valid packet, so marking signing as 'seen valid'\n"));
66 sign_info
->seen_valid
= true;
69 if (!sign_info
->seen_valid
) {
70 /* If we have never seen a good packet, just turn it off */
71 DEBUG(5, ("signing_good: signing negotiated but not required and peer\n"
72 "isn't sending correct signatures. Turning off.\n"));
73 smbcli_set_signing_off(sign_info
);
76 /* bad packet after signing started - fail and disconnect. */
77 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", seq
));
84 void sign_outgoing_message(struct smb_request_buffer
*out
, DATA_BLOB
*mac_key
, unsigned int seq_num
)
86 uint8_t calc_md5_mac
[16];
87 gnutls_hash_hd_t hash_hnd
= NULL
;
91 * Firstly put the sequence number into the first 4 bytes.
92 * and zero out the next 4 bytes.
94 SIVAL(out
->hdr
, HDR_SS_FIELD
, seq_num
);
95 SIVAL(out
->hdr
, HDR_SS_FIELD
+ 4, 0);
97 /* mark the packet as signed - BEFORE we sign it...*/
98 mark_packet_signed(out
);
100 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
101 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
106 rc
= gnutls_hash(hash_hnd
, mac_key
->data
, mac_key
->length
);
108 gnutls_hash_deinit(hash_hnd
, NULL
);
111 rc
= gnutls_hash(hash_hnd
,
112 out
->buffer
+ NBT_HDR_SIZE
,
113 out
->size
- NBT_HDR_SIZE
);
115 gnutls_hash_deinit(hash_hnd
, NULL
);
118 gnutls_hash_deinit(hash_hnd
, calc_md5_mac
);
120 memcpy(&out
->hdr
[HDR_SS_FIELD
], calc_md5_mac
, 8);
122 DEBUG(5, ("sign_outgoing_message: SENT SIG (seq: %d): sent SMB signature of\n",
124 dump_data(5, calc_md5_mac
, 8);
125 ZERO_ARRAY(calc_md5_mac
);
126 /* req->out.hdr[HDR_SS_FIELD+2]=0;
127 Uncomment this to test if the remote server actually verifies signitures...*/
130 bool check_signed_incoming_message(struct smb_request_buffer
*in
, DATA_BLOB
*mac_key
, unsigned int seq_num
)
133 uint8_t calc_md5_mac
[16];
134 uint8_t *server_sent_mac
;
135 uint8_t sequence_buf
[8];
136 gnutls_hash_hd_t hash_hnd
;
137 const size_t offset_end_of_sig
= (HDR_SS_FIELD
+ 8);
140 const int sign_range
= 0;
142 /* room enough for the signature? */
143 if (in
->size
< NBT_HDR_SIZE
+ HDR_SS_FIELD
+ 8) {
147 if (!mac_key
->length
) {
152 /* its quite bogus to be guessing sequence numbers, but very useful
153 when debugging signing implementations */
154 for (i
= 0-sign_range
; i
<= 0+sign_range
; i
++) {
156 * Firstly put the sequence number into the first 4 bytes.
157 * and zero out the next 4 bytes.
159 SIVAL(sequence_buf
, 0, seq_num
+ i
);
160 SIVAL(sequence_buf
, 4, 0);
162 /* get a copy of the server-sent mac */
163 server_sent_mac
= &in
->hdr
[HDR_SS_FIELD
];
165 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
166 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
172 rc
= gnutls_hash(hash_hnd
, mac_key
->data
, mac_key
->length
);
174 gnutls_hash_deinit(hash_hnd
, NULL
);
178 rc
= gnutls_hash(hash_hnd
, in
->hdr
, HDR_SS_FIELD
);
180 gnutls_hash_deinit(hash_hnd
, NULL
);
184 rc
= gnutls_hash(hash_hnd
, sequence_buf
, sizeof(sequence_buf
));
186 gnutls_hash_deinit(hash_hnd
, NULL
);
190 rc
= gnutls_hash(hash_hnd
,
191 in
->hdr
+ offset_end_of_sig
,
192 in
->size
- NBT_HDR_SIZE
- (offset_end_of_sig
));
194 gnutls_hash_deinit(hash_hnd
, NULL
);
199 gnutls_hash_deinit(hash_hnd
, calc_md5_mac
);
201 ok
= mem_equal_const_time(server_sent_mac
, calc_md5_mac
, 8);
205 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): wanted SMB signature of\n", seq_num
+ i
));
206 dump_data(5, calc_md5_mac
, 8);
208 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): got SMB signature of\n", seq_num
+ i
));
209 dump_data(5, server_sent_mac
, 8);
211 DEBUG(15, ("check_signed_incoming_message: GOOD SIG (seq: %d): got SMB signature of\n", seq_num
+ i
));
212 dump_data(5, server_sent_mac
, 8);
215 ZERO_ARRAY(calc_md5_mac
);
221 DEBUG(0,("SIGNING OFFSET %d (should be %d)\n", i
, seq_num
));
229 SMB signing - NULL implementation
231 @note Used as an initialisation only - it will not correctly
232 shut down a real signing mechanism
234 bool smbcli_set_signing_off(struct smb_signing_context
*sign_info
)
236 DEBUG(5, ("Shutdown SMB signing\n"));
237 sign_info
->doing_signing
= false;
238 data_blob_free(&sign_info
->mac_key
);
239 sign_info
->signing_state
= SMB_SIGNING_ENGINE_OFF
;
243 /***********************************************************
244 SMB signing - Simple implementation - setup the MAC key.
245 ************************************************************/
246 bool smbcli_simple_set_signing(TALLOC_CTX
*mem_ctx
,
247 struct smb_signing_context
*sign_info
,
248 const DATA_BLOB
*user_session_key
,
249 const DATA_BLOB
*response
)
251 if (sign_info
->mandatory_signing
) {
252 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
255 DEBUG(5, ("SMB signing enabled!\n"));
257 if (response
&& response
->length
) {
258 sign_info
->mac_key
= data_blob_talloc(mem_ctx
, NULL
, response
->length
+ user_session_key
->length
);
260 sign_info
->mac_key
= data_blob_talloc(mem_ctx
, NULL
, user_session_key
->length
);
263 memcpy(&sign_info
->mac_key
.data
[0], user_session_key
->data
, user_session_key
->length
);
265 if (response
&& response
->length
) {
266 memcpy(&sign_info
->mac_key
.data
[user_session_key
->length
],response
->data
, response
->length
);
269 dump_data_pw("Started Signing with key:\n", sign_info
->mac_key
.data
, sign_info
->mac_key
.length
);
271 sign_info
->signing_state
= SMB_SIGNING_ENGINE_ON
;
272 sign_info
->next_seq_num
= 2;