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 /***********************************************************
28 SMB signing - Common code before we set a new signing implementation
29 ************************************************************/
30 bool set_smb_signing_common(struct smb_signing_context
*sign_info
)
32 if (sign_info
->doing_signing
) {
33 DEBUG(5, ("SMB Signing already in progress, so we don't start it again\n"));
37 if (!sign_info
->allow_smb_signing
) {
38 DEBUG(5, ("SMB Signing has been locally disabled\n"));
45 void mark_packet_signed(struct smb_request_buffer
*out
)
48 flags2
= SVAL(out
->hdr
, HDR_FLG2
);
49 flags2
|= FLAGS2_SMB_SECURITY_SIGNATURES
;
50 SSVAL(out
->hdr
, HDR_FLG2
, flags2
);
53 bool signing_good(struct smb_signing_context
*sign_info
,
54 unsigned int seq
, bool good
)
57 if (!sign_info
->doing_signing
) {
58 DEBUG(5, ("Seen valid packet, so turning signing on\n"));
59 sign_info
->doing_signing
= true;
61 if (!sign_info
->seen_valid
) {
62 DEBUG(5, ("Seen valid packet, so marking signing as 'seen valid'\n"));
63 sign_info
->seen_valid
= true;
66 if (!sign_info
->seen_valid
) {
67 /* If we have never seen a good packet, just turn it off */
68 DEBUG(5, ("signing_good: signing negotiated but not required and peer\n"
69 "isn't sending correct signatures. Turning off.\n"));
70 smbcli_set_signing_off(sign_info
);
73 /* bad packet after signing started - fail and disconnect. */
74 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", seq
));
81 void sign_outgoing_message(struct smb_request_buffer
*out
, DATA_BLOB
*mac_key
, unsigned int seq_num
)
83 uint8_t calc_md5_mac
[16];
84 struct MD5Context md5_ctx
;
87 * Firstly put the sequence number into the first 4 bytes.
88 * and zero out the next 4 bytes.
90 SIVAL(out
->hdr
, HDR_SS_FIELD
, seq_num
);
91 SIVAL(out
->hdr
, HDR_SS_FIELD
+ 4, 0);
93 /* mark the packet as signed - BEFORE we sign it...*/
94 mark_packet_signed(out
);
96 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
98 MD5Update(&md5_ctx
, mac_key
->data
, mac_key
->length
);
100 out
->buffer
+ NBT_HDR_SIZE
,
101 out
->size
- NBT_HDR_SIZE
);
102 MD5Final(calc_md5_mac
, &md5_ctx
);
104 memcpy(&out
->hdr
[HDR_SS_FIELD
], calc_md5_mac
, 8);
106 DEBUG(5, ("sign_outgoing_message: SENT SIG (seq: %d): sent SMB signature of\n",
108 dump_data(5, calc_md5_mac
, 8);
109 /* req->out.hdr[HDR_SS_FIELD+2]=0;
110 Uncomment this to test if the remote server actually verifies signitures...*/
113 bool check_signed_incoming_message(struct smb_request_buffer
*in
, DATA_BLOB
*mac_key
, unsigned int seq_num
)
116 uint8_t calc_md5_mac
[16];
117 uint8_t *server_sent_mac
;
118 uint8_t sequence_buf
[8];
119 struct MD5Context md5_ctx
;
120 const size_t offset_end_of_sig
= (HDR_SS_FIELD
+ 8);
122 const int sign_range
= 0;
124 /* room enough for the signature? */
125 if (in
->size
< NBT_HDR_SIZE
+ HDR_SS_FIELD
+ 8) {
129 if (!mac_key
->length
) {
134 /* its quite bogus to be guessing sequence numbers, but very useful
135 when debugging signing implementations */
136 for (i
= 0-sign_range
; i
<= 0+sign_range
; i
++) {
138 * Firstly put the sequence number into the first 4 bytes.
139 * and zero out the next 4 bytes.
141 SIVAL(sequence_buf
, 0, seq_num
+ i
);
142 SIVAL(sequence_buf
, 4, 0);
144 /* get a copy of the server-sent mac */
145 server_sent_mac
= &in
->hdr
[HDR_SS_FIELD
];
147 /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
149 MD5Update(&md5_ctx
, mac_key
->data
,
151 MD5Update(&md5_ctx
, in
->hdr
, HDR_SS_FIELD
);
152 MD5Update(&md5_ctx
, sequence_buf
, sizeof(sequence_buf
));
154 MD5Update(&md5_ctx
, in
->hdr
+ offset_end_of_sig
,
155 in
->size
- NBT_HDR_SIZE
- (offset_end_of_sig
));
156 MD5Final(calc_md5_mac
, &md5_ctx
);
158 good
= (memcmp(server_sent_mac
, calc_md5_mac
, 8) == 0);
162 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): wanted SMB signature of\n", seq_num
+ i
));
163 dump_data(5, calc_md5_mac
, 8);
165 DEBUG(5, ("check_signed_incoming_message: BAD SIG (seq: %d): got SMB signature of\n", seq_num
+ i
));
166 dump_data(5, server_sent_mac
, 8);
168 DEBUG(15, ("check_signed_incoming_message: GOOD SIG (seq: %d): got SMB signature of\n", seq_num
+ i
));
169 dump_data(5, server_sent_mac
, 8);
176 if (good
&& i
!= 0) {
177 DEBUG(0,("SIGNING OFFSET %d (should be %d)\n", i
, seq_num
));
184 SMB signing - NULL implementation
186 @note Used as an initialisation only - it will not correctly
187 shut down a real signing mechanism
189 bool smbcli_set_signing_off(struct smb_signing_context
*sign_info
)
191 DEBUG(5, ("Shutdown SMB signing\n"));
192 sign_info
->doing_signing
= false;
193 data_blob_free(&sign_info
->mac_key
);
194 sign_info
->signing_state
= SMB_SIGNING_ENGINE_OFF
;
198 /***********************************************************
199 SMB signing - Simple implementation - setup the MAC key.
200 ************************************************************/
201 bool smbcli_simple_set_signing(TALLOC_CTX
*mem_ctx
,
202 struct smb_signing_context
*sign_info
,
203 const DATA_BLOB
*user_session_key
,
204 const DATA_BLOB
*response
)
206 if (sign_info
->mandatory_signing
) {
207 DEBUG(5, ("Mandatory SMB signing enabled!\n"));
210 DEBUG(5, ("SMB signing enabled!\n"));
212 if (response
&& response
->length
) {
213 sign_info
->mac_key
= data_blob_talloc(mem_ctx
, NULL
, response
->length
+ user_session_key
->length
);
215 sign_info
->mac_key
= data_blob_talloc(mem_ctx
, NULL
, user_session_key
->length
);
218 memcpy(&sign_info
->mac_key
.data
[0], user_session_key
->data
, user_session_key
->length
);
220 if (response
&& response
->length
) {
221 memcpy(&sign_info
->mac_key
.data
[user_session_key
->length
],response
->data
, response
->length
);
224 dump_data_pw("Started Signing with key:\n", sign_info
->mac_key
.data
, sign_info
->mac_key
.length
);
226 sign_info
->signing_state
= SMB_SIGNING_ENGINE_ON
;
227 sign_info
->next_seq_num
= 2;