blob: e11d8c6bb2270484a2ed5651c838786c2d09f6bf [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"
24#include "cifsglob.h"
25#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Calculate and return the CIFS signature based on the mac key and the smb pdu */
32/* the 16 byte signature must be allocated by the caller */
33/* Note we only use the 1st eight bytes */
34/* Note that the smb header signature field on input contains the
35 sequence number before this function is called */
36
37extern void mdfour(unsigned char *out, unsigned char *in, int n);
38extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
Steve French7c7b25b2006-06-01 19:20:10 +000039extern void SMBencrypt(unsigned char *passwd, unsigned char *c8,
40 unsigned char *p24);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Steve French12b3b8f2006-02-09 21:12:47 +000042static int cifs_calculate_signature(const struct smb_hdr * cifs_pdu,
43 const char * key, char * signature)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct MD5Context context;
46
47 if((cifs_pdu == NULL) || (signature == NULL))
48 return -EINVAL;
49
50 MD5Init(&context);
Steve French7c7b25b2006-06-01 19:20:10 +000051 MD5Update(&context,key,CIFS_SESS_KEY_SIZE+16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 MD5Update(&context,cifs_pdu->Protocol,cifs_pdu->smb_buf_length);
53 MD5Final(signature,&context);
54 return 0;
55}
56
Steve Frenchad009ac2005-04-28 22:41:05 -070057int cifs_sign_smb(struct smb_hdr * cifs_pdu, struct TCP_Server_Info * server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 __u32 * pexpected_response_sequence_number)
59{
60 int rc = 0;
61 char smb_signature[20];
62
Steve Frenchad009ac2005-04-28 22:41:05 -070063 if((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return -EINVAL;
65
66 if((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
67 return rc;
68
69 spin_lock(&GlobalMid_Lock);
Steve Frenchad009ac2005-04-28 22:41:05 -070070 cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(server->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cifs_pdu->Signature.Sequence.Reserved = 0;
72
Steve Frenchad009ac2005-04-28 22:41:05 -070073 *pexpected_response_sequence_number = server->sequence_number++;
74 server->sequence_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 spin_unlock(&GlobalMid_Lock);
76
Steve Frenchad009ac2005-04-28 22:41:05 -070077 rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,smb_signature);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if(rc)
79 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
80 else
81 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
82
83 return rc;
84}
85
Steve French84afc292005-12-02 13:32:45 -080086static int cifs_calc_signature2(const struct kvec * iov, int n_vec,
87 const char * key, char * signature)
88{
Steve Frenche9917a02006-03-31 21:22:00 +000089 struct MD5Context context;
90 int i;
Steve French84afc292005-12-02 13:32:45 -080091
Steve Frenche9917a02006-03-31 21:22:00 +000092 if((iov == NULL) || (signature == NULL))
93 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -080094
Steve Frenche9917a02006-03-31 21:22:00 +000095 MD5Init(&context);
Steve French7c7b25b2006-06-01 19:20:10 +000096 MD5Update(&context,key,CIFS_SESS_KEY_SIZE+16);
Steve Frenche9917a02006-03-31 21:22:00 +000097 for(i=0;i<n_vec;i++) {
98 if(iov[i].iov_base == NULL) {
99 cERROR(1,("null iovec entry"));
100 return -EIO;
101 } else if(iov[i].iov_len == 0)
102 break; /* bail out if we are sent nothing to sign */
103 /* The first entry includes a length field (which does not get
104 signed that occupies the first 4 bytes before the header */
105 if(i==0) {
106 if (iov[0].iov_len <= 8 ) /* cmd field at offset 9 */
107 break; /* nothing to sign or corrupt header */
108 MD5Update(&context,iov[0].iov_base+4, iov[0].iov_len-4);
109 } else
110 MD5Update(&context,iov[i].iov_base, iov[i].iov_len);
111 }
Steve French84afc292005-12-02 13:32:45 -0800112
Steve Frenche9917a02006-03-31 21:22:00 +0000113 MD5Final(signature,&context);
Steve French84afc292005-12-02 13:32:45 -0800114
Steve Frenche9917a02006-03-31 21:22:00 +0000115 return 0;
Steve French84afc292005-12-02 13:32:45 -0800116}
117
118
119int cifs_sign_smb2(struct kvec * iov, int n_vec, struct TCP_Server_Info *server,
120 __u32 * pexpected_response_sequence_number)
121{
122 int rc = 0;
123 char smb_signature[20];
124 struct smb_hdr * cifs_pdu = iov[0].iov_base;
125
126 if((cifs_pdu == NULL) || (server == NULL))
127 return -EINVAL;
128
129 if((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
130 return rc;
131
132 spin_lock(&GlobalMid_Lock);
133 cifs_pdu->Signature.Sequence.SequenceNumber =
134 cpu_to_le32(server->sequence_number);
135 cifs_pdu->Signature.Sequence.Reserved = 0;
136
137 *pexpected_response_sequence_number = server->sequence_number++;
138 server->sequence_number++;
139 spin_unlock(&GlobalMid_Lock);
140
141 rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key,
142 smb_signature);
143 if(rc)
144 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
145 else
146 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
147
148 return rc;
149
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152int cifs_verify_signature(struct smb_hdr * cifs_pdu, const char * mac_key,
153 __u32 expected_sequence_number)
154{
155 unsigned int rc;
156 char server_response_sig[8];
157 char what_we_think_sig_should_be[20];
158
159 if((cifs_pdu == NULL) || (mac_key == NULL))
160 return -EINVAL;
161
162 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
163 return 0;
164
165 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
166 struct smb_com_lock_req * pSMB = (struct smb_com_lock_req *)cifs_pdu;
167 if(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
168 return 0;
169 }
170
171 /* BB what if signatures are supposed to be on for session but server does not
172 send one? BB */
173
174 /* Do not need to verify session setups with signature "BSRSPYL " */
175 if(memcmp(cifs_pdu->Signature.SecuritySignature,"BSRSPYL ",8)==0)
176 cFYI(1,("dummy signature received for smb command 0x%x",cifs_pdu->Command));
177
178 /* save off the origiginal signature so we can modify the smb and check
179 its signature against what the server sent */
180 memcpy(server_response_sig,cifs_pdu->Signature.SecuritySignature,8);
181
182 cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(expected_sequence_number);
183 cifs_pdu->Signature.Sequence.Reserved = 0;
184
185 rc = cifs_calculate_signature(cifs_pdu, mac_key,
186 what_we_think_sig_should_be);
187
188 if(rc)
189 return rc;
190
191
192/* cifs_dump_mem("what we think it should be: ",what_we_think_sig_should_be,16); */
193
194 if(memcmp(server_response_sig, what_we_think_sig_should_be, 8))
195 return -EACCES;
196 else
197 return 0;
198
199}
200
201/* We fill in key by putting in 40 byte array which was allocated by caller */
202int cifs_calculate_mac_key(char * key, const char * rn, const char * password)
203{
204 char temp_key[16];
205 if ((key == NULL) || (rn == NULL))
206 return -EINVAL;
207
208 E_md4hash(password, temp_key);
209 mdfour(key,temp_key,16);
Steve French7c7b25b2006-06-01 19:20:10 +0000210 memcpy(key+16,rn, CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
214int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, struct nls_table * nls_info)
215{
216 char temp_hash[16];
217 struct HMACMD5Context ctx;
218 char * ucase_buf;
Steve Frenche89dc922005-11-11 15:18:19 -0800219 __le16 * unicode_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned int i,user_name_len,dom_name_len;
221
222 if(ses == NULL)
223 return -EINVAL;
224
225 E_md4hash(ses->password, temp_hash);
226
227 hmac_md5_init_limK_to_64(temp_hash, 16, &ctx);
228 user_name_len = strlen(ses->userName);
229 if(user_name_len > MAX_USERNAME_SIZE)
230 return -EINVAL;
Steve French39798772006-05-31 22:40:51 +0000231 if(ses->domainName == NULL)
232 return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 dom_name_len = strlen(ses->domainName);
234 if(dom_name_len > MAX_USERNAME_SIZE)
235 return -EINVAL;
236
237 ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
238 if(ucase_buf == NULL)
239 return -ENOMEM;
240 unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
241 if(unicode_buf == NULL) {
242 kfree(ucase_buf);
243 return -ENOMEM;
244 }
245
246 for(i=0;i<user_name_len;i++)
247 ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
248 ucase_buf[i] = 0;
249 user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
250 unicode_buf[user_name_len] = 0;
251 user_name_len++;
252
253 for(i=0;i<dom_name_len;i++)
254 ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
255 ucase_buf[i] = 0;
256 dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info);
257
258 unicode_buf[user_name_len + dom_name_len] = 0;
259 hmac_md5_update((const unsigned char *) unicode_buf,
260 (user_name_len+dom_name_len)*2,&ctx);
261
Steve Frenchad009ac2005-04-28 22:41:05 -0700262 hmac_md5_final(ses->server->mac_signing_key,&ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(ucase_buf);
264 kfree(unicode_buf);
265 return 0;
266}
Steve French7c7b25b2006-06-01 19:20:10 +0000267
268#ifdef CONFIG_CIFS_WEAK_PW_HASH
269void calc_lanman_hash(struct cifsSesInfo * ses, char * lnm_session_key)
270{
271 int i;
272 char password_with_pad[CIFS_ENCPWD_SIZE];
273
274 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
275 strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE);
276
277 /* calculate old style session key */
278 /* calling toupper is less broken than repeatedly
279 calling nls_toupper would be since that will never
280 work for UTF8, but neither handles multibyte code pages
281 but the only alternative would be converting to UCS-16 (Unicode)
282 (using a routine something like UniStrupr) then
283 uppercasing and then converting back from Unicode - which
284 would only worth doing it if we knew it were utf8. Basically
285 utf8 and other multibyte codepages each need their own strupper
286 function since a byte at a time will ont work. */
287
288 for(i = 0; i < CIFS_ENCPWD_SIZE; i++) {
289 password_with_pad[i] = toupper(password_with_pad[i]);
290 }
291
292 SMBencrypt(password_with_pad, ses->server->cryptKey, lnm_session_key);
293 /* clear password before we return/free memory */
294 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
295}
296#endif /* CIFS_WEAK_PW_HASH */
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298void CalcNTLMv2_response(const struct cifsSesInfo * ses,char * v2_session_response)
299{
300 struct HMACMD5Context context;
301 memcpy(v2_session_response + 8, ses->server->cryptKey,8);
302 /* gen_blob(v2_session_response + 16); */
Steve Frenchad009ac2005-04-28 22:41:05 -0700303 hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 hmac_md5_update(ses->server->cryptKey,8,&context);
306/* hmac_md5_update(v2_session_response+16)client thing,8,&context); */ /* BB fix */
307
308 hmac_md5_final(v2_session_response,&context);
Steve French5815449d2006-02-14 01:36:20 +0000309 cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); /* BB removeme BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}