blob: 8422df81b994164ef1d129e9eb5ed41777f56f4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsencrypt.c
3 *
Steve French12b3b8f2006-02-09 21:12:47 +00004 * Copyright (C) International Business Machines Corp., 2005,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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"
Steve Frenchffdd6e42007-06-24 21:15:44 +000024#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "cifs_debug.h"
26#include "md5.h"
27#include "cifs_unicode.h"
28#include "cifsproto.h"
Steve French7c7b25b2006-06-01 19:20:10 +000029#include <linux/ctype.h>
Steve French6d027cf2006-06-05 16:26:05 +000030#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Steve Frenchffdd6e42007-06-24 21:15:44 +000032/* Calculate and return the CIFS signature based on the mac key and SMB PDU */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* the 16 byte signature must be allocated by the caller */
34/* Note we only use the 1st eight bytes */
Steve Frenchffdd6e42007-06-24 21:15:44 +000035/* Note that the smb header signature field on input contains the
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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);
Steve French7c7b25b2006-06-01 19:20:10 +000040extern void SMBencrypt(unsigned char *passwd, unsigned char *c8,
Steve Frenchffdd6e42007-06-24 21:15:44 +000041 unsigned char *p24);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Steve Frenchffdd6e42007-06-24 21:15:44 +000043static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
44 const char *key, char *signature)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct MD5Context context;
47
Steve Frenchffdd6e42007-06-24 21:15:44 +000048 if ((cifs_pdu == NULL) || (signature == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return -EINVAL;
50
51 MD5Init(&context);
Steve Frenchffdd6e42007-06-24 21:15:44 +000052 MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16);
53 MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
54 MD5Final(signature, &context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return 0;
56}
57
Steve Frenchffdd6e42007-06-24 21:15:44 +000058int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
59 __u32 *pexpected_response_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 int rc = 0;
62 char smb_signature[20];
63
Steve Frenchffdd6e42007-06-24 21:15:44 +000064 if ((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EINVAL;
66
Steve Frenchffdd6e42007-06-24 21:15:44 +000067 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return rc;
69
70 spin_lock(&GlobalMid_Lock);
Steve Frenchffdd6e42007-06-24 21:15:44 +000071 cifs_pdu->Signature.Sequence.SequenceNumber =
72 cpu_to_le32(server->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 cifs_pdu->Signature.Sequence.Reserved = 0;
74
Steve Frenchad009ac2005-04-28 22:41:05 -070075 *pexpected_response_sequence_number = server->sequence_number++;
76 server->sequence_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 spin_unlock(&GlobalMid_Lock);
78
Steve Frenchffdd6e42007-06-24 21:15:44 +000079 rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,
80 smb_signature);
81 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
83 else
84 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
85
86 return rc;
87}
88
Steve Frenchffdd6e42007-06-24 21:15:44 +000089static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
90 const char *key, char *signature)
Steve French84afc292005-12-02 13:32:45 -080091{
Steve Frenche9917a02006-03-31 21:22:00 +000092 struct MD5Context context;
93 int i;
Steve French84afc292005-12-02 13:32:45 -080094
Steve Frenchffdd6e42007-06-24 21:15:44 +000095 if ((iov == NULL) || (signature == NULL))
Steve Frenche9917a02006-03-31 21:22:00 +000096 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -080097
Steve Frenche9917a02006-03-31 21:22:00 +000098 MD5Init(&context);
Steve Frenchffdd6e42007-06-24 21:15:44 +000099 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"));
Steve Frenche9917a02006-03-31 21:22:00 +0000103 return -EIO;
Steve Frenchffdd6e42007-06-24 21:15:44 +0000104 } else if (iov[i].iov_len == 0)
Steve Frenche9917a02006-03-31 21:22:00 +0000105 break; /* bail out if we are sent nothing to sign */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000106 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +0000107 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000108 if (i == 0) {
Steve Frenche9917a02006-03-31 21:22:00 +0000109 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
Steve Frenchffdd6e42007-06-24 21:15:44 +0000113 MD5Update(&context, iov[i].iov_base, iov[i].iov_len);
Steve Frenche9917a02006-03-31 21:22:00 +0000114 }
Steve French84afc292005-12-02 13:32:45 -0800115
Steve Frenchffdd6e42007-06-24 21:15:44 +0000116 MD5Final(signature, &context);
Steve French84afc292005-12-02 13:32:45 -0800117
Steve Frenche9917a02006-03-31 21:22:00 +0000118 return 0;
Steve French84afc292005-12-02 13:32:45 -0800119}
120
121
Steve Frenchffdd6e42007-06-24 21:15:44 +0000122int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
Steve French84afc292005-12-02 13:32:45 -0800123 __u32 * pexpected_response_sequence_number)
124{
125 int rc = 0;
126 char smb_signature[20];
Steve Frenchffdd6e42007-06-24 21:15:44 +0000127 struct smb_hdr *cifs_pdu = iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800128
Steve Frenchffdd6e42007-06-24 21:15:44 +0000129 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800130 return -EINVAL;
131
Steve Frenchffdd6e42007-06-24 21:15:44 +0000132 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Steve French84afc292005-12-02 13:32:45 -0800133 return rc;
134
Steve Frenchffdd6e42007-06-24 21:15:44 +0000135 spin_lock(&GlobalMid_Lock);
136 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800137 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000138 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800139
Steve Frenchffdd6e42007-06-24 21:15:44 +0000140 *pexpected_response_sequence_number = server->sequence_number++;
141 server->sequence_number++;
142 spin_unlock(&GlobalMid_Lock);
Steve French84afc292005-12-02 13:32:45 -0800143
Steve Frenchffdd6e42007-06-24 21:15:44 +0000144 rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key,
Steve French84afc292005-12-02 13:32:45 -0800145 smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000146 if (rc)
147 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
148 else
149 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800150
Steve Frenchffdd6e42007-06-24 21:15:44 +0000151 return rc;
Steve French84afc292005-12-02 13:32:45 -0800152}
153
Steve Frenchffdd6e42007-06-24 21:15:44 +0000154int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key,
155 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 unsigned int rc;
158 char server_response_sig[8];
159 char what_we_think_sig_should_be[20];
160
Steve Frenchffdd6e42007-06-24 21:15:44 +0000161 if ((cifs_pdu == NULL) || (mac_key == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EINVAL;
163
164 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
165 return 0;
166
167 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve Frenchffdd6e42007-06-24 21:15:44 +0000168 struct smb_com_lock_req * pSMB =
169 (struct smb_com_lock_req *)cifs_pdu;
170 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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);
Steve French7c7b25b2006-06-01 19:20:10 +0000213 memcpy(key+16,rn, CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
Steve French1717ffc2006-06-08 05:41:32 +0000217int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses,
218 const struct nls_table * nls_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 char temp_hash[16];
221 struct HMACMD5Context ctx;
222 char * ucase_buf;
Steve Frenche89dc922005-11-11 15:18:19 -0800223 __le16 * unicode_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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;
Steve French39798772006-05-31 22:40:51 +0000235 if(ses->domainName == NULL)
236 return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 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
Steve Frenchad009ac2005-04-28 22:41:05 -0700266 hmac_md5_final(ses->server->mac_signing_key,&ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 kfree(ucase_buf);
268 kfree(unicode_buf);
269 return 0;
270}
Steve French7c7b25b2006-06-01 19:20:10 +0000271
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
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000278 if(ses->server == NULL)
279 return;
280
Steve French7c7b25b2006-06-01 19:20:10 +0000281 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Steve French66abda52006-08-11 16:52:09 +0000282 if(ses->password)
283 strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000284
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000285 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
Steve French7c7b25b2006-06-01 19:20:10 +0000291 /* 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
Steve French1717ffc2006-06-08 05:41:32 +0000312static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
313 const struct nls_table * nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000314{
315 int rc = 0;
316 int len;
317 char nt_hash[16];
318 struct HMACMD5Context * pctxt;
Steve French1717ffc2006-06-08 05:41:32 +0000319 wchar_t * user;
320 wchar_t * domain;
Steve Frencha8ee0342006-06-05 23:34:19 +0000321
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
Steve French1717ffc2006-06-08 05:41:32 +0000330 /* convert Domainname to unicode and uppercase */
Steve Frencha8ee0342006-06-05 23:34:19 +0000331 hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
332
333 /* convert ses->userName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000334 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);
Steve Frencha8ee0342006-06-05 23:34:19 +0000341
342 /* convert ses->domainName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000343 if(ses->domainName) {
344 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000345
Steve French1717ffc2006-06-08 05:41:32 +0000346 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);
Steve Frencha8ee0342006-06-05 23:34:19 +0000351
Steve French1717ffc2006-06-08 05:41:32 +0000352 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);
Steve Frencha8ee0342006-06-05 23:34:19 +0000362
363 return rc;
364}
365
Steve French1717ffc2006-06-08 05:41:32 +0000366void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf,
367 const struct nls_table * nls_cp)
Steve French6d027cf2006-06-05 16:26:05 +0000368{
Steve Frencha8ee0342006-06-05 23:34:19 +0000369 int rc;
Steve French6d027cf2006-06-05 16:26:05 +0000370 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));
Steve French1717ffc2006-06-08 05:41:32 +0000375 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
Steve French6d027cf2006-06-05 16:26:05 +0000376 buf->reserved2 = 0;
Steve French33ec32f2006-12-08 04:14:28 +0000377 buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
Steve French6d027cf2006-06-05 16:26:05 +0000378 buf->names[0].length = 0;
Steve French33ec32f2006-12-08 04:14:28 +0000379 buf->names[1].type = 0;
380 buf->names[1].length = 0;
Steve Frencha8ee0342006-06-05 23:34:19 +0000381
Steve French6d027cf2006-06-05 16:26:05 +0000382 /* calculate buf->ntlmv2_hash */
Steve French1717ffc2006-06-08 05:41:32 +0000383 rc = calc_ntlmv2_hash(ses, nls_cp);
Steve Frencha8ee0342006-06-05 23:34:19 +0000384 if(rc)
385 cERROR(1,("could not get v2 hash rc %d",rc));
Steve French1717ffc2006-06-08 05:41:32 +0000386 CalcNTLMv2_response(ses, resp_buf);
Steve French6d027cf2006-06-05 16:26:05 +0000387}
388
Steve French1717ffc2006-06-08 05:41:32 +0000389void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct HMACMD5Context context;
Steve French1717ffc2006-06-08 05:41:32 +0000392 /* rest of v2 struct already generated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 memcpy(v2_session_response + 8, ses->server->cryptKey,8);
Steve Frenchad009ac2005-04-28 22:41:05 -0700394 hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Steve French1717ffc2006-06-08 05:41:32 +0000396 hmac_md5_update(v2_session_response+8,
397 sizeof(struct ntlmv2_resp) - 8, &context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 hmac_md5_final(v2_session_response,&context);
Steve French1717ffc2006-06-08 05:41:32 +0000400/* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}