Commit | Line | Data |
---|---|---|
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 | ||
38 | extern void mdfour(unsigned char *out, unsigned char *in, int n); | |
39 | extern void E_md4hash(const unsigned char *passwd, unsigned char *p16); | |
7c7b25bc | 40 | extern void SMBencrypt(unsigned char *passwd, unsigned char *c8, |
ffdd6e4d | 41 | unsigned char *p24); |
1da177e4 | 42 | |
ffdd6e4d | 43 | static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, |
b609f06a | 44 | const struct mac_key *key, char *signature) |
1da177e4 LT |
45 | { |
46 | struct MD5Context context; | |
47 | ||
b609f06a | 48 | if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL)) |
1da177e4 LT |
49 | return -EINVAL; |
50 | ||
51 | MD5Init(&context); | |
b609f06a | 52 | MD5Update(&context, (char *)&key->data, key->len); |
ffdd6e4d | 53 | MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length); |
b609f06a | 54 | |
ffdd6e4d | 55 | MD5Final(signature, &context); |
1da177e4 LT |
56 | return 0; |
57 | } | |
58 | ||
ffdd6e4d SF |
59 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
60 | __u32 *pexpected_response_sequence_number) | |
1da177e4 LT |
61 | { |
62 | int rc = 0; | |
63 | char smb_signature[20]; | |
64 | ||
ffdd6e4d | 65 | if ((cifs_pdu == NULL) || (server == NULL)) |
1da177e4 LT |
66 | return -EINVAL; |
67 | ||
ffdd6e4d | 68 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
1da177e4 LT |
69 | return rc; |
70 | ||
71 | spin_lock(&GlobalMid_Lock); | |
ffdd6e4d SF |
72 | cifs_pdu->Signature.Sequence.SequenceNumber = |
73 | cpu_to_le32(server->sequence_number); | |
1da177e4 LT |
74 | cifs_pdu->Signature.Sequence.Reserved = 0; |
75 | ||
ad009ac9 SF |
76 | *pexpected_response_sequence_number = server->sequence_number++; |
77 | server->sequence_number++; | |
1da177e4 LT |
78 | spin_unlock(&GlobalMid_Lock); |
79 | ||
b609f06a | 80 | rc = cifs_calculate_signature(cifs_pdu, &server->mac_signing_key, |
ffdd6e4d SF |
81 | smb_signature); |
82 | if (rc) | |
1da177e4 LT |
83 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
84 | else | |
85 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); | |
86 | ||
87 | return rc; | |
88 | } | |
89 | ||
ffdd6e4d | 90 | static int cifs_calc_signature2(const struct kvec *iov, int n_vec, |
b609f06a | 91 | const struct mac_key *key, char *signature) |
84afc29b | 92 | { |
e9917a00 SF |
93 | struct MD5Context context; |
94 | int i; | |
84afc29b | 95 | |
b609f06a | 96 | if ((iov == NULL) || (signature == NULL) || (key == NULL)) |
e9917a00 | 97 | return -EINVAL; |
84afc29b | 98 | |
e9917a00 | 99 | MD5Init(&context); |
b609f06a | 100 | MD5Update(&context, (char *)&key->data, key->len); |
ffdd6e4d SF |
101 | for (i=0;i<n_vec;i++) { |
102 | if (iov[i].iov_base == NULL) { | |
103 | cERROR(1 ,("null iovec entry")); | |
e9917a00 | 104 | return -EIO; |
ffdd6e4d | 105 | } else if (iov[i].iov_len == 0) |
e9917a00 | 106 | break; /* bail out if we are sent nothing to sign */ |
ffdd6e4d | 107 | /* The first entry includes a length field (which does not get |
e9917a00 | 108 | signed that occupies the first 4 bytes before the header */ |
ffdd6e4d | 109 | if (i == 0) { |
e9917a00 SF |
110 | if (iov[0].iov_len <= 8 ) /* cmd field at offset 9 */ |
111 | break; /* nothing to sign or corrupt header */ | |
112 | MD5Update(&context,iov[0].iov_base+4, iov[0].iov_len-4); | |
113 | } else | |
ffdd6e4d | 114 | MD5Update(&context, iov[i].iov_base, iov[i].iov_len); |
e9917a00 | 115 | } |
84afc29b | 116 | |
ffdd6e4d | 117 | MD5Final(signature, &context); |
84afc29b | 118 | |
e9917a00 | 119 | return 0; |
84afc29b SF |
120 | } |
121 | ||
122 | ||
ffdd6e4d | 123 | int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, |
84afc29b SF |
124 | __u32 * pexpected_response_sequence_number) |
125 | { | |
126 | int rc = 0; | |
127 | char smb_signature[20]; | |
ffdd6e4d | 128 | struct smb_hdr *cifs_pdu = iov[0].iov_base; |
84afc29b | 129 | |
ffdd6e4d | 130 | if ((cifs_pdu == NULL) || (server == NULL)) |
84afc29b SF |
131 | return -EINVAL; |
132 | ||
ffdd6e4d | 133 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
84afc29b SF |
134 | return rc; |
135 | ||
ffdd6e4d SF |
136 | spin_lock(&GlobalMid_Lock); |
137 | cifs_pdu->Signature.Sequence.SequenceNumber = | |
84afc29b | 138 | cpu_to_le32(server->sequence_number); |
ffdd6e4d | 139 | cifs_pdu->Signature.Sequence.Reserved = 0; |
84afc29b | 140 | |
ffdd6e4d SF |
141 | *pexpected_response_sequence_number = server->sequence_number++; |
142 | server->sequence_number++; | |
143 | spin_unlock(&GlobalMid_Lock); | |
84afc29b | 144 | |
b609f06a | 145 | rc = cifs_calc_signature2(iov, n_vec, &server->mac_signing_key, |
84afc29b | 146 | smb_signature); |
ffdd6e4d SF |
147 | if (rc) |
148 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); | |
149 | else | |
150 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); | |
84afc29b | 151 | |
ffdd6e4d | 152 | return rc; |
84afc29b SF |
153 | } |
154 | ||
b609f06a SF |
155 | int cifs_verify_signature(struct smb_hdr *cifs_pdu, |
156 | const struct mac_key *mac_key, | |
ffdd6e4d | 157 | __u32 expected_sequence_number) |
1da177e4 LT |
158 | { |
159 | unsigned int rc; | |
160 | char server_response_sig[8]; | |
161 | char what_we_think_sig_should_be[20]; | |
162 | ||
ffdd6e4d | 163 | if ((cifs_pdu == NULL) || (mac_key == NULL)) |
1da177e4 LT |
164 | return -EINVAL; |
165 | ||
166 | if (cifs_pdu->Command == SMB_COM_NEGOTIATE) | |
167 | return 0; | |
168 | ||
169 | if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) { | |
ffdd6e4d SF |
170 | struct smb_com_lock_req * pSMB = |
171 | (struct smb_com_lock_req *)cifs_pdu; | |
172 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) | |
1da177e4 LT |
173 | return 0; |
174 | } | |
175 | ||
176 | /* BB what if signatures are supposed to be on for session but server does not | |
177 | send one? BB */ | |
178 | ||
179 | /* Do not need to verify session setups with signature "BSRSPYL " */ | |
180 | if(memcmp(cifs_pdu->Signature.SecuritySignature,"BSRSPYL ",8)==0) | |
181 | cFYI(1,("dummy signature received for smb command 0x%x",cifs_pdu->Command)); | |
182 | ||
183 | /* save off the origiginal signature so we can modify the smb and check | |
184 | its signature against what the server sent */ | |
185 | memcpy(server_response_sig,cifs_pdu->Signature.SecuritySignature,8); | |
186 | ||
187 | cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(expected_sequence_number); | |
188 | cifs_pdu->Signature.Sequence.Reserved = 0; | |
189 | ||
190 | rc = cifs_calculate_signature(cifs_pdu, mac_key, | |
191 | what_we_think_sig_should_be); | |
192 | ||
193 | if(rc) | |
194 | return rc; | |
195 | ||
196 | ||
197 | /* cifs_dump_mem("what we think it should be: ",what_we_think_sig_should_be,16); */ | |
198 | ||
199 | if(memcmp(server_response_sig, what_we_think_sig_should_be, 8)) | |
200 | return -EACCES; | |
201 | else | |
202 | return 0; | |
203 | ||
204 | } | |
205 | ||
206 | /* We fill in key by putting in 40 byte array which was allocated by caller */ | |
b609f06a SF |
207 | int cifs_calculate_mac_key(struct mac_key *key, const char *rn, |
208 | const char *password) | |
1da177e4 LT |
209 | { |
210 | char temp_key[16]; | |
211 | if ((key == NULL) || (rn == NULL)) | |
212 | return -EINVAL; | |
213 | ||
214 | E_md4hash(password, temp_key); | |
b609f06a SF |
215 | mdfour(key->data.ntlm, temp_key, 16); |
216 | memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE); | |
217 | key->len = 40; | |
1da177e4 LT |
218 | return 0; |
219 | } | |
220 | ||
1717ffc5 SF |
221 | int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, |
222 | const struct nls_table * nls_info) | |
1da177e4 LT |
223 | { |
224 | char temp_hash[16]; | |
225 | struct HMACMD5Context ctx; | |
226 | char * ucase_buf; | |
e89dc920 | 227 | __le16 * unicode_buf; |
1da177e4 LT |
228 | unsigned int i,user_name_len,dom_name_len; |
229 | ||
230 | if(ses == NULL) | |
231 | return -EINVAL; | |
232 | ||
233 | E_md4hash(ses->password, temp_hash); | |
234 | ||
235 | hmac_md5_init_limK_to_64(temp_hash, 16, &ctx); | |
236 | user_name_len = strlen(ses->userName); | |
237 | if(user_name_len > MAX_USERNAME_SIZE) | |
238 | return -EINVAL; | |
3979877e SF |
239 | if(ses->domainName == NULL) |
240 | return -EINVAL; /* BB should we use CIFS_LINUX_DOM */ | |
1da177e4 | 241 | dom_name_len = strlen(ses->domainName); |
b609f06a | 242 | if (dom_name_len > MAX_USERNAME_SIZE) |
1da177e4 LT |
243 | return -EINVAL; |
244 | ||
245 | ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL); | |
b609f06a | 246 | if (ucase_buf == NULL) |
1da177e4 LT |
247 | return -ENOMEM; |
248 | unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL); | |
b609f06a | 249 | if (unicode_buf == NULL) { |
1da177e4 LT |
250 | kfree(ucase_buf); |
251 | return -ENOMEM; | |
252 | } | |
253 | ||
b609f06a | 254 | for (i = 0;i < user_name_len; i++) |
1da177e4 LT |
255 | ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]]; |
256 | ucase_buf[i] = 0; | |
257 | user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); | |
258 | unicode_buf[user_name_len] = 0; | |
259 | user_name_len++; | |
260 | ||
b609f06a | 261 | for (i = 0; i < dom_name_len; i++) |
1da177e4 LT |
262 | ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]]; |
263 | ucase_buf[i] = 0; | |
264 | dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); | |
265 | ||
266 | unicode_buf[user_name_len + dom_name_len] = 0; | |
267 | hmac_md5_update((const unsigned char *) unicode_buf, | |
b609f06a | 268 | (user_name_len+dom_name_len)*2, &ctx); |
1da177e4 | 269 | |
b609f06a | 270 | hmac_md5_final(ses->server->ntlmv2_hash, &ctx); |
1da177e4 LT |
271 | kfree(ucase_buf); |
272 | kfree(unicode_buf); | |
273 | return 0; | |
274 | } | |
7c7b25bc SF |
275 | |
276 | #ifdef CONFIG_CIFS_WEAK_PW_HASH | |
277 | void calc_lanman_hash(struct cifsSesInfo * ses, char * lnm_session_key) | |
278 | { | |
279 | int i; | |
280 | char password_with_pad[CIFS_ENCPWD_SIZE]; | |
281 | ||
bdc4bf6e SF |
282 | if(ses->server == NULL) |
283 | return; | |
284 | ||
7c7b25bc | 285 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
66abda5e SF |
286 | if(ses->password) |
287 | strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE); | |
7c7b25bc | 288 | |
bdc4bf6e SF |
289 | if((ses->server->secMode & SECMODE_PW_ENCRYPT) == 0) |
290 | if(extended_security & CIFSSEC_MAY_PLNTXT) { | |
291 | memcpy(lnm_session_key, password_with_pad, CIFS_ENCPWD_SIZE); | |
292 | return; | |
293 | } | |
294 | ||
7c7b25bc SF |
295 | /* calculate old style session key */ |
296 | /* calling toupper is less broken than repeatedly | |
297 | calling nls_toupper would be since that will never | |
298 | work for UTF8, but neither handles multibyte code pages | |
299 | but the only alternative would be converting to UCS-16 (Unicode) | |
300 | (using a routine something like UniStrupr) then | |
301 | uppercasing and then converting back from Unicode - which | |
302 | would only worth doing it if we knew it were utf8. Basically | |
303 | utf8 and other multibyte codepages each need their own strupper | |
304 | function since a byte at a time will ont work. */ | |
305 | ||
306 | for(i = 0; i < CIFS_ENCPWD_SIZE; i++) { | |
307 | password_with_pad[i] = toupper(password_with_pad[i]); | |
308 | } | |
309 | ||
310 | SMBencrypt(password_with_pad, ses->server->cryptKey, lnm_session_key); | |
311 | /* clear password before we return/free memory */ | |
312 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); | |
313 | } | |
314 | #endif /* CIFS_WEAK_PW_HASH */ | |
315 | ||
1717ffc5 SF |
316 | static int calc_ntlmv2_hash(struct cifsSesInfo *ses, |
317 | const struct nls_table * nls_cp) | |
a8ee0344 SF |
318 | { |
319 | int rc = 0; | |
320 | int len; | |
321 | char nt_hash[16]; | |
322 | struct HMACMD5Context * pctxt; | |
1717ffc5 SF |
323 | wchar_t * user; |
324 | wchar_t * domain; | |
a8ee0344 SF |
325 | |
326 | pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL); | |
327 | ||
328 | if(pctxt == NULL) | |
329 | return -ENOMEM; | |
330 | ||
331 | /* calculate md4 hash of password */ | |
332 | E_md4hash(ses->password, nt_hash); | |
333 | ||
1717ffc5 | 334 | /* convert Domainname to unicode and uppercase */ |
a8ee0344 SF |
335 | hmac_md5_init_limK_to_64(nt_hash, 16, pctxt); |
336 | ||
337 | /* convert ses->userName to unicode and uppercase */ | |
1717ffc5 SF |
338 | len = strlen(ses->userName); |
339 | user = kmalloc(2 + (len * 2), GFP_KERNEL); | |
340 | if(user == NULL) | |
341 | goto calc_exit_2; | |
342 | len = cifs_strtoUCS(user, ses->userName, len, nls_cp); | |
343 | UniStrupr(user); | |
344 | hmac_md5_update((char *)user, 2*len, pctxt); | |
a8ee0344 SF |
345 | |
346 | /* convert ses->domainName to unicode and uppercase */ | |
1717ffc5 SF |
347 | if(ses->domainName) { |
348 | len = strlen(ses->domainName); | |
a8ee0344 | 349 | |
1717ffc5 SF |
350 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
351 | if(domain == NULL) | |
352 | goto calc_exit_1; | |
353 | len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp); | |
b609f06a SF |
354 | /* the following line was removed since it didn't work well |
355 | with lower cased domain name that passed as an option. | |
356 | Maybe converting the domain name earlier makes sense */ | |
357 | /* UniStrupr(domain); */ | |
a8ee0344 | 358 | |
1717ffc5 SF |
359 | hmac_md5_update((char *)domain, 2*len, pctxt); |
360 | ||
361 | kfree(domain); | |
362 | } | |
363 | calc_exit_1: | |
364 | kfree(user); | |
365 | calc_exit_2: | |
366 | /* BB FIXME what about bytes 24 through 40 of the signing key? | |
367 | compare with the NTLM example */ | |
b609f06a | 368 | hmac_md5_final(ses->server->ntlmv2_hash, pctxt); |
a8ee0344 SF |
369 | |
370 | return rc; | |
371 | } | |
372 | ||
1717ffc5 SF |
373 | void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf, |
374 | const struct nls_table * nls_cp) | |
6d027cfd | 375 | { |
a8ee0344 | 376 | int rc; |
6d027cfd | 377 | struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf; |
b609f06a | 378 | struct HMACMD5Context context; |
6d027cfd SF |
379 | |
380 | buf->blob_signature = cpu_to_le32(0x00000101); | |
381 | buf->reserved = 0; | |
382 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); | |
1717ffc5 | 383 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); |
6d027cfd | 384 | buf->reserved2 = 0; |
33ec32fa | 385 | buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE); |
6d027cfd | 386 | buf->names[0].length = 0; |
33ec32fa SF |
387 | buf->names[1].type = 0; |
388 | buf->names[1].length = 0; | |
a8ee0344 | 389 | |
6d027cfd | 390 | /* calculate buf->ntlmv2_hash */ |
1717ffc5 | 391 | rc = calc_ntlmv2_hash(ses, nls_cp); |
a8ee0344 SF |
392 | if(rc) |
393 | cERROR(1,("could not get v2 hash rc %d",rc)); | |
1717ffc5 | 394 | CalcNTLMv2_response(ses, resp_buf); |
b609f06a SF |
395 | |
396 | /* now calculate the MAC key for NTLMv2 */ | |
397 | hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context); | |
398 | hmac_md5_update(resp_buf, 16, &context); | |
399 | hmac_md5_final(ses->server->mac_signing_key.data.ntlmv2.key, &context); | |
400 | ||
401 | memcpy(&ses->server->mac_signing_key.data.ntlmv2.resp, resp_buf, | |
402 | sizeof(struct ntlmv2_resp)); | |
403 | ses->server->mac_signing_key.len = 16 + sizeof(struct ntlmv2_resp); | |
6d027cfd SF |
404 | } |
405 | ||
1717ffc5 | 406 | void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response) |
1da177e4 LT |
407 | { |
408 | struct HMACMD5Context context; | |
1717ffc5 | 409 | /* rest of v2 struct already generated */ |
1da177e4 | 410 | memcpy(v2_session_response + 8, ses->server->cryptKey,8); |
b609f06a | 411 | hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context); |
1da177e4 | 412 | |
b609f06a | 413 | hmac_md5_update(v2_session_response+8, |
1717ffc5 | 414 | sizeof(struct ntlmv2_resp) - 8, &context); |
1da177e4 LT |
415 | |
416 | hmac_md5_final(v2_session_response,&context); | |
1717ffc5 | 417 | /* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */ |
1da177e4 | 418 | } |