3ae964bbfdc3f61490d8faf805635f491daf7cda
[safe/jmp/linux-2.6] / fs / cifs / cifsencrypt.c
1 /*
2  *   fs/cifs/cifsencrypt.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2005,2006
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"
24 #include "cifsglob.h" 
25 #include "cifs_debug.h"
26 #include "md5.h"
27 #include "cifs_unicode.h"
28 #include "cifsproto.h"
29 #include <linux/ctype.h>
30
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
37 extern void mdfour(unsigned char *out, unsigned char *in, int n);
38 extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
39 extern void SMBencrypt(unsigned char *passwd, unsigned char *c8,
40                        unsigned char *p24);
41         
42 static int cifs_calculate_signature(const struct smb_hdr * cifs_pdu, 
43                                     const char * key, char * signature)
44 {
45         struct  MD5Context context;
46
47         if((cifs_pdu == NULL) || (signature == NULL))
48                 return -EINVAL;
49
50         MD5Init(&context);
51         MD5Update(&context,key,CIFS_SESS_KEY_SIZE+16);
52         MD5Update(&context,cifs_pdu->Protocol,cifs_pdu->smb_buf_length);
53         MD5Final(signature,&context);
54         return 0;
55 }
56
57 int cifs_sign_smb(struct smb_hdr * cifs_pdu, struct TCP_Server_Info * server,
58         __u32 * pexpected_response_sequence_number)
59 {
60         int rc = 0;
61         char smb_signature[20];
62
63         if((cifs_pdu == NULL) || (server == NULL))
64                 return -EINVAL;
65
66         if((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) 
67                 return rc;
68
69         spin_lock(&GlobalMid_Lock);
70         cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(server->sequence_number);
71         cifs_pdu->Signature.Sequence.Reserved = 0;
72         
73         *pexpected_response_sequence_number = server->sequence_number++;
74         server->sequence_number++;
75         spin_unlock(&GlobalMid_Lock);
76
77         rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key,smb_signature);
78         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
86 static int cifs_calc_signature2(const struct kvec * iov, int n_vec,
87                                 const char * key, char * signature)
88 {
89         struct  MD5Context context;
90         int i;
91
92         if((iov == NULL) || (signature == NULL))
93                 return -EINVAL;
94
95         MD5Init(&context);
96         MD5Update(&context,key,CIFS_SESS_KEY_SIZE+16);
97         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         }
112
113         MD5Final(signature,&context);
114
115         return 0;
116 }
117
118
119 int 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
152 int 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 */
202 int 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);
210         memcpy(key+16,rn, CIFS_SESS_KEY_SIZE);
211         return 0;
212 }
213
214 int 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;
219         __le16 * unicode_buf;
220         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;
231         if(ses->domainName == NULL)
232                 return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
233         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
262         hmac_md5_final(ses->server->mac_signing_key,&ctx);
263         kfree(ucase_buf);
264         kfree(unicode_buf);
265         return 0;
266 }
267
268 #ifdef CONFIG_CIFS_WEAK_PW_HASH
269 void calc_lanman_hash(struct cifsSesInfo * ses, char * lnm_session_key)
270 {
271         int i;
272         char password_with_pad[CIFS_ENCPWD_SIZE];
273
274         if(ses->server == NULL)
275                 return;
276
277         memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
278         strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE);
279
280         if((ses->server->secMode & SECMODE_PW_ENCRYPT) == 0)
281                 if(extended_security & CIFSSEC_MAY_PLNTXT) {
282                         memcpy(lnm_session_key, password_with_pad, CIFS_ENCPWD_SIZE); 
283                         return;
284                 }
285
286         /* calculate old style session key */
287         /* calling toupper is less broken than repeatedly
288         calling nls_toupper would be since that will never
289         work for UTF8, but neither handles multibyte code pages
290         but the only alternative would be converting to UCS-16 (Unicode)
291         (using a routine something like UniStrupr) then
292         uppercasing and then converting back from Unicode - which
293         would only worth doing it if we knew it were utf8. Basically
294         utf8 and other multibyte codepages each need their own strupper
295         function since a byte at a time will ont work. */
296
297         for(i = 0; i < CIFS_ENCPWD_SIZE; i++) {
298                 password_with_pad[i] = toupper(password_with_pad[i]);
299         }
300
301         SMBencrypt(password_with_pad, ses->server->cryptKey, lnm_session_key);
302         /* clear password before we return/free memory */
303         memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
304 }
305 #endif /* CIFS_WEAK_PW_HASH */
306
307 void CalcNTLMv2_response(const struct cifsSesInfo * ses,char * v2_session_response)
308 {
309         struct HMACMD5Context context;
310         memcpy(v2_session_response + 8, ses->server->cryptKey,8);
311         /* gen_blob(v2_session_response + 16); */
312         hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context);
313
314         hmac_md5_update(ses->server->cryptKey,8,&context);
315 /*      hmac_md5_update(v2_session_response+16)client thing,8,&context); */ /* BB fix */
316
317         hmac_md5_final(v2_session_response,&context);
318         cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); /* BB removeme BB */
319 }