blob: 96908874a45c81e4a2b860965c1d0b659bc6d702 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsencrypt.c
3 *
Steve French12b3b8f2006-02-09 21:12:47 +00004 * Copyright (C) International Business Machines Corp., 2005,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "cifspdu.h"
Steve Frenchffdd6e42007-06-24 21:15:44 +000025#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifs_debug.h"
27#include "md5.h"
28#include "cifs_unicode.h"
29#include "cifsproto.h"
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -050030#include "ntlmssp.h"
Steve French7c7b25b2006-06-01 19:20:10 +000031#include <linux/ctype.h>
Steve French6d027cf2006-06-05 16:26:05 +000032#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Steve Frenchffdd6e42007-06-24 21:15:44 +000034/* Calculate and return the CIFS signature based on the mac key and SMB PDU */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* the 16 byte signature must be allocated by the caller */
36/* Note we only use the 1st eight bytes */
Steve Frenchffdd6e42007-06-24 21:15:44 +000037/* Note that the smb header signature field on input contains the
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 sequence number before this function is called */
39
40extern void mdfour(unsigned char *out, unsigned char *in, int n);
41extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
Jeff Layton4e53a3f2008-12-05 20:41:21 -050042extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
Steve Frenchffdd6e42007-06-24 21:15:44 +000043 unsigned char *p24);
Steve French50c2f752007-07-13 00:33:32 +000044
Steve Frenchffdd6e42007-06-24 21:15:44 +000045static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -050046 struct TCP_Server_Info *server, char *signature)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050048 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Shirish Pargaonkar21e73392010-10-21 06:42:55 -050050 if (cifs_pdu == NULL || signature == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return -EINVAL;
52
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050053 if (!server->secmech.sdescmd5) {
54 cERROR(1, "%s: Can't generate signature\n", __func__);
55 return -1;
56 }
Steve Frenchb609f062007-07-09 07:55:14 +000057
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050058 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);
71
Steve French56234e22010-09-08 20:57:05 +000072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Steve Frenchffdd6e42007-06-24 21:15:44 +000075int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
76 __u32 *pexpected_response_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int rc = 0;
79 char smb_signature[20];
80
Steve Frenchffdd6e42007-06-24 21:15:44 +000081 if ((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -EINVAL;
83
Steve Frenchffdd6e42007-06-24 21:15:44 +000084 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return rc;
86
87 spin_lock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +000088 cifs_pdu->Signature.Sequence.SequenceNumber =
89 cpu_to_le32(server->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French50c2f752007-07-13 00:33:32 +000091
Steve Frenchad009ac2005-04-28 22:41:05 -070092 *pexpected_response_sequence_number = server->sequence_number++;
93 server->sequence_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_unlock(&GlobalMid_Lock);
95
Shirish Pargaonkar21e73392010-10-21 06:42:55 -050096 rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +000097 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
99 else
100 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
101
102 return rc;
103}
104
Steve Frenchffdd6e42007-06-24 21:15:44 +0000105static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500106 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800107{
Steve Frenche9917a02006-03-31 21:22:00 +0000108 int i;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500109 int rc;
Steve French84afc292005-12-02 13:32:45 -0800110
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500111 if (iov == NULL || signature == NULL || server == NULL)
Steve Frenche9917a02006-03-31 21:22:00 +0000112 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800113
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500114 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
Steve French50c2f752007-07-13 00:33:32 +0000128 for (i = 0; i < n_vec; i++) {
Jeff Layton745542e2007-11-03 04:34:04 +0000129 if (iov[i].iov_len == 0)
130 continue;
Steve Frenchffdd6e42007-06-24 21:15:44 +0000131 if (iov[i].iov_base == NULL) {
Steve French56234e22010-09-08 20:57:05 +0000132 cERROR(1, "null iovec entry");
Steve Frenche9917a02006-03-31 21:22:00 +0000133 return -EIO;
Jeff Layton745542e2007-11-03 04:34:04 +0000134 }
Steve Frenchffdd6e42007-06-24 21:15:44 +0000135 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +0000136 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000137 if (i == 0) {
Steve French63d25832007-11-05 21:46:10 +0000138 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
Steve Frenche9917a02006-03-31 21:22:00 +0000139 break; /* nothing to sign or corrupt header */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500140 crypto_shash_update(&server->secmech.sdescmd5->shash,
141 iov[i].iov_base + 4, iov[i].iov_len - 4);
Steve Frenche9917a02006-03-31 21:22:00 +0000142 } else
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500143 crypto_shash_update(&server->secmech.sdescmd5->shash,
144 iov[i].iov_base, iov[i].iov_len);
Steve Frenche9917a02006-03-31 21:22:00 +0000145 }
Steve French84afc292005-12-02 13:32:45 -0800146
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500147 rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
Steve French84afc292005-12-02 13:32:45 -0800148
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500149 return rc;
Steve French84afc292005-12-02 13:32:45 -0800150}
151
Steve Frenchffdd6e42007-06-24 21:15:44 +0000152int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000153 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800154{
155 int rc = 0;
156 char smb_signature[20];
Steve Frenchffdd6e42007-06-24 21:15:44 +0000157 struct smb_hdr *cifs_pdu = iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800158
Steve Frenchffdd6e42007-06-24 21:15:44 +0000159 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800160 return -EINVAL;
161
Steve Frenchffdd6e42007-06-24 21:15:44 +0000162 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Steve French84afc292005-12-02 13:32:45 -0800163 return rc;
164
Steve Frenchffdd6e42007-06-24 21:15:44 +0000165 spin_lock(&GlobalMid_Lock);
166 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800167 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000168 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800169
Steve Frenchffdd6e42007-06-24 21:15:44 +0000170 *pexpected_response_sequence_number = server->sequence_number++;
171 server->sequence_number++;
172 spin_unlock(&GlobalMid_Lock);
Steve French84afc292005-12-02 13:32:45 -0800173
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500174 rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000175 if (rc)
176 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
177 else
178 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800179
Steve Frenchffdd6e42007-06-24 21:15:44 +0000180 return rc;
Steve French84afc292005-12-02 13:32:45 -0800181}
182
Steve Frenchb609f062007-07-09 07:55:14 +0000183int cifs_verify_signature(struct smb_hdr *cifs_pdu,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500184 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000185 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000187 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 char server_response_sig[8];
189 char what_we_think_sig_should_be[20];
190
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500191 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -EINVAL;
193
194 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
195 return 0;
196
197 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000198 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000199 (struct smb_com_lock_req *)cifs_pdu;
200 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202 }
203
Steve French50c2f752007-07-13 00:33:32 +0000204 /* BB what if signatures are supposed to be on for session but
205 server does not send one? BB */
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000208 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000209 cFYI(1, "dummy signature received for smb command 0x%x",
210 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* save off the origiginal signature so we can modify the smb and check
213 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000214 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Steve French50c2f752007-07-13 00:33:32 +0000216 cifs_pdu->Signature.Sequence.SequenceNumber =
217 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 cifs_pdu->Signature.Sequence.Reserved = 0;
219
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500220 rc = cifs_calculate_signature(cifs_pdu, server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 what_we_think_sig_should_be);
222
Steve French50c2f752007-07-13 00:33:32 +0000223 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return rc;
225
Steve French50c2f752007-07-13 00:33:32 +0000226/* cifs_dump_mem("what we think it should be: ",
227 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Steve French50c2f752007-07-13 00:33:32 +0000229 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EACCES;
231 else
232 return 0;
233
234}
235
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500236/* first calculate 24 bytes ntlm response and then 16 byte session key */
237int setup_ntlm_response(struct cifsSesInfo *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500239 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
240 char temp_key[CIFS_SESS_KEY_SIZE];
241
242 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EINVAL;
244
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500245 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
252 SMBNTencrypt(ses->password, ses->cryptKey,
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
Steve French7c7b25b2006-06-01 19:20:10 +0000261#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500262void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
263 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000264{
265 int i;
266 char password_with_pad[CIFS_ENCPWD_SIZE];
267
268 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500269 if (password)
270 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000271
Jeff Layton04912d62010-04-24 07:57:45 -0400272 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500273 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
274 memcpy(lnm_session_key, password_with_pad,
275 CIFS_ENCPWD_SIZE);
276 return;
277 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000278
Steve French7c7b25b2006-06-01 19:20:10 +0000279 /* 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
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000290 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000291 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000292
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500293 SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
294
Steve French7c7b25b2006-06-01 19:20:10 +0000295 /* clear password before we return/free memory */
296 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
297}
298#endif /* CIFS_WEAK_PW_HASH */
299
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500300/* 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.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500304 */
305static int
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500306build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500307{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500308 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;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500314 struct ntlmssp2_name *attrptr;
315
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500316 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 */
331 ses->tilen = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500332 ses->tiblob = kzalloc(ses->tilen, GFP_KERNEL);
333 if (!ses->tiblob) {
334 ses->tilen = 0;
335 cERROR(1, "Challenge target info allocation failure");
336 return -ENOMEM;
337 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500338
339 blobptr = ses->tiblob;
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));
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500379
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 */
393static int
394find_domain_name(struct cifsSesInfo *ses)
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
403 if (!ses->tilen || !ses->tiblob)
404 return 0;
405
406 blobptr = ses->tiblob;
407 blobend = ses->tiblob + ses->tilen;
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) {
Jeff Laytonccc46a742010-10-06 19:51:12 -0400423 struct nls_table *default_nls;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500424 ses->domainName =
425 kmalloc(attrsize + 1, GFP_KERNEL);
426 if (!ses->domainName)
427 return -ENOMEM;
Jeff Laytonccc46a742010-10-06 19:51:12 -0400428 default_nls = load_nls_default();
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500429 cifs_from_ucs2(ses->domainName,
430 (__le16 *)blobptr, attrsize, attrsize,
Jeff Laytonccc46a742010-10-06 19:51:12 -0400431 default_nls, false);
432 unload_nls(default_nls);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500433 break;
434 }
435 }
436 blobptr += attrsize; /* advance attr value */
437 }
438
439 return 0;
440}
441
Steve French50c2f752007-07-13 00:33:32 +0000442static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
443 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000444{
445 int rc = 0;
446 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500447 char nt_hash[CIFS_NTHASH_SIZE];
Steve French50c2f752007-07-13 00:33:32 +0000448 wchar_t *user;
449 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500450 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000451
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500452 if (!ses->server->secmech.sdeschmacmd5) {
453 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
454 return -1;
455 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000456
457 /* calculate md4 hash of password */
458 E_md4hash(ses->password, nt_hash);
459
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500460 crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
461 CIFS_NTHASH_SIZE);
462
463 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
464 if (rc) {
465 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
466 return rc;
467 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000468
469 /* convert ses->userName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000470 len = strlen(ses->userName);
471 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500472 if (user == NULL) {
473 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
474 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000475 goto calc_exit_2;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500476 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000477 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
Steve French1717ffc2006-06-08 05:41:32 +0000478 UniStrupr(user);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500479
480 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
481 (char *)user, 2 * len);
Steve Frencha8ee0342006-06-05 23:34:19 +0000482
483 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000484 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000485 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000486
Steve French50c2f752007-07-13 00:33:32 +0000487 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500488 if (domain == NULL) {
489 cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
490 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000491 goto calc_exit_1;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500492 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000493 len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
494 nls_cp);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500495 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
496 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000497 kfree(domain);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500498 } else if (ses->serverName) {
499 len = strlen(ses->serverName);
500
501 server = kmalloc(2 + (len * 2), GFP_KERNEL);
502 if (server == NULL) {
503 cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
504 rc = -ENOMEM;
505 goto calc_exit_1;
506 }
507 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
508 nls_cp);
509 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
510 (char *)server, 2 * len);
511 kfree(server);
Steve French1717ffc2006-06-08 05:41:32 +0000512 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500513
514 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
515 ses->ntlmv2_hash);
516
Steve French1717ffc2006-06-08 05:41:32 +0000517calc_exit_1:
518 kfree(user);
519calc_exit_2:
Steve Frencha8ee0342006-06-05 23:34:19 +0000520 return rc;
521}
522
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500523static int
524CalcNTLMv2_response(const struct cifsSesInfo *ses)
525{
526 int rc;
527 unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
528
529 if (!ses->server->secmech.sdeschmacmd5) {
530 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
531 return -1;
532 }
533
534 crypto_shash_setkey(ses->server->secmech.hmacmd5,
535 ses->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
536
537 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
538 if (rc) {
539 cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
540 return rc;
541 }
542
543 memcpy(ses->auth_key.response + offset,
544 ses->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
545 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
546 ses->auth_key.response + offset, ses->auth_key.len - offset);
547
548 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
549 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
550
551 return rc;
552}
553
554
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500555int
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500556setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000557{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000558 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500559 int baselen;
560 struct ntlmv2_resp *buf;
Steve French6d027cf2006-06-05 16:26:05 +0000561
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500562 if (ses->server->secType == RawNTLMSSP) {
563 if (!ses->domainName) {
564 rc = find_domain_name(ses);
565 if (rc) {
566 cERROR(1, "error %d finding domain name", rc);
567 goto setup_ntlmv2_rsp_ret;
568 }
569 }
570 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500571 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500572 if (rc) {
573 cERROR(1, "error %d building av pair blob", rc);
574 return rc;
575 }
576 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000577
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500578 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
579 ses->auth_key.len = baselen + ses->tilen;
580 ses->auth_key.response = kmalloc(ses->auth_key.len, GFP_KERNEL);
581 if (!ses->auth_key.response) {
582 rc = ENOMEM;
583 cERROR(1, "%s: Can't allocate auth blob", __func__);
584 goto setup_ntlmv2_rsp_ret;
585 }
586
587 buf = (struct ntlmv2_resp *)
588 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
589 buf->blob_signature = cpu_to_le32(0x00000101);
590 buf->reserved = 0;
591 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
592 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
593 buf->reserved2 = 0;
594
595 memcpy(ses->auth_key.response + baselen, ses->tiblob, ses->tilen);
596
Steve French6d027cf2006-06-05 16:26:05 +0000597 /* calculate buf->ntlmv2_hash */
Steve French1717ffc2006-06-08 05:41:32 +0000598 rc = calc_ntlmv2_hash(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500599 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000600 cERROR(1, "could not get v2 hash rc %d", rc);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500601 goto setup_ntlmv2_rsp_ret;
602 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500603 rc = CalcNTLMv2_response(ses);
604 if (rc) {
605 cERROR(1, "Could not calculate CR1 rc: %d", rc);
606 goto setup_ntlmv2_rsp_ret;
607 }
Steve Frenchb609f062007-07-09 07:55:14 +0000608
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500609 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500610 crypto_shash_setkey(ses->server->secmech.hmacmd5,
611 ses->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
612
613 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
614 if (rc) {
615 cERROR(1, "%s: Could not init hmacmd5\n", __func__);
616 goto setup_ntlmv2_rsp_ret;
617 }
618
619 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
620 ses->auth_key.response + CIFS_SESS_KEY_SIZE,
621 CIFS_HMAC_MD5_HASH_SIZE);
622
623 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
624 ses->auth_key.response);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500625
626 return 0;
627
628setup_ntlmv2_rsp_ret:
629 kfree(ses->tiblob);
630 ses->tiblob = NULL;
631 ses->tilen = 0;
632
633 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000634}
635
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500636int
637calc_seckey(struct cifsSesInfo *ses)
638{
639 int rc;
640 struct crypto_blkcipher *tfm_arc4;
641 struct scatterlist sgin, sgout;
642 struct blkcipher_desc desc;
643 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
644
645 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
646
647 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
648 if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
649 cERROR(1, "could not allocate crypto API arc4\n");
650 return PTR_ERR(tfm_arc4);
651 }
652
653 desc.tfm = tfm_arc4;
654
655 crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
656 CIFS_SESS_KEY_SIZE);
657
658 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
659 sg_init_one(&sgout, ses->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
660
661 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
662 if (rc) {
663 cERROR(1, "could not encrypt session key rc: %d\n", rc);
664 crypto_free_blkcipher(tfm_arc4);
665 return rc;
666 }
667
668 /* make secondary_key/nonce as session key */
669 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
670 /* and make len as that of session key only */
671 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
672
673 crypto_free_blkcipher(tfm_arc4);
674
675 return 0;
676}
677
678void
679cifs_crypto_shash_release(struct TCP_Server_Info *server)
680{
681 if (server->secmech.md5)
682 crypto_free_shash(server->secmech.md5);
683
684 if (server->secmech.hmacmd5)
685 crypto_free_shash(server->secmech.hmacmd5);
686
687 kfree(server->secmech.sdeschmacmd5);
688
689 kfree(server->secmech.sdescmd5);
690}
691
692int
693cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
694{
695 int rc;
696 unsigned int size;
697
698 server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
699 if (!server->secmech.hmacmd5 ||
700 IS_ERR(server->secmech.hmacmd5)) {
701 cERROR(1, "could not allocate crypto hmacmd5\n");
702 return PTR_ERR(server->secmech.hmacmd5);
703 }
704
705 server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
706 if (!server->secmech.md5 || IS_ERR(server->secmech.md5)) {
707 cERROR(1, "could not allocate crypto md5\n");
708 rc = PTR_ERR(server->secmech.md5);
709 goto crypto_allocate_md5_fail;
710 }
711
712 size = sizeof(struct shash_desc) +
713 crypto_shash_descsize(server->secmech.hmacmd5);
714 server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
715 if (!server->secmech.sdeschmacmd5) {
716 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
717 rc = -ENOMEM;
718 goto crypto_allocate_hmacmd5_sdesc_fail;
719 }
720 server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
721 server->secmech.sdeschmacmd5->shash.flags = 0x0;
722
723
724 size = sizeof(struct shash_desc) +
725 crypto_shash_descsize(server->secmech.md5);
726 server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
727 if (!server->secmech.sdescmd5) {
728 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
729 rc = -ENOMEM;
730 goto crypto_allocate_md5_sdesc_fail;
731 }
732 server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
733 server->secmech.sdescmd5->shash.flags = 0x0;
734
735 return 0;
736
737crypto_allocate_md5_sdesc_fail:
738 kfree(server->secmech.sdeschmacmd5);
739
740crypto_allocate_hmacmd5_sdesc_fail:
741 crypto_free_shash(server->secmech.md5);
742
743crypto_allocate_md5_fail:
744 crypto_free_shash(server->secmech.hmacmd5);
745
746 return rc;
747}