]>
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> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
1da177e4 | 24 | #include "cifspdu.h" |
ffdd6e4d | 25 | #include "cifsglob.h" |
1da177e4 LT |
26 | #include "cifs_debug.h" |
27 | #include "md5.h" | |
28 | #include "cifs_unicode.h" | |
29 | #include "cifsproto.h" | |
2b149f11 | 30 | #include "ntlmssp.h" |
7c7b25bc | 31 | #include <linux/ctype.h> |
6d027cfd | 32 | #include <linux/random.h> |
1da177e4 | 33 | |
ffdd6e4d | 34 | /* Calculate and return the CIFS signature based on the mac key and SMB PDU */ |
1da177e4 LT |
35 | /* the 16 byte signature must be allocated by the caller */ |
36 | /* Note we only use the 1st eight bytes */ | |
ffdd6e4d | 37 | /* Note that the smb header signature field on input contains the |
1da177e4 LT |
38 | sequence number before this function is called */ |
39 | ||
40 | extern void mdfour(unsigned char *out, unsigned char *in, int n); | |
41 | extern void E_md4hash(const unsigned char *passwd, unsigned char *p16); | |
4e53a3fb | 42 | extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8, |
ffdd6e4d | 43 | unsigned char *p24); |
50c2f753 | 44 | |
ffdd6e4d | 45 | static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, |
21e73393 | 46 | struct TCP_Server_Info *server, char *signature) |
1da177e4 | 47 | { |
307fbd31 | 48 | int rc; |
1da177e4 | 49 | |
21e73393 | 50 | if (cifs_pdu == NULL || signature == NULL || server == NULL) |
1da177e4 LT |
51 | return -EINVAL; |
52 | ||
307fbd31 SP |
53 | if (!server->secmech.sdescmd5) { |
54 | cERROR(1, "%s: Can't generate signature\n", __func__); | |
55 | return -1; | |
56 | } | |
57 | ||
58 | rc = crypto_shash_init(&server->secmech.sdescmd5->shash); | |
59 | if (rc) { | |
60 | cERROR(1, "%s: Oould not init md5\n", __func__); | |
61 | return rc; | |
62 | } | |
63 | ||
64 | crypto_shash_update(&server->secmech.sdescmd5->shash, | |
65 | server->session_key.response, server->session_key.len); | |
66 | ||
67 | crypto_shash_update(&server->secmech.sdescmd5->shash, | |
68 | cifs_pdu->Protocol, cifs_pdu->smb_buf_length); | |
69 | ||
70 | rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature); | |
b609f06a | 71 | |
56234e27 | 72 | return 0; |
1da177e4 LT |
73 | } |
74 | ||
ffdd6e4d SF |
75 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
76 | __u32 *pexpected_response_sequence_number) | |
1da177e4 LT |
77 | { |
78 | int rc = 0; | |
79 | char smb_signature[20]; | |
80 | ||
ffdd6e4d | 81 | if ((cifs_pdu == NULL) || (server == NULL)) |
1da177e4 LT |
82 | return -EINVAL; |
83 | ||
ffdd6e4d | 84 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
1da177e4 LT |
85 | return rc; |
86 | ||
87 | spin_lock(&GlobalMid_Lock); | |
50c2f753 SF |
88 | cifs_pdu->Signature.Sequence.SequenceNumber = |
89 | cpu_to_le32(server->sequence_number); | |
1da177e4 | 90 | cifs_pdu->Signature.Sequence.Reserved = 0; |
50c2f753 | 91 | |
ad009ac9 SF |
92 | *pexpected_response_sequence_number = server->sequence_number++; |
93 | server->sequence_number++; | |
1da177e4 LT |
94 | spin_unlock(&GlobalMid_Lock); |
95 | ||
21e73393 | 96 | rc = cifs_calculate_signature(cifs_pdu, server, smb_signature); |
ffdd6e4d | 97 | if (rc) |
1da177e4 LT |
98 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
99 | else | |
100 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); | |
101 | ||
102 | return rc; | |
103 | } | |
104 | ||
ffdd6e4d | 105 | static int cifs_calc_signature2(const struct kvec *iov, int n_vec, |
21e73393 | 106 | struct TCP_Server_Info *server, char *signature) |
84afc29b | 107 | { |
e9917a00 | 108 | int i; |
307fbd31 | 109 | int rc; |
84afc29b | 110 | |
21e73393 | 111 | if (iov == NULL || signature == NULL || server == NULL) |
e9917a00 | 112 | return -EINVAL; |
84afc29b | 113 | |
307fbd31 SP |
114 | if (!server->secmech.sdescmd5) { |
115 | cERROR(1, "%s: Can't generate signature\n", __func__); | |
116 | return -1; | |
117 | } | |
118 | ||
119 | rc = crypto_shash_init(&server->secmech.sdescmd5->shash); | |
120 | if (rc) { | |
121 | cERROR(1, "%s: Oould not init md5\n", __func__); | |
122 | return rc; | |
123 | } | |
124 | ||
125 | crypto_shash_update(&server->secmech.sdescmd5->shash, | |
126 | server->session_key.response, server->session_key.len); | |
127 | ||
50c2f753 | 128 | for (i = 0; i < n_vec; i++) { |
745542e2 JL |
129 | if (iov[i].iov_len == 0) |
130 | continue; | |
ffdd6e4d | 131 | if (iov[i].iov_base == NULL) { |
56234e27 | 132 | cERROR(1, "null iovec entry"); |
e9917a00 | 133 | return -EIO; |
745542e2 | 134 | } |
ffdd6e4d | 135 | /* The first entry includes a length field (which does not get |
e9917a00 | 136 | signed that occupies the first 4 bytes before the header */ |
ffdd6e4d | 137 | if (i == 0) { |
63d2583f | 138 | if (iov[0].iov_len <= 8) /* cmd field at offset 9 */ |
e9917a00 | 139 | break; /* nothing to sign or corrupt header */ |
307fbd31 SP |
140 | crypto_shash_update(&server->secmech.sdescmd5->shash, |
141 | iov[i].iov_base + 4, iov[i].iov_len - 4); | |
e9917a00 | 142 | } else |
307fbd31 SP |
143 | crypto_shash_update(&server->secmech.sdescmd5->shash, |
144 | iov[i].iov_base, iov[i].iov_len); | |
e9917a00 | 145 | } |
84afc29b | 146 | |
307fbd31 | 147 | rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature); |
84afc29b | 148 | |
307fbd31 | 149 | return rc; |
84afc29b SF |
150 | } |
151 | ||
ffdd6e4d | 152 | int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, |
63d2583f | 153 | __u32 *pexpected_response_sequence_number) |
84afc29b SF |
154 | { |
155 | int rc = 0; | |
156 | char smb_signature[20]; | |
ffdd6e4d | 157 | struct smb_hdr *cifs_pdu = iov[0].iov_base; |
84afc29b | 158 | |
ffdd6e4d | 159 | if ((cifs_pdu == NULL) || (server == NULL)) |
84afc29b SF |
160 | return -EINVAL; |
161 | ||
ffdd6e4d | 162 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
84afc29b SF |
163 | return rc; |
164 | ||
ffdd6e4d SF |
165 | spin_lock(&GlobalMid_Lock); |
166 | cifs_pdu->Signature.Sequence.SequenceNumber = | |
84afc29b | 167 | cpu_to_le32(server->sequence_number); |
ffdd6e4d | 168 | cifs_pdu->Signature.Sequence.Reserved = 0; |
84afc29b | 169 | |
ffdd6e4d SF |
170 | *pexpected_response_sequence_number = server->sequence_number++; |
171 | server->sequence_number++; | |
172 | spin_unlock(&GlobalMid_Lock); | |
84afc29b | 173 | |
21e73393 | 174 | rc = cifs_calc_signature2(iov, n_vec, server, smb_signature); |
ffdd6e4d SF |
175 | if (rc) |
176 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); | |
177 | else | |
178 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); | |
84afc29b | 179 | |
ffdd6e4d | 180 | return rc; |
84afc29b SF |
181 | } |
182 | ||
b609f06a | 183 | int cifs_verify_signature(struct smb_hdr *cifs_pdu, |
21e73393 | 184 | struct TCP_Server_Info *server, |
ffdd6e4d | 185 | __u32 expected_sequence_number) |
1da177e4 | 186 | { |
c8e56f1f | 187 | unsigned int rc; |
1da177e4 LT |
188 | char server_response_sig[8]; |
189 | char what_we_think_sig_should_be[20]; | |
190 | ||
21e73393 | 191 | if (cifs_pdu == NULL || server == NULL) |
1da177e4 LT |
192 | return -EINVAL; |
193 | ||
194 | if (cifs_pdu->Command == SMB_COM_NEGOTIATE) | |
195 | return 0; | |
196 | ||
197 | if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) { | |
50c2f753 | 198 | struct smb_com_lock_req *pSMB = |
ffdd6e4d SF |
199 | (struct smb_com_lock_req *)cifs_pdu; |
200 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) | |
1da177e4 LT |
201 | return 0; |
202 | } | |
203 | ||
50c2f753 SF |
204 | /* BB what if signatures are supposed to be on for session but |
205 | server does not send one? BB */ | |
206 | ||
1da177e4 | 207 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
50c2f753 | 208 | if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0) |
b6b38f70 JP |
209 | cFYI(1, "dummy signature received for smb command 0x%x", |
210 | cifs_pdu->Command); | |
1da177e4 LT |
211 | |
212 | /* save off the origiginal signature so we can modify the smb and check | |
213 | its signature against what the server sent */ | |
50c2f753 | 214 | memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8); |
1da177e4 | 215 | |
50c2f753 SF |
216 | cifs_pdu->Signature.Sequence.SequenceNumber = |
217 | cpu_to_le32(expected_sequence_number); | |
1da177e4 LT |
218 | cifs_pdu->Signature.Sequence.Reserved = 0; |
219 | ||
21e73393 | 220 | rc = cifs_calculate_signature(cifs_pdu, server, |
1da177e4 LT |
221 | what_we_think_sig_should_be); |
222 | ||
50c2f753 | 223 | if (rc) |
1da177e4 LT |
224 | return rc; |
225 | ||
50c2f753 SF |
226 | /* cifs_dump_mem("what we think it should be: ", |
227 | what_we_think_sig_should_be, 16); */ | |
1da177e4 | 228 | |
50c2f753 | 229 | if (memcmp(server_response_sig, what_we_think_sig_should_be, 8)) |
1da177e4 LT |
230 | return -EACCES; |
231 | else | |
232 | return 0; | |
233 | ||
234 | } | |
235 | ||
21e73393 SP |
236 | /* first calculate 24 bytes ntlm response and then 16 byte session key */ |
237 | int setup_ntlm_response(struct cifsSesInfo *ses) | |
1da177e4 | 238 | { |
21e73393 SP |
239 | unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE; |
240 | char temp_key[CIFS_SESS_KEY_SIZE]; | |
241 | ||
242 | if (!ses) | |
1da177e4 LT |
243 | return -EINVAL; |
244 | ||
21e73393 SP |
245 | ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL); |
246 | if (!ses->auth_key.response) { | |
247 | cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len); | |
248 | return -ENOMEM; | |
249 | } | |
250 | ses->auth_key.len = temp_len; | |
251 | ||
d3ba50b1 | 252 | SMBNTencrypt(ses->password, ses->server->cryptkey, |
21e73393 SP |
253 | ses->auth_key.response + CIFS_SESS_KEY_SIZE); |
254 | ||
255 | E_md4hash(ses->password, temp_key); | |
256 | mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE); | |
257 | ||
1da177e4 LT |
258 | return 0; |
259 | } | |
260 | ||
7c7b25bc | 261 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
4e53a3fb JL |
262 | void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt, |
263 | char *lnm_session_key) | |
7c7b25bc SF |
264 | { |
265 | int i; | |
266 | char password_with_pad[CIFS_ENCPWD_SIZE]; | |
267 | ||
268 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); | |
4e53a3fb JL |
269 | if (password) |
270 | strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE); | |
271 | ||
04912d6a | 272 | if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) { |
4e53a3fb JL |
273 | memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE); |
274 | memcpy(lnm_session_key, password_with_pad, | |
275 | CIFS_ENCPWD_SIZE); | |
276 | return; | |
277 | } | |
bdc4bf6e | 278 | |
7c7b25bc SF |
279 | /* calculate old style session key */ |
280 | /* calling toupper is less broken than repeatedly | |
281 | calling nls_toupper would be since that will never | |
282 | work for UTF8, but neither handles multibyte code pages | |
283 | but the only alternative would be converting to UCS-16 (Unicode) | |
284 | (using a routine something like UniStrupr) then | |
285 | uppercasing and then converting back from Unicode - which | |
286 | would only worth doing it if we knew it were utf8. Basically | |
287 | utf8 and other multibyte codepages each need their own strupper | |
288 | function since a byte at a time will ont work. */ | |
289 | ||
ef571cad | 290 | for (i = 0; i < CIFS_ENCPWD_SIZE; i++) |
7c7b25bc | 291 | password_with_pad[i] = toupper(password_with_pad[i]); |
7c7b25bc | 292 | |
4e53a3fb JL |
293 | SMBencrypt(password_with_pad, cryptkey, lnm_session_key); |
294 | ||
7c7b25bc SF |
295 | /* clear password before we return/free memory */ |
296 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); | |
297 | } | |
298 | #endif /* CIFS_WEAK_PW_HASH */ | |
299 | ||
9daa42e2 SP |
300 | /* Build a proper attribute value/target info pairs blob. |
301 | * Fill in netbios and dns domain name and workstation name | |
302 | * and client time (total five av pairs and + one end of fields indicator. | |
303 | * Allocate domain name which gets freed when session struct is deallocated. | |
2b149f11 SP |
304 | */ |
305 | static int | |
9daa42e2 | 306 | build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp) |
2b149f11 | 307 | { |
9daa42e2 SP |
308 | unsigned int dlen; |
309 | unsigned int wlen; | |
310 | unsigned int size = 6 * sizeof(struct ntlmssp2_name); | |
311 | __le64 curtime; | |
312 | char *defdmname = "WORKGROUP"; | |
313 | unsigned char *blobptr; | |
2b149f11 SP |
314 | struct ntlmssp2_name *attrptr; |
315 | ||
9daa42e2 SP |
316 | if (!ses->domainName) { |
317 | ses->domainName = kstrdup(defdmname, GFP_KERNEL); | |
318 | if (!ses->domainName) | |
319 | return -ENOMEM; | |
320 | } | |
321 | ||
322 | dlen = strlen(ses->domainName); | |
323 | wlen = strlen(ses->server->hostname); | |
324 | ||
325 | /* The length of this blob is a size which is | |
326 | * six times the size of a structure which holds name/size + | |
327 | * two times the unicode length of a domain name + | |
328 | * two times the unicode length of a server name + | |
329 | * size of a timestamp (which is 8 bytes). | |
330 | */ | |
d3686d54 SP |
331 | ses->auth_key.len = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8; |
332 | ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL); | |
333 | if (!ses->auth_key.response) { | |
334 | ses->auth_key.len = 0; | |
2b149f11 SP |
335 | cERROR(1, "Challenge target info allocation failure"); |
336 | return -ENOMEM; | |
337 | } | |
9daa42e2 | 338 | |
d3686d54 | 339 | blobptr = ses->auth_key.response; |
9daa42e2 SP |
340 | attrptr = (struct ntlmssp2_name *) blobptr; |
341 | ||
342 | attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME); | |
343 | attrptr->length = cpu_to_le16(2 * dlen); | |
344 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); | |
345 | cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp); | |
346 | ||
347 | blobptr += 2 * dlen; | |
348 | attrptr = (struct ntlmssp2_name *) blobptr; | |
349 | ||
350 | attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_COMPUTER_NAME); | |
351 | attrptr->length = cpu_to_le16(2 * wlen); | |
352 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); | |
353 | cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp); | |
354 | ||
355 | blobptr += 2 * wlen; | |
356 | attrptr = (struct ntlmssp2_name *) blobptr; | |
357 | ||
358 | attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_DOMAIN_NAME); | |
359 | attrptr->length = cpu_to_le16(2 * dlen); | |
360 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); | |
361 | cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp); | |
362 | ||
363 | blobptr += 2 * dlen; | |
364 | attrptr = (struct ntlmssp2_name *) blobptr; | |
365 | ||
366 | attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_COMPUTER_NAME); | |
367 | attrptr->length = cpu_to_le16(2 * wlen); | |
368 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); | |
369 | cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp); | |
370 | ||
371 | blobptr += 2 * wlen; | |
372 | attrptr = (struct ntlmssp2_name *) blobptr; | |
373 | ||
374 | attrptr->type = cpu_to_le16(NTLMSSP_AV_TIMESTAMP); | |
375 | attrptr->length = cpu_to_le16(sizeof(__le64)); | |
376 | blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name); | |
377 | curtime = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); | |
378 | memcpy(blobptr, &curtime, sizeof(__le64)); | |
2b149f11 SP |
379 | |
380 | return 0; | |
381 | } | |
382 | ||
383 | /* Server has provided av pairs/target info in the type 2 challenge | |
384 | * packet and we have plucked it and stored within smb session. | |
385 | * We parse that blob here to find netbios domain name to be used | |
386 | * as part of ntlmv2 authentication (in Target String), if not already | |
387 | * specified on the command line. | |
388 | * If this function returns without any error but without fetching | |
389 | * domain name, authentication may fail against some server but | |
390 | * may not fail against other (those who are not very particular | |
391 | * about target string i.e. for some, just user name might suffice. | |
392 | */ | |
393 | static int | |
f7c5445a | 394 | find_domain_name(struct cifsSesInfo *ses, const struct nls_table *nls_cp) |
2b149f11 SP |
395 | { |
396 | unsigned int attrsize; | |
397 | unsigned int type; | |
398 | unsigned int onesize = sizeof(struct ntlmssp2_name); | |
399 | unsigned char *blobptr; | |
400 | unsigned char *blobend; | |
401 | struct ntlmssp2_name *attrptr; | |
402 | ||
d3686d54 | 403 | if (!ses->auth_key.len || !ses->auth_key.response) |
2b149f11 SP |
404 | return 0; |
405 | ||
d3686d54 SP |
406 | blobptr = ses->auth_key.response; |
407 | blobend = blobptr + ses->auth_key.len; | |
2b149f11 SP |
408 | |
409 | while (blobptr + onesize < blobend) { | |
410 | attrptr = (struct ntlmssp2_name *) blobptr; | |
411 | type = le16_to_cpu(attrptr->type); | |
412 | if (type == NTLMSSP_AV_EOL) | |
413 | break; | |
414 | blobptr += 2; /* advance attr type */ | |
415 | attrsize = le16_to_cpu(attrptr->length); | |
416 | blobptr += 2; /* advance attr size */ | |
417 | if (blobptr + attrsize > blobend) | |
418 | break; | |
419 | if (type == NTLMSSP_AV_NB_DOMAIN_NAME) { | |
420 | if (!attrsize) | |
421 | break; | |
422 | if (!ses->domainName) { | |
423 | ses->domainName = | |
424 | kmalloc(attrsize + 1, GFP_KERNEL); | |
425 | if (!ses->domainName) | |
426 | return -ENOMEM; | |
427 | cifs_from_ucs2(ses->domainName, | |
428 | (__le16 *)blobptr, attrsize, attrsize, | |
f7c5445a | 429 | nls_cp, false); |
2b149f11 SP |
430 | break; |
431 | } | |
432 | } | |
433 | blobptr += attrsize; /* advance attr value */ | |
434 | } | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
d3686d54 | 439 | static int calc_ntlmv2_hash(struct cifsSesInfo *ses, char *ntlmv2_hash, |
50c2f753 | 440 | const struct nls_table *nls_cp) |
a8ee0344 SF |
441 | { |
442 | int rc = 0; | |
443 | int len; | |
307fbd31 | 444 | char nt_hash[CIFS_NTHASH_SIZE]; |
50c2f753 SF |
445 | wchar_t *user; |
446 | wchar_t *domain; | |
307fbd31 | 447 | wchar_t *server; |
a8ee0344 | 448 | |
307fbd31 SP |
449 | if (!ses->server->secmech.sdeschmacmd5) { |
450 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n"); | |
451 | return -1; | |
452 | } | |
56234e27 | 453 | |
c8e56f1f SF |
454 | /* calculate md4 hash of password */ |
455 | E_md4hash(ses->password, nt_hash); | |
9fbc5908 | 456 | |
307fbd31 SP |
457 | crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash, |
458 | CIFS_NTHASH_SIZE); | |
459 | ||
460 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); | |
461 | if (rc) { | |
462 | cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n"); | |
463 | return rc; | |
464 | } | |
a8ee0344 SF |
465 | |
466 | /* convert ses->userName to unicode and uppercase */ | |
1717ffc5 SF |
467 | len = strlen(ses->userName); |
468 | user = kmalloc(2 + (len * 2), GFP_KERNEL); | |
307fbd31 SP |
469 | if (user == NULL) { |
470 | cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n"); | |
471 | rc = -ENOMEM; | |
1717ffc5 | 472 | goto calc_exit_2; |
307fbd31 | 473 | } |
8f2376ad | 474 | len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp); |
1717ffc5 | 475 | UniStrupr(user); |
307fbd31 SP |
476 | |
477 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, | |
478 | (char *)user, 2 * len); | |
a8ee0344 SF |
479 | |
480 | /* convert ses->domainName to unicode and uppercase */ | |
50c2f753 | 481 | if (ses->domainName) { |
1717ffc5 | 482 | len = strlen(ses->domainName); |
a8ee0344 | 483 | |
50c2f753 | 484 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
307fbd31 SP |
485 | if (domain == NULL) { |
486 | cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure"); | |
487 | rc = -ENOMEM; | |
1717ffc5 | 488 | goto calc_exit_1; |
307fbd31 | 489 | } |
8f2376ad CG |
490 | len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len, |
491 | nls_cp); | |
307fbd31 SP |
492 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
493 | (char *)domain, 2 * len); | |
1717ffc5 | 494 | kfree(domain); |
307fbd31 SP |
495 | } else if (ses->serverName) { |
496 | len = strlen(ses->serverName); | |
497 | ||
498 | server = kmalloc(2 + (len * 2), GFP_KERNEL); | |
499 | if (server == NULL) { | |
500 | cERROR(1, "calc_ntlmv2_hash: server mem alloc failure"); | |
501 | rc = -ENOMEM; | |
502 | goto calc_exit_1; | |
503 | } | |
504 | len = cifs_strtoUCS((__le16 *)server, ses->serverName, len, | |
505 | nls_cp); | |
506 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, | |
507 | (char *)server, 2 * len); | |
508 | kfree(server); | |
1717ffc5 | 509 | } |
307fbd31 SP |
510 | |
511 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, | |
d3686d54 | 512 | ntlmv2_hash); |
307fbd31 | 513 | |
1717ffc5 SF |
514 | calc_exit_1: |
515 | kfree(user); | |
516 | calc_exit_2: | |
307fbd31 SP |
517 | return rc; |
518 | } | |
519 | ||
520 | static int | |
d3686d54 | 521 | CalcNTLMv2_response(const struct cifsSesInfo *ses, char *ntlmv2_hash) |
307fbd31 SP |
522 | { |
523 | int rc; | |
524 | unsigned int offset = CIFS_SESS_KEY_SIZE + 8; | |
525 | ||
526 | if (!ses->server->secmech.sdeschmacmd5) { | |
527 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n"); | |
528 | return -1; | |
529 | } | |
530 | ||
531 | crypto_shash_setkey(ses->server->secmech.hmacmd5, | |
d3686d54 | 532 | ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
307fbd31 SP |
533 | |
534 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); | |
535 | if (rc) { | |
536 | cERROR(1, "CalcNTLMv2_response: could not init hmacmd5"); | |
537 | return rc; | |
538 | } | |
539 | ||
d3ba50b1 SP |
540 | if (ses->server->secType == RawNTLMSSP) |
541 | memcpy(ses->auth_key.response + offset, | |
d3686d54 | 542 | ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); |
d3ba50b1 SP |
543 | else |
544 | memcpy(ses->auth_key.response + offset, | |
545 | ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); | |
307fbd31 SP |
546 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, |
547 | ses->auth_key.response + offset, ses->auth_key.len - offset); | |
548 | ||
549 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, | |
550 | ses->auth_key.response + CIFS_SESS_KEY_SIZE); | |
9fbc5908 SF |
551 | |
552 | return rc; | |
553 | } | |
554 | ||
307fbd31 | 555 | |
2b149f11 | 556 | int |
21e73393 | 557 | setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp) |
9fbc5908 | 558 | { |
c8e56f1f | 559 | int rc; |
21e73393 | 560 | int baselen; |
d3686d54 | 561 | unsigned int tilen; |
21e73393 | 562 | struct ntlmv2_resp *buf; |
d3686d54 SP |
563 | char ntlmv2_hash[16]; |
564 | unsigned char *tiblob = NULL; /* target info blob */ | |
6d027cfd | 565 | |
2b149f11 SP |
566 | if (ses->server->secType == RawNTLMSSP) { |
567 | if (!ses->domainName) { | |
f7c5445a | 568 | rc = find_domain_name(ses, nls_cp); |
2b149f11 SP |
569 | if (rc) { |
570 | cERROR(1, "error %d finding domain name", rc); | |
571 | goto setup_ntlmv2_rsp_ret; | |
572 | } | |
573 | } | |
574 | } else { | |
9daa42e2 | 575 | rc = build_avpair_blob(ses, nls_cp); |
2b149f11 SP |
576 | if (rc) { |
577 | cERROR(1, "error %d building av pair blob", rc); | |
d3686d54 | 578 | goto setup_ntlmv2_rsp_ret; |
2b149f11 SP |
579 | } |
580 | } | |
a8ee0344 | 581 | |
21e73393 | 582 | baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp); |
d3686d54 SP |
583 | tilen = ses->auth_key.len; |
584 | tiblob = ses->auth_key.response; | |
585 | ||
586 | ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL); | |
21e73393 SP |
587 | if (!ses->auth_key.response) { |
588 | rc = ENOMEM; | |
d3686d54 | 589 | ses->auth_key.len = 0; |
21e73393 SP |
590 | cERROR(1, "%s: Can't allocate auth blob", __func__); |
591 | goto setup_ntlmv2_rsp_ret; | |
592 | } | |
d3686d54 | 593 | ses->auth_key.len += baselen; |
21e73393 SP |
594 | |
595 | buf = (struct ntlmv2_resp *) | |
596 | (ses->auth_key.response + CIFS_SESS_KEY_SIZE); | |
597 | buf->blob_signature = cpu_to_le32(0x00000101); | |
598 | buf->reserved = 0; | |
599 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); | |
600 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); | |
601 | buf->reserved2 = 0; | |
602 | ||
d3686d54 | 603 | memcpy(ses->auth_key.response + baselen, tiblob, tilen); |
21e73393 | 604 | |
f7c5445a | 605 | /* calculate ntlmv2_hash */ |
d3686d54 | 606 | rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp); |
2b149f11 | 607 | if (rc) { |
b6b38f70 | 608 | cERROR(1, "could not get v2 hash rc %d", rc); |
2b149f11 SP |
609 | goto setup_ntlmv2_rsp_ret; |
610 | } | |
f7c5445a SP |
611 | |
612 | /* calculate first part of the client response (CR1) */ | |
d3686d54 | 613 | rc = CalcNTLMv2_response(ses, ntlmv2_hash); |
307fbd31 SP |
614 | if (rc) { |
615 | cERROR(1, "Could not calculate CR1 rc: %d", rc); | |
616 | goto setup_ntlmv2_rsp_ret; | |
617 | } | |
b609f06a | 618 | |
5d0d2882 | 619 | /* now calculate the session key for NTLMv2 */ |
307fbd31 | 620 | crypto_shash_setkey(ses->server->secmech.hmacmd5, |
d3686d54 | 621 | ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
307fbd31 SP |
622 | |
623 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash); | |
624 | if (rc) { | |
625 | cERROR(1, "%s: Could not init hmacmd5\n", __func__); | |
626 | goto setup_ntlmv2_rsp_ret; | |
627 | } | |
628 | ||
629 | crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash, | |
630 | ses->auth_key.response + CIFS_SESS_KEY_SIZE, | |
631 | CIFS_HMAC_MD5_HASH_SIZE); | |
632 | ||
633 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash, | |
634 | ses->auth_key.response); | |
2b149f11 | 635 | |
2b149f11 | 636 | setup_ntlmv2_rsp_ret: |
d3686d54 | 637 | kfree(tiblob); |
2b149f11 SP |
638 | |
639 | return rc; | |
6d027cfd SF |
640 | } |
641 | ||
d2b91521 SP |
642 | int |
643 | calc_seckey(struct cifsSesInfo *ses) | |
644 | { | |
645 | int rc; | |
646 | struct crypto_blkcipher *tfm_arc4; | |
647 | struct scatterlist sgin, sgout; | |
648 | struct blkcipher_desc desc; | |
649 | unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */ | |
650 | ||
651 | get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE); | |
652 | ||
653 | tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC); | |
654 | if (!tfm_arc4 || IS_ERR(tfm_arc4)) { | |
655 | cERROR(1, "could not allocate crypto API arc4\n"); | |
656 | return PTR_ERR(tfm_arc4); | |
657 | } | |
658 | ||
659 | desc.tfm = tfm_arc4; | |
660 | ||
661 | crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response, | |
662 | CIFS_SESS_KEY_SIZE); | |
663 | ||
664 | sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE); | |
d3686d54 | 665 | sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE); |
d2b91521 SP |
666 | |
667 | rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE); | |
668 | if (rc) { | |
669 | cERROR(1, "could not encrypt session key rc: %d\n", rc); | |
670 | crypto_free_blkcipher(tfm_arc4); | |
671 | return rc; | |
672 | } | |
673 | ||
674 | /* make secondary_key/nonce as session key */ | |
675 | memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE); | |
676 | /* and make len as that of session key only */ | |
677 | ses->auth_key.len = CIFS_SESS_KEY_SIZE; | |
678 | ||
679 | crypto_free_blkcipher(tfm_arc4); | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
684 | void | |
685 | cifs_crypto_shash_release(struct TCP_Server_Info *server) | |
686 | { | |
687 | if (server->secmech.md5) | |
688 | crypto_free_shash(server->secmech.md5); | |
689 | ||
690 | if (server->secmech.hmacmd5) | |
691 | crypto_free_shash(server->secmech.hmacmd5); | |
692 | ||
693 | kfree(server->secmech.sdeschmacmd5); | |
694 | ||
695 | kfree(server->secmech.sdescmd5); | |
696 | } | |
697 | ||
698 | int | |
699 | cifs_crypto_shash_allocate(struct TCP_Server_Info *server) | |
700 | { | |
701 | int rc; | |
702 | unsigned int size; | |
703 | ||
704 | server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0); | |
705 | if (!server->secmech.hmacmd5 || | |
706 | IS_ERR(server->secmech.hmacmd5)) { | |
707 | cERROR(1, "could not allocate crypto hmacmd5\n"); | |
708 | return PTR_ERR(server->secmech.hmacmd5); | |
709 | } | |
710 | ||
711 | server->secmech.md5 = crypto_alloc_shash("md5", 0, 0); | |
712 | if (!server->secmech.md5 || IS_ERR(server->secmech.md5)) { | |
713 | cERROR(1, "could not allocate crypto md5\n"); | |
714 | rc = PTR_ERR(server->secmech.md5); | |
715 | goto crypto_allocate_md5_fail; | |
716 | } | |
717 | ||
718 | size = sizeof(struct shash_desc) + | |
719 | crypto_shash_descsize(server->secmech.hmacmd5); | |
720 | server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL); | |
721 | if (!server->secmech.sdeschmacmd5) { | |
722 | cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n"); | |
723 | rc = -ENOMEM; | |
724 | goto crypto_allocate_hmacmd5_sdesc_fail; | |
725 | } | |
726 | server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5; | |
727 | server->secmech.sdeschmacmd5->shash.flags = 0x0; | |
728 | ||
729 | ||
730 | size = sizeof(struct shash_desc) + | |
731 | crypto_shash_descsize(server->secmech.md5); | |
732 | server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL); | |
733 | if (!server->secmech.sdescmd5) { | |
734 | cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n"); | |
735 | rc = -ENOMEM; | |
736 | goto crypto_allocate_md5_sdesc_fail; | |
737 | } | |
738 | server->secmech.sdescmd5->shash.tfm = server->secmech.md5; | |
739 | server->secmech.sdescmd5->shash.flags = 0x0; | |
740 | ||
741 | return 0; | |
742 | ||
743 | crypto_allocate_md5_sdesc_fail: | |
744 | kfree(server->secmech.sdeschmacmd5); | |
745 | ||
746 | crypto_allocate_hmacmd5_sdesc_fail: | |
747 | crypto_free_shash(server->secmech.md5); | |
748 | ||
749 | crypto_allocate_md5_fail: | |
750 | crypto_free_shash(server->secmech.hmacmd5); | |
751 | ||
752 | return rc; | |
753 | } |