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