]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/cifsencrypt.c
[CIFS] more whitespace fixes
[mirror_ubuntu-artful-kernel.git] / fs / cifs / cifsencrypt.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifsencrypt.c
3 *
12b3b8ff 4 * Copyright (C) International Business Machines Corp., 2005,2006
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/fs.h>
23#include "cifspdu.h"
ffdd6e4d 24#include "cifsglob.h"
1da177e4
LT
25#include "cifs_debug.h"
26#include "md5.h"
27#include "cifs_unicode.h"
28#include "cifsproto.h"
7c7b25bc 29#include <linux/ctype.h>
6d027cfd 30#include <linux/random.h>
1da177e4 31
ffdd6e4d 32/* Calculate and return the CIFS signature based on the mac key and SMB PDU */
1da177e4
LT
33/* the 16 byte signature must be allocated by the caller */
34/* Note we only use the 1st eight bytes */
ffdd6e4d 35/* Note that the smb header signature field on input contains the
1da177e4
LT
36 sequence number before this function is called */
37
38extern void mdfour(unsigned char *out, unsigned char *in, int n);
39extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
7c7b25bc 40extern void SMBencrypt(unsigned char *passwd, unsigned char *c8,
ffdd6e4d 41 unsigned char *p24);
1da177e4 42
ffdd6e4d
SF
43static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
44 const char *key, char *signature)
1da177e4
LT
45{
46 struct MD5Context context;
47
ffdd6e4d 48 if ((cifs_pdu == NULL) || (signature == NULL))
1da177e4
LT
49 return -EINVAL;
50
51 MD5Init(&context);
ffdd6e4d
SF
52 MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16);
53 MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
54 MD5Final(signature, &context);
1da177e4
LT
55 return 0;
56}
57
ffdd6e4d
SF
58int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
59 __u32 *pexpected_response_sequence_number)
1da177e4
LT
60{
61 int rc = 0;
62 char smb_signature[20];
63
ffdd6e4d 64 if ((cifs_pdu == NULL) || (server == NULL))
1da177e4
LT
65 return -EINVAL;
66
ffdd6e4d 67 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
1da177e4
LT
68 return rc;
69
70 spin_lock(&GlobalMid_Lock);
ffdd6e4d
SF
71 cifs_pdu->Signature.Sequence.SequenceNumber =
72 cpu_to_le32(server->sequence_number);
1da177e4
LT
73 cifs_pdu->Signature.Sequence.Reserved = 0;
74
ad009ac9
SF
75 *pexpected_response_sequence_number = server->sequence_number++;
76 server->sequence_number++;
1da177e4
LT
77 spin_unlock(&GlobalMid_Lock);
78
ffdd6e4d
SF
79 rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,
80 smb_signature);
81 if (rc)
1da177e4
LT
82 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
83 else
84 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
85
86 return rc;
87}
88
ffdd6e4d
SF
89static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
90 const char *key, char *signature)
84afc29b 91{
e9917a00
SF
92 struct MD5Context context;
93 int i;
84afc29b 94
ffdd6e4d 95 if ((iov == NULL) || (signature == NULL))
e9917a00 96 return -EINVAL;
84afc29b 97
e9917a00 98 MD5Init(&context);
ffdd6e4d
SF
99 MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16);
100 for (i=0;i<n_vec;i++) {
101 if (iov[i].iov_base == NULL) {
102 cERROR(1 ,("null iovec entry"));
e9917a00 103 return -EIO;
ffdd6e4d 104 } else if (iov[i].iov_len == 0)
e9917a00 105 break; /* bail out if we are sent nothing to sign */
ffdd6e4d 106 /* The first entry includes a length field (which does not get
e9917a00 107 signed that occupies the first 4 bytes before the header */
ffdd6e4d 108 if (i == 0) {
e9917a00
SF
109 if (iov[0].iov_len <= 8 ) /* cmd field at offset 9 */
110 break; /* nothing to sign or corrupt header */
111 MD5Update(&context,iov[0].iov_base+4, iov[0].iov_len-4);
112 } else
ffdd6e4d 113 MD5Update(&context, iov[i].iov_base, iov[i].iov_len);
e9917a00 114 }
84afc29b 115
ffdd6e4d 116 MD5Final(signature, &context);
84afc29b 117
e9917a00 118 return 0;
84afc29b
SF
119}
120
121
ffdd6e4d 122int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
84afc29b
SF
123 __u32 * pexpected_response_sequence_number)
124{
125 int rc = 0;
126 char smb_signature[20];
ffdd6e4d 127 struct smb_hdr *cifs_pdu = iov[0].iov_base;
84afc29b 128
ffdd6e4d 129 if ((cifs_pdu == NULL) || (server == NULL))
84afc29b
SF
130 return -EINVAL;
131
ffdd6e4d 132 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
84afc29b
SF
133 return rc;
134
ffdd6e4d
SF
135 spin_lock(&GlobalMid_Lock);
136 cifs_pdu->Signature.Sequence.SequenceNumber =
84afc29b 137 cpu_to_le32(server->sequence_number);
ffdd6e4d 138 cifs_pdu->Signature.Sequence.Reserved = 0;
84afc29b 139
ffdd6e4d
SF
140 *pexpected_response_sequence_number = server->sequence_number++;
141 server->sequence_number++;
142 spin_unlock(&GlobalMid_Lock);
84afc29b 143
ffdd6e4d 144 rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key,
84afc29b 145 smb_signature);
ffdd6e4d
SF
146 if (rc)
147 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
148 else
149 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
84afc29b 150
ffdd6e4d 151 return rc;
84afc29b
SF
152}
153
ffdd6e4d
SF
154int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key,
155 __u32 expected_sequence_number)
1da177e4
LT
156{
157 unsigned int rc;
158 char server_response_sig[8];
159 char what_we_think_sig_should_be[20];
160
ffdd6e4d 161 if ((cifs_pdu == NULL) || (mac_key == NULL))
1da177e4
LT
162 return -EINVAL;
163
164 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
165 return 0;
166
167 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
ffdd6e4d
SF
168 struct smb_com_lock_req * pSMB =
169 (struct smb_com_lock_req *)cifs_pdu;
170 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
1da177e4
LT
171 return 0;
172 }
173
174 /* BB what if signatures are supposed to be on for session but server does not
175 send one? BB */
176
177 /* Do not need to verify session setups with signature "BSRSPYL " */
178 if(memcmp(cifs_pdu->Signature.SecuritySignature,"BSRSPYL ",8)==0)
179 cFYI(1,("dummy signature received for smb command 0x%x",cifs_pdu->Command));
180
181 /* save off the origiginal signature so we can modify the smb and check
182 its signature against what the server sent */
183 memcpy(server_response_sig,cifs_pdu->Signature.SecuritySignature,8);
184
185 cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(expected_sequence_number);
186 cifs_pdu->Signature.Sequence.Reserved = 0;
187
188 rc = cifs_calculate_signature(cifs_pdu, mac_key,
189 what_we_think_sig_should_be);
190
191 if(rc)
192 return rc;
193
194
195/* cifs_dump_mem("what we think it should be: ",what_we_think_sig_should_be,16); */
196
197 if(memcmp(server_response_sig, what_we_think_sig_should_be, 8))
198 return -EACCES;
199 else
200 return 0;
201
202}
203
204/* We fill in key by putting in 40 byte array which was allocated by caller */
205int cifs_calculate_mac_key(char * key, const char * rn, const char * password)
206{
207 char temp_key[16];
208 if ((key == NULL) || (rn == NULL))
209 return -EINVAL;
210
211 E_md4hash(password, temp_key);
212 mdfour(key,temp_key,16);
7c7b25bc 213 memcpy(key+16,rn, CIFS_SESS_KEY_SIZE);
1da177e4
LT
214 return 0;
215}
216
1717ffc5
SF
217int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses,
218 const struct nls_table * nls_info)
1da177e4
LT
219{
220 char temp_hash[16];
221 struct HMACMD5Context ctx;
222 char * ucase_buf;
e89dc920 223 __le16 * unicode_buf;
1da177e4
LT
224 unsigned int i,user_name_len,dom_name_len;
225
226 if(ses == NULL)
227 return -EINVAL;
228
229 E_md4hash(ses->password, temp_hash);
230
231 hmac_md5_init_limK_to_64(temp_hash, 16, &ctx);
232 user_name_len = strlen(ses->userName);
233 if(user_name_len > MAX_USERNAME_SIZE)
234 return -EINVAL;
3979877e
SF
235 if(ses->domainName == NULL)
236 return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
1da177e4
LT
237 dom_name_len = strlen(ses->domainName);
238 if(dom_name_len > MAX_USERNAME_SIZE)
239 return -EINVAL;
240
241 ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
242 if(ucase_buf == NULL)
243 return -ENOMEM;
244 unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
245 if(unicode_buf == NULL) {
246 kfree(ucase_buf);
247 return -ENOMEM;
248 }
249
250 for(i=0;i<user_name_len;i++)
251 ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
252 ucase_buf[i] = 0;
253 user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
254 unicode_buf[user_name_len] = 0;
255 user_name_len++;
256
257 for(i=0;i<dom_name_len;i++)
258 ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
259 ucase_buf[i] = 0;
260 dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
261
262 unicode_buf[user_name_len + dom_name_len] = 0;
263 hmac_md5_update((const unsigned char *) unicode_buf,
264 (user_name_len+dom_name_len)*2,&ctx);
265
ad009ac9 266 hmac_md5_final(ses->server->mac_signing_key,&ctx);
1da177e4
LT
267 kfree(ucase_buf);
268 kfree(unicode_buf);
269 return 0;
270}
7c7b25bc
SF
271
272#ifdef CONFIG_CIFS_WEAK_PW_HASH
273void calc_lanman_hash(struct cifsSesInfo * ses, char * lnm_session_key)
274{
275 int i;
276 char password_with_pad[CIFS_ENCPWD_SIZE];
277
bdc4bf6e
SF
278 if(ses->server == NULL)
279 return;
280
7c7b25bc 281 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
66abda5e
SF
282 if(ses->password)
283 strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE);
7c7b25bc 284
bdc4bf6e
SF
285 if((ses->server->secMode & SECMODE_PW_ENCRYPT) == 0)
286 if(extended_security & CIFSSEC_MAY_PLNTXT) {
287 memcpy(lnm_session_key, password_with_pad, CIFS_ENCPWD_SIZE);
288 return;
289 }
290
7c7b25bc
SF
291 /* calculate old style session key */
292 /* calling toupper is less broken than repeatedly
293 calling nls_toupper would be since that will never
294 work for UTF8, but neither handles multibyte code pages
295 but the only alternative would be converting to UCS-16 (Unicode)
296 (using a routine something like UniStrupr) then
297 uppercasing and then converting back from Unicode - which
298 would only worth doing it if we knew it were utf8. Basically
299 utf8 and other multibyte codepages each need their own strupper
300 function since a byte at a time will ont work. */
301
302 for(i = 0; i < CIFS_ENCPWD_SIZE; i++) {
303 password_with_pad[i] = toupper(password_with_pad[i]);
304 }
305
306 SMBencrypt(password_with_pad, ses->server->cryptKey, lnm_session_key);
307 /* clear password before we return/free memory */
308 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
309}
310#endif /* CIFS_WEAK_PW_HASH */
311
1717ffc5
SF
312static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
313 const struct nls_table * nls_cp)
a8ee0344
SF
314{
315 int rc = 0;
316 int len;
317 char nt_hash[16];
318 struct HMACMD5Context * pctxt;
1717ffc5
SF
319 wchar_t * user;
320 wchar_t * domain;
a8ee0344
SF
321
322 pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL);
323
324 if(pctxt == NULL)
325 return -ENOMEM;
326
327 /* calculate md4 hash of password */
328 E_md4hash(ses->password, nt_hash);
329
1717ffc5 330 /* convert Domainname to unicode and uppercase */
a8ee0344
SF
331 hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
332
333 /* convert ses->userName to unicode and uppercase */
1717ffc5
SF
334 len = strlen(ses->userName);
335 user = kmalloc(2 + (len * 2), GFP_KERNEL);
336 if(user == NULL)
337 goto calc_exit_2;
338 len = cifs_strtoUCS(user, ses->userName, len, nls_cp);
339 UniStrupr(user);
340 hmac_md5_update((char *)user, 2*len, pctxt);
a8ee0344
SF
341
342 /* convert ses->domainName to unicode and uppercase */
1717ffc5
SF
343 if(ses->domainName) {
344 len = strlen(ses->domainName);
a8ee0344 345
1717ffc5
SF
346 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
347 if(domain == NULL)
348 goto calc_exit_1;
349 len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp);
350 UniStrupr(domain);
a8ee0344 351
1717ffc5
SF
352 hmac_md5_update((char *)domain, 2*len, pctxt);
353
354 kfree(domain);
355 }
356calc_exit_1:
357 kfree(user);
358calc_exit_2:
359 /* BB FIXME what about bytes 24 through 40 of the signing key?
360 compare with the NTLM example */
361 hmac_md5_final(ses->server->mac_signing_key, pctxt);
a8ee0344
SF
362
363 return rc;
364}
365
1717ffc5
SF
366void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf,
367 const struct nls_table * nls_cp)
6d027cfd 368{
a8ee0344 369 int rc;
6d027cfd
SF
370 struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf;
371
372 buf->blob_signature = cpu_to_le32(0x00000101);
373 buf->reserved = 0;
374 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
1717ffc5 375 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
6d027cfd 376 buf->reserved2 = 0;
33ec32fa 377 buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
6d027cfd 378 buf->names[0].length = 0;
33ec32fa
SF
379 buf->names[1].type = 0;
380 buf->names[1].length = 0;
a8ee0344 381
6d027cfd 382 /* calculate buf->ntlmv2_hash */
1717ffc5 383 rc = calc_ntlmv2_hash(ses, nls_cp);
a8ee0344
SF
384 if(rc)
385 cERROR(1,("could not get v2 hash rc %d",rc));
1717ffc5 386 CalcNTLMv2_response(ses, resp_buf);
6d027cfd
SF
387}
388
1717ffc5 389void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response)
1da177e4
LT
390{
391 struct HMACMD5Context context;
1717ffc5 392 /* rest of v2 struct already generated */
1da177e4 393 memcpy(v2_session_response + 8, ses->server->cryptKey,8);
ad009ac9 394 hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
1da177e4 395
1717ffc5
SF
396 hmac_md5_update(v2_session_response+8,
397 sizeof(struct ntlmv2_resp) - 8, &context);
1da177e4
LT
398
399 hmac_md5_final(v2_session_response,&context);
1717ffc5 400/* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
1da177e4 401}