ctdb-scripts: Don't set arp_filter=1 by default in 10.interface
[samba4-gss.git] / source4 / libcli / raw / smb_signing.c
blobc921db2cb4335dc069c0d65ad8061385a1dbd9ed
1 /*
2 Unix SMB/CIFS implementation.
3 SMB Signing Code
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/>.
22 #include "includes.h"
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"));
37 return false;
40 if (!sign_info->allow_smb_signing) {
41 DEBUG(5, ("SMB Signing has been locally disabled\n"));
42 return false;
45 return true;
48 void mark_packet_signed(struct smb_request_buffer *out)
50 uint16_t flags2;
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)
59 if (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;
68 } else {
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);
74 return true;
75 } else {
76 /* bad packet after signing started - fail and disconnect. */
77 DEBUG(0, ("signing_good: BAD SIG: seq %u\n", seq));
78 return false;
81 return true;
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;
88 int rc;
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);
102 if (rc < 0) {
103 return;
106 rc = gnutls_hash(hash_hnd, mac_key->data, mac_key->length);
107 if (rc < 0) {
108 gnutls_hash_deinit(hash_hnd, NULL);
109 return;
111 rc = gnutls_hash(hash_hnd,
112 out->buffer + NBT_HDR_SIZE,
113 out->size - NBT_HDR_SIZE);
114 if (rc < 0) {
115 gnutls_hash_deinit(hash_hnd, NULL);
116 return;
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",
123 seq_num));
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)
132 bool ok = false;
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);
138 int rc;
139 int i;
140 const int sign_range = 0;
142 /* room enough for the signature? */
143 if (in->size < NBT_HDR_SIZE + HDR_SS_FIELD + 8) {
144 return false;
147 if (!mac_key->length) {
148 /* NO key yet */
149 return false;
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);
167 if (rc < 0) {
168 ok = false;
169 goto out;
172 rc = gnutls_hash(hash_hnd, mac_key->data, mac_key->length);
173 if (rc < 0) {
174 gnutls_hash_deinit(hash_hnd, NULL);
175 ok = false;
176 goto out;
178 rc = gnutls_hash(hash_hnd, in->hdr, HDR_SS_FIELD);
179 if (rc < 0) {
180 gnutls_hash_deinit(hash_hnd, NULL);
181 ok = false;
182 goto out;
184 rc = gnutls_hash(hash_hnd, sequence_buf, sizeof(sequence_buf));
185 if (rc < 0) {
186 gnutls_hash_deinit(hash_hnd, NULL);
187 ok = false;
188 goto out;
190 rc = gnutls_hash(hash_hnd,
191 in->hdr + offset_end_of_sig,
192 in->size - NBT_HDR_SIZE - (offset_end_of_sig));
193 if (rc < 0) {
194 gnutls_hash_deinit(hash_hnd, NULL);
195 ok = false;
196 goto out;
199 gnutls_hash_deinit(hash_hnd, calc_md5_mac);
201 ok = mem_equal_const_time(server_sent_mac, calc_md5_mac, 8);
203 if (i == 0) {
204 if (!ok) {
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);
210 } else {
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);
217 if (ok) break;
220 if (ok && i != 0) {
221 DEBUG(0,("SIGNING OFFSET %d (should be %d)\n", i, seq_num));
224 out:
225 return ok;
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;
240 return true;
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);
259 } else {
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;
274 return true;