Align the Python and EJS ldap tests.
[kai/samba.git] / source / libcli / smb2 / signing.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 Signing Code
5
6    Copyright (C) Andrew Tridgell <tridge@samba.org> 2008
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 "libcli/raw/libcliraw.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "heimdal/lib/hcrypto/sha.h"
27
28 /*
29   NOTE: this code does not yet interoperate with the windows SMB2
30   implementation. We are waiting on feedback on the docs to find out
31   why
32  */
33
34
35 /*
36   setup signing on a transport
37  */
38 NTSTATUS smb2_start_signing(struct smb2_transport *transport)
39 {
40         if (transport->signing.session_key.length != 16) {
41                 DEBUG(2,("Wrong session key length %u for SMB2 signing\n",
42                          (unsigned)transport->signing.session_key.length));
43                 return NT_STATUS_ACCESS_DENIED;
44         }
45
46         transport->signing.signing_started = true;
47         return NT_STATUS_OK;
48 }
49
50 /*
51   sign an outgoing message
52  */
53 NTSTATUS smb2_sign_message(struct smb2_request *req)
54 {
55         struct smb2_request_buffer *buf = &req->out;
56         uint64_t session_id;
57         SHA256_CTX m;
58         uint8_t res[32];
59
60         if (!req->transport->signing.doing_signing ||
61             !req->transport->signing.signing_started) {
62                 return NT_STATUS_OK;
63         }
64
65         if (buf->size < NBT_HDR_SIZE + SMB2_HDR_SIGNATURE + 16) {
66                 /* can't sign non-SMB2 messages */
67                 return NT_STATUS_OK;
68         }
69
70         session_id = BVAL(buf->hdr, SMB2_HDR_SESSION_ID);
71         if (session_id == 0) {
72                 /* we don't sign messages with a zero session_id. See
73                    MS-SMB2 3.2.4.1.1 */
74                 return NT_STATUS_OK;            
75         }
76
77         if (req->transport->signing.session_key.length != 16) {
78                 DEBUG(2,("Wrong session key length %u for SMB2 signing\n",
79                          (unsigned)req->transport->signing.session_key.length));
80                 return NT_STATUS_ACCESS_DENIED;
81         }
82
83         memset(buf->hdr + SMB2_HDR_SIGNATURE, 0, 16);
84
85         SIVAL(buf->hdr, SMB2_HDR_FLAGS, IVAL(buf->hdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_SIGNED);
86
87         ZERO_STRUCT(m);
88         SHA256_Init(&m);
89         SHA256_Update(&m, req->transport->signing.session_key.data, 
90                       req->transport->signing.session_key.length);
91         SHA256_Update(&m, buf->buffer+NBT_HDR_SIZE, buf->size-NBT_HDR_SIZE);
92         SHA256_Final(res, &m);
93
94         DEBUG(5,("signed SMB2 message of size %u\n", (unsigned)buf->size - NBT_HDR_SIZE));
95
96         memcpy(buf->hdr + SMB2_HDR_SIGNATURE, res, 16);
97
98         if (DEBUGLVL(5)) {
99                 /* check our own signature */
100                 smb2_check_signature(req->transport, buf->buffer, buf->size);
101         }
102
103         return NT_STATUS_OK;    
104 }
105
106 /*
107   check an incoming signature
108  */
109 NTSTATUS smb2_check_signature(struct smb2_transport *transport,
110                               uint8_t *buffer, uint_t length)
111 {
112         uint64_t session_id;
113         SHA256_CTX m;
114         uint8_t res[SHA256_DIGEST_LENGTH];
115         uint8_t sig[16];
116
117         if (!transport->signing.signing_started ||
118             !transport->signing.doing_signing) {
119                 return NT_STATUS_OK;
120         }
121
122         if (length < NBT_HDR_SIZE + SMB2_HDR_SIGNATURE + 16) {
123                 /* can't check non-SMB2 messages */
124                 return NT_STATUS_OK;
125         }
126
127         session_id = BVAL(buffer+NBT_HDR_SIZE, SMB2_HDR_SESSION_ID);
128         if (session_id == 0) {
129                 /* don't sign messages with a zero session_id. See
130                    MS-SMB2 3.2.4.1.1 */
131                 return NT_STATUS_OK;            
132         }
133
134         if (transport->signing.session_key.length == 0) {
135                 /* we don't have the session key yet */
136                 return NT_STATUS_OK;
137         }
138
139         if (transport->signing.session_key.length != 16) {
140                 DEBUG(2,("Wrong session key length %u for SMB2 signing\n",
141                          (unsigned)transport->signing.session_key.length));
142                 return NT_STATUS_ACCESS_DENIED;
143         }
144
145         memcpy(sig, buffer+NBT_HDR_SIZE+SMB2_HDR_SIGNATURE, 16);
146
147         memset(buffer + NBT_HDR_SIZE + SMB2_HDR_SIGNATURE, 0, 16);
148
149         ZERO_STRUCT(m);
150         SHA256_Init(&m);
151         SHA256_Update(&m, transport->signing.session_key.data,    16);
152         SHA256_Update(&m, buffer+NBT_HDR_SIZE, length-NBT_HDR_SIZE);
153         SHA256_Final(res, &m);
154
155         memcpy(buffer+NBT_HDR_SIZE+SMB2_HDR_SIGNATURE, sig, 16);
156
157         if (memcmp(res, sig, 16) != 0) {
158                 DEBUG(0,("Bad SMB2 signature for message of size %u\n", length));
159                 dump_data(0, sig, 16);
160                 dump_data(0, res, 16);
161                 return NT_STATUS_ACCESS_DENIED;
162         }
163
164         return NT_STATUS_OK;    
165 }