s3: make cli_resolve_path return NTSTATUS
[kai/samba.git] / source3 / libsmb / smb_signing.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB Signing Code
4    Copyright (C) Jeremy Allison 2003.
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
6    Copyright (C) Stefan Metzmacher 2009
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "../lib/crypto/md5.h"
24 #include "smb_signing.h"
25
26 /* Used by the SMB signing functions. */
27
28 struct smb_signing_state {
29         /* is signing localy allowed */
30         bool allowed;
31
32         /* is signing localy mandatory */
33         bool mandatory;
34
35         /* is signing negotiated by the peer */
36         bool negotiated;
37
38         /* send BSRSPYL signatures */
39         bool bsrspyl;
40
41         bool active; /* Have I ever seen a validly signed packet? */
42
43         /* mac_key.length > 0 means signing is started */
44         DATA_BLOB mac_key;
45
46         /* the next expected seqnum */
47         uint32_t seqnum;
48
49         TALLOC_CTX *mem_ctx;
50         void *(*alloc_fn)(TALLOC_CTX *mem_ctx, size_t len);
51         void (*free_fn)(TALLOC_CTX *mem_ctx, void *ptr);
52 };
53
54 static void smb_signing_reset_info(struct smb_signing_state *si)
55 {
56         si->active = false;
57         si->bsrspyl = false;
58         si->seqnum = 0;
59
60         if (si->free_fn) {
61                 si->free_fn(si->mem_ctx, si->mac_key.data);
62         } else {
63                 talloc_free(si->mac_key.data);
64         }
65         si->mac_key.data = NULL;
66         si->mac_key.length = 0;
67 }
68
69 struct smb_signing_state *smb_signing_init_ex(TALLOC_CTX *mem_ctx,
70                                               bool allowed,
71                                               bool mandatory,
72                                               void *(*alloc_fn)(TALLOC_CTX *, size_t),
73                                               void (*free_fn)(TALLOC_CTX *, void *))
74 {
75         struct smb_signing_state *si;
76
77         if (alloc_fn) {
78                 void *p = alloc_fn(mem_ctx, sizeof(struct smb_signing_state));
79                 if (p == NULL) {
80                         return NULL;
81                 }
82                 memset(p, 0, sizeof(struct smb_signing_state));
83                 si = (struct smb_signing_state *)p;
84                 si->mem_ctx = mem_ctx;
85                 si->alloc_fn = alloc_fn;
86                 si->free_fn = free_fn;
87         } else {
88                 si = talloc_zero(mem_ctx, struct smb_signing_state);
89                 if (si == NULL) {
90                         return NULL;
91                 }
92         }
93
94         if (mandatory) {
95                 allowed = true;
96         }
97
98         si->allowed = allowed;
99         si->mandatory = mandatory;
100
101         return si;
102 }
103
104 struct smb_signing_state *smb_signing_init(TALLOC_CTX *mem_ctx,
105                                            bool allowed,
106                                            bool mandatory)
107 {
108         return smb_signing_init_ex(mem_ctx, allowed, mandatory, NULL, NULL);
109 }
110
111 static bool smb_signing_good(struct smb_signing_state *si,
112                              bool good, uint32_t seq)
113 {
114         if (good) {
115                 if (!si->active) {
116                         si->active = true;
117                 }
118                 return true;
119         }
120
121         if (!si->mandatory && !si->active) {
122                 /* Non-mandatory signing - just turn off if this is the first bad packet.. */
123                 DEBUG(5, ("smb_signing_good: signing negotiated but not required and peer\n"
124                           "isn't sending correct signatures. Turning off.\n"));
125                 smb_signing_reset_info(si);
126                 return true;
127         }
128
129         /* Mandatory signing or bad packet after signing started - fail and disconnect. */
130         DEBUG(0, ("smb_signing_good: BAD SIG: seq %u\n", (unsigned int)seq));
131         return false;
132 }
133
134 static void smb_signing_md5(const DATA_BLOB *mac_key,
135                             const uint8_t *buf, uint32_t seq_number,
136                             uint8_t calc_md5_mac[16])
137 {
138         const size_t offset_end_of_sig = (smb_ss_field + 8);
139         uint8_t sequence_buf[8];
140         struct MD5Context md5_ctx;
141
142         /*
143          * Firstly put the sequence number into the first 4 bytes.
144          * and zero out the next 4 bytes.
145          *
146          * We do this here, to avoid modifying the packet.
147          */
148
149         DEBUG(10,("smb_signing_md5: sequence number %u\n", seq_number ));
150
151         SIVAL(sequence_buf, 0, seq_number);
152         SIVAL(sequence_buf, 4, 0);
153
154         /* Calculate the 16 byte MAC - but don't alter the data in the
155            incoming packet.
156
157            This makes for a bit of fussing about, but it's not too bad.
158         */
159         MD5Init(&md5_ctx);
160
161         /* intialise with the key */
162         MD5Update(&md5_ctx, mac_key->data, mac_key->length);
163
164         /* copy in the first bit of the SMB header */
165         MD5Update(&md5_ctx, buf + 4, smb_ss_field - 4);
166
167         /* copy in the sequence number, instead of the signature */
168         MD5Update(&md5_ctx, sequence_buf, sizeof(sequence_buf));
169
170         /* copy in the rest of the packet in, skipping the signature */
171         MD5Update(&md5_ctx, buf + offset_end_of_sig, 
172                   smb_len(buf) - (offset_end_of_sig - 4));
173
174         /* calculate the MD5 sig */
175         MD5Final(calc_md5_mac, &md5_ctx);
176 }
177
178 uint32_t smb_signing_next_seqnum(struct smb_signing_state *si, bool oneway)
179 {
180         uint32_t seqnum;
181
182         if (si->mac_key.length == 0) {
183                 return 0;
184         }
185
186         seqnum = si->seqnum;
187         if (oneway) {
188                 si->seqnum += 1;
189         } else {
190                 si->seqnum += 2;
191         }
192
193         return seqnum;
194 }
195
196 void smb_signing_cancel_reply(struct smb_signing_state *si, bool oneway)
197 {
198         if (si->mac_key.length == 0) {
199                 return;
200         }
201
202         if (oneway) {
203                 si->seqnum -= 1;
204         } else {
205                 si->seqnum -= 2;
206         }
207 }
208
209 void smb_signing_sign_pdu(struct smb_signing_state *si,
210                           uint8_t *outbuf, uint32_t seqnum)
211 {
212         uint8_t calc_md5_mac[16];
213         uint16_t flags2;
214
215         if (si->mac_key.length == 0) {
216                 if (!si->bsrspyl) {
217                         return;
218                 }
219         }
220
221         /* JRA Paranioa test - we should be able to get rid of this... */
222         if (smb_len(outbuf) < (smb_ss_field + 8 - 4)) {
223                 DEBUG(1,("smb_signing_sign_pdu: Logic error. "
224                          "Can't check signature on short packet! smb_len = %u\n",
225                          smb_len(outbuf)));
226                 abort();
227         }
228
229         /* mark the packet as signed - BEFORE we sign it...*/
230         flags2 = SVAL(outbuf,smb_flg2);
231         flags2 |= FLAGS2_SMB_SECURITY_SIGNATURES;
232         SSVAL(outbuf, smb_flg2, flags2);
233
234         if (si->bsrspyl) {
235                 /* I wonder what BSRSPYL stands for - but this is what MS
236                    actually sends! */
237                 memcpy(calc_md5_mac, "BSRSPYL ", 8);
238         } else {
239                 smb_signing_md5(&si->mac_key, outbuf,
240                                 seqnum, calc_md5_mac);
241         }
242
243         DEBUG(10, ("smb_signing_sign_pdu: sent SMB signature of\n"));
244         dump_data(10, calc_md5_mac, 8);
245
246         memcpy(&outbuf[smb_ss_field], calc_md5_mac, 8);
247
248 /*      outbuf[smb_ss_field+2]=0;
249         Uncomment this to test if the remote server actually verifies signatures...*/
250 }
251
252 bool smb_signing_check_pdu(struct smb_signing_state *si,
253                            const uint8_t *inbuf, uint32_t seqnum)
254 {
255         bool good;
256         uint8_t calc_md5_mac[16];
257         const uint8_t *reply_sent_mac;
258
259         if (si->mac_key.length == 0) {
260                 return true;
261         }
262
263         if (smb_len(inbuf) < (smb_ss_field + 8 - 4)) {
264                 DEBUG(1,("smb_signing_check_pdu: Can't check signature "
265                          "on short packet! smb_len = %u\n",
266                          smb_len(inbuf)));
267                 return False;
268         }
269
270         smb_signing_md5(&si->mac_key, inbuf,
271                         seqnum, calc_md5_mac);
272
273         reply_sent_mac = &inbuf[smb_ss_field];
274         good = (memcmp(reply_sent_mac, calc_md5_mac, 8) == 0);
275
276         if (!good) {
277                 int i;
278                 const int sign_range = 5;
279
280                 DEBUG(5, ("smb_signing_check_pdu: BAD SIG: wanted SMB signature of\n"));
281                 dump_data(5, calc_md5_mac, 8);
282
283                 DEBUG(5, ("smb_signing_check_pdu: BAD SIG: got SMB signature of\n"));
284                 dump_data(5, reply_sent_mac, 8);
285
286                 for (i = -sign_range; i < sign_range; i++) {
287                         smb_signing_md5(&si->mac_key, inbuf,
288                                         seqnum+i, calc_md5_mac);
289                         if (memcmp(reply_sent_mac, calc_md5_mac, 8) == 0) {
290                                 DEBUG(0,("smb_signing_check_pdu: "
291                                          "out of seq. seq num %u matches. "
292                                          "We were expecting seq %u\n",
293                                          (unsigned int)seqnum+i,
294                                          (unsigned int)seqnum));
295                                 break;
296                         }
297                 }
298         } else {
299                 DEBUG(10, ("smb_signing_check_pdu: seq %u: "
300                            "got good SMB signature of\n",
301                            (unsigned int)seqnum));
302                 dump_data(10, reply_sent_mac, 8);
303         }
304
305         return smb_signing_good(si, good, seqnum);
306 }
307
308 bool smb_signing_set_bsrspyl(struct smb_signing_state *si)
309 {
310         if (!si->negotiated) {
311                 return false;
312         }
313
314         if (si->active) {
315                 return false;
316         }
317
318         si->bsrspyl = true;
319
320         return true;
321 }
322
323 bool smb_signing_activate(struct smb_signing_state *si,
324                           const DATA_BLOB user_session_key,
325                           const DATA_BLOB response)
326 {
327         size_t len;
328         off_t ofs;
329
330         if (!user_session_key.length) {
331                 return false;
332         }
333
334         if (!si->negotiated) {
335                 return false;
336         }
337
338         if (si->active) {
339                 return false;
340         }
341
342         if (si->mac_key.length > 0) {
343                 return false;
344         }
345
346         smb_signing_reset_info(si);
347
348         len = response.length + user_session_key.length;
349         if (si->alloc_fn) {
350                 si->mac_key.data = (uint8_t *)si->alloc_fn(si->mem_ctx, len);
351                 if (si->mac_key.data == NULL) {
352                         return false;
353                 }
354         } else {
355                 si->mac_key.data = (uint8_t *)talloc_size(si, len);
356                 if (si->mac_key.data == NULL) {
357                         return false;
358                 }
359         }
360         si->mac_key.length = len;
361
362         ofs = 0;
363         memcpy(&si->mac_key.data[ofs], user_session_key.data, user_session_key.length);
364
365         DEBUG(10, ("smb_signing_activate: user_session_key\n"));
366         dump_data(10, user_session_key.data, user_session_key.length);
367
368         if (response.length) {
369                 ofs = user_session_key.length;
370                 memcpy(&si->mac_key.data[ofs], response.data, response.length);
371                 DEBUG(10, ("smb_signing_activate: response_data\n"));
372                 dump_data(10, response.data, response.length);
373         } else {
374                 DEBUG(10, ("smb_signing_activate: NULL response_data\n"));
375         }
376
377         dump_data_pw("smb_signing_activate: mac key is:\n",
378                      si->mac_key.data, si->mac_key.length);
379
380         /* Initialise the sequence number */
381         si->seqnum = 2;
382
383         return true;
384 }
385
386 bool smb_signing_is_active(struct smb_signing_state *si)
387 {
388         return si->active;
389 }
390
391 bool smb_signing_is_allowed(struct smb_signing_state *si)
392 {
393         return si->allowed;
394 }
395
396 bool smb_signing_is_mandatory(struct smb_signing_state *si)
397 {
398         return si->mandatory;
399 }
400
401 bool smb_signing_set_negotiated(struct smb_signing_state *si)
402 {
403         if (!si->allowed) {
404                 return false;
405         }
406
407         si->negotiated = true;
408
409         return true;
410 }
411
412 bool smb_signing_is_negotiated(struct smb_signing_state *si)
413 {
414         return si->negotiated;
415 }