blob: 66f3d50d067682aa2203e30676550cce6421ec3a [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
Jeff Laytona0f8b4f2011-01-07 11:30:28 -050075/* must be called with server->srv_mutex held */
Steve Frenchffdd6e42007-06-24 21:15:44 +000076int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
77 __u32 *pexpected_response_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int rc = 0;
80 char smb_signature[20];
81
Steve Frenchffdd6e42007-06-24 21:15:44 +000082 if ((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -EINVAL;
84
Steve Frenchffdd6e42007-06-24 21:15:44 +000085 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return rc;
87
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
Shirish Pargaonkar21e73392010-10-21 06:42:55 -050095 rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +000096 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
98 else
99 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
100
101 return rc;
102}
103
Steve Frenchffdd6e42007-06-24 21:15:44 +0000104static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500105 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800106{
Steve Frenche9917a02006-03-31 21:22:00 +0000107 int i;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500108 int rc;
Steve French84afc292005-12-02 13:32:45 -0800109
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500110 if (iov == NULL || signature == NULL || server == NULL)
Steve Frenche9917a02006-03-31 21:22:00 +0000111 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800112
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500113 if (!server->secmech.sdescmd5) {
114 cERROR(1, "%s: Can't generate signature\n", __func__);
115 return -1;
116 }
117
118 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
119 if (rc) {
120 cERROR(1, "%s: Oould not init md5\n", __func__);
121 return rc;
122 }
123
124 crypto_shash_update(&server->secmech.sdescmd5->shash,
125 server->session_key.response, server->session_key.len);
126
Steve French50c2f752007-07-13 00:33:32 +0000127 for (i = 0; i < n_vec; i++) {
Jeff Layton745542e2007-11-03 04:34:04 +0000128 if (iov[i].iov_len == 0)
129 continue;
Steve Frenchffdd6e42007-06-24 21:15:44 +0000130 if (iov[i].iov_base == NULL) {
Steve French56234e22010-09-08 20:57:05 +0000131 cERROR(1, "null iovec entry");
Steve Frenche9917a02006-03-31 21:22:00 +0000132 return -EIO;
Jeff Layton745542e2007-11-03 04:34:04 +0000133 }
Steve Frenchffdd6e42007-06-24 21:15:44 +0000134 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +0000135 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000136 if (i == 0) {
Steve French63d25832007-11-05 21:46:10 +0000137 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
Steve Frenche9917a02006-03-31 21:22:00 +0000138 break; /* nothing to sign or corrupt header */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500139 crypto_shash_update(&server->secmech.sdescmd5->shash,
140 iov[i].iov_base + 4, iov[i].iov_len - 4);
Steve Frenche9917a02006-03-31 21:22:00 +0000141 } else
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500142 crypto_shash_update(&server->secmech.sdescmd5->shash,
143 iov[i].iov_base, iov[i].iov_len);
Steve Frenche9917a02006-03-31 21:22:00 +0000144 }
Steve French84afc292005-12-02 13:32:45 -0800145
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500146 rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
Steve French84afc292005-12-02 13:32:45 -0800147
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500148 return rc;
Steve French84afc292005-12-02 13:32:45 -0800149}
150
Jeff Laytona0f8b4f2011-01-07 11:30:28 -0500151/* must be called with server->srv_mutex held */
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 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800166 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000167 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800168
Steve Frenchffdd6e42007-06-24 21:15:44 +0000169 *pexpected_response_sequence_number = server->sequence_number++;
170 server->sequence_number++;
Steve French84afc292005-12-02 13:32:45 -0800171
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500172 rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000173 if (rc)
174 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
175 else
176 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800177
Steve Frenchffdd6e42007-06-24 21:15:44 +0000178 return rc;
Steve French84afc292005-12-02 13:32:45 -0800179}
180
Steve Frenchb609f062007-07-09 07:55:14 +0000181int cifs_verify_signature(struct smb_hdr *cifs_pdu,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500182 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000183 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000185 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 char server_response_sig[8];
187 char what_we_think_sig_should_be[20];
188
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500189 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EINVAL;
191
192 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
193 return 0;
194
195 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000196 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000197 (struct smb_com_lock_req *)cifs_pdu;
198 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
200 }
201
Steve French50c2f752007-07-13 00:33:32 +0000202 /* BB what if signatures are supposed to be on for session but
203 server does not send one? BB */
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000206 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000207 cFYI(1, "dummy signature received for smb command 0x%x",
208 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* save off the origiginal signature so we can modify the smb and check
211 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000212 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Steve French50c2f752007-07-13 00:33:32 +0000214 cifs_pdu->Signature.Sequence.SequenceNumber =
215 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 cifs_pdu->Signature.Sequence.Reserved = 0;
217
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500218 rc = cifs_calculate_signature(cifs_pdu, server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 what_we_think_sig_should_be);
220
Steve French50c2f752007-07-13 00:33:32 +0000221 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return rc;
223
Steve French50c2f752007-07-13 00:33:32 +0000224/* cifs_dump_mem("what we think it should be: ",
225 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Steve French50c2f752007-07-13 00:33:32 +0000227 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EACCES;
229 else
230 return 0;
231
232}
233
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500234/* first calculate 24 bytes ntlm response and then 16 byte session key */
235int setup_ntlm_response(struct cifsSesInfo *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500237 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
238 char temp_key[CIFS_SESS_KEY_SIZE];
239
240 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -EINVAL;
242
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500243 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
244 if (!ses->auth_key.response) {
245 cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len);
246 return -ENOMEM;
247 }
248 ses->auth_key.len = temp_len;
249
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500250 SMBNTencrypt(ses->password, ses->server->cryptkey,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500251 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
252
253 E_md4hash(ses->password, temp_key);
254 mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257}
258
Steve French7c7b25b2006-06-01 19:20:10 +0000259#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500260void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
261 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000262{
263 int i;
264 char password_with_pad[CIFS_ENCPWD_SIZE];
265
266 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500267 if (password)
268 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000269
Jeff Layton04912d62010-04-24 07:57:45 -0400270 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500271 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
272 memcpy(lnm_session_key, password_with_pad,
273 CIFS_ENCPWD_SIZE);
274 return;
275 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000276
Steve French7c7b25b2006-06-01 19:20:10 +0000277 /* calculate old style session key */
278 /* calling toupper is less broken than repeatedly
279 calling nls_toupper would be since that will never
280 work for UTF8, but neither handles multibyte code pages
281 but the only alternative would be converting to UCS-16 (Unicode)
282 (using a routine something like UniStrupr) then
283 uppercasing and then converting back from Unicode - which
284 would only worth doing it if we knew it were utf8. Basically
285 utf8 and other multibyte codepages each need their own strupper
286 function since a byte at a time will ont work. */
287
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000288 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000289 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000290
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500291 SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
292
Steve French7c7b25b2006-06-01 19:20:10 +0000293 /* clear password before we return/free memory */
294 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
295}
296#endif /* CIFS_WEAK_PW_HASH */
297
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500298/* Build a proper attribute value/target info pairs blob.
299 * Fill in netbios and dns domain name and workstation name
300 * and client time (total five av pairs and + one end of fields indicator.
301 * Allocate domain name which gets freed when session struct is deallocated.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500302 */
303static int
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500304build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500305{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500306 unsigned int dlen;
307 unsigned int wlen;
308 unsigned int size = 6 * sizeof(struct ntlmssp2_name);
309 __le64 curtime;
310 char *defdmname = "WORKGROUP";
311 unsigned char *blobptr;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500312 struct ntlmssp2_name *attrptr;
313
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500314 if (!ses->domainName) {
315 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
316 if (!ses->domainName)
317 return -ENOMEM;
318 }
319
320 dlen = strlen(ses->domainName);
321 wlen = strlen(ses->server->hostname);
322
323 /* The length of this blob is a size which is
324 * six times the size of a structure which holds name/size +
325 * two times the unicode length of a domain name +
326 * two times the unicode length of a server name +
327 * size of a timestamp (which is 8 bytes).
328 */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500329 ses->auth_key.len = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
330 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
331 if (!ses->auth_key.response) {
332 ses->auth_key.len = 0;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500333 cERROR(1, "Challenge target info allocation failure");
334 return -ENOMEM;
335 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500336
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500337 blobptr = ses->auth_key.response;
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500338 attrptr = (struct ntlmssp2_name *) blobptr;
339
340 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
341 attrptr->length = cpu_to_le16(2 * dlen);
342 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
343 cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
344
345 blobptr += 2 * dlen;
346 attrptr = (struct ntlmssp2_name *) blobptr;
347
348 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_COMPUTER_NAME);
349 attrptr->length = cpu_to_le16(2 * wlen);
350 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
351 cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
352
353 blobptr += 2 * wlen;
354 attrptr = (struct ntlmssp2_name *) blobptr;
355
356 attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_DOMAIN_NAME);
357 attrptr->length = cpu_to_le16(2 * dlen);
358 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
359 cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
360
361 blobptr += 2 * dlen;
362 attrptr = (struct ntlmssp2_name *) blobptr;
363
364 attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_COMPUTER_NAME);
365 attrptr->length = cpu_to_le16(2 * wlen);
366 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
367 cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
368
369 blobptr += 2 * wlen;
370 attrptr = (struct ntlmssp2_name *) blobptr;
371
372 attrptr->type = cpu_to_le16(NTLMSSP_AV_TIMESTAMP);
373 attrptr->length = cpu_to_le16(sizeof(__le64));
374 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
375 curtime = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
376 memcpy(blobptr, &curtime, sizeof(__le64));
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500377
378 return 0;
379}
380
381/* Server has provided av pairs/target info in the type 2 challenge
382 * packet and we have plucked it and stored within smb session.
383 * We parse that blob here to find netbios domain name to be used
384 * as part of ntlmv2 authentication (in Target String), if not already
385 * specified on the command line.
386 * If this function returns without any error but without fetching
387 * domain name, authentication may fail against some server but
388 * may not fail against other (those who are not very particular
389 * about target string i.e. for some, just user name might suffice.
390 */
391static int
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -0500392find_domain_name(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500393{
394 unsigned int attrsize;
395 unsigned int type;
396 unsigned int onesize = sizeof(struct ntlmssp2_name);
397 unsigned char *blobptr;
398 unsigned char *blobend;
399 struct ntlmssp2_name *attrptr;
400
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500401 if (!ses->auth_key.len || !ses->auth_key.response)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500402 return 0;
403
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500404 blobptr = ses->auth_key.response;
405 blobend = blobptr + ses->auth_key.len;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500406
407 while (blobptr + onesize < blobend) {
408 attrptr = (struct ntlmssp2_name *) blobptr;
409 type = le16_to_cpu(attrptr->type);
410 if (type == NTLMSSP_AV_EOL)
411 break;
412 blobptr += 2; /* advance attr type */
413 attrsize = le16_to_cpu(attrptr->length);
414 blobptr += 2; /* advance attr size */
415 if (blobptr + attrsize > blobend)
416 break;
417 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
418 if (!attrsize)
419 break;
420 if (!ses->domainName) {
421 ses->domainName =
422 kmalloc(attrsize + 1, GFP_KERNEL);
423 if (!ses->domainName)
424 return -ENOMEM;
425 cifs_from_ucs2(ses->domainName,
426 (__le16 *)blobptr, attrsize, attrsize,
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -0500427 nls_cp, false);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500428 break;
429 }
430 }
431 blobptr += attrsize; /* advance attr value */
432 }
433
434 return 0;
435}
436
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500437static int calc_ntlmv2_hash(struct cifsSesInfo *ses, char *ntlmv2_hash,
Steve French50c2f752007-07-13 00:33:32 +0000438 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000439{
440 int rc = 0;
441 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500442 char nt_hash[CIFS_NTHASH_SIZE];
Steve French50c2f752007-07-13 00:33:32 +0000443 wchar_t *user;
444 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500445 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000446
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500447 if (!ses->server->secmech.sdeschmacmd5) {
448 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
449 return -1;
450 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000451
452 /* calculate md4 hash of password */
453 E_md4hash(ses->password, nt_hash);
454
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500455 crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
456 CIFS_NTHASH_SIZE);
457
458 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
459 if (rc) {
460 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
461 return rc;
462 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000463
464 /* convert ses->userName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000465 len = strlen(ses->userName);
466 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500467 if (user == NULL) {
468 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
469 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000470 goto calc_exit_2;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500471 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000472 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
Steve French1717ffc2006-06-08 05:41:32 +0000473 UniStrupr(user);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500474
475 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
476 (char *)user, 2 * len);
Steve Frencha8ee0342006-06-05 23:34:19 +0000477
478 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000479 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000480 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000481
Steve French50c2f752007-07-13 00:33:32 +0000482 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500483 if (domain == NULL) {
484 cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
485 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000486 goto calc_exit_1;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500487 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000488 len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
489 nls_cp);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500490 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
491 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000492 kfree(domain);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500493 } else if (ses->serverName) {
494 len = strlen(ses->serverName);
495
496 server = kmalloc(2 + (len * 2), GFP_KERNEL);
497 if (server == NULL) {
498 cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
499 rc = -ENOMEM;
500 goto calc_exit_1;
501 }
502 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
503 nls_cp);
504 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
505 (char *)server, 2 * len);
506 kfree(server);
Steve French1717ffc2006-06-08 05:41:32 +0000507 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500508
509 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500510 ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500511
Steve French1717ffc2006-06-08 05:41:32 +0000512calc_exit_1:
513 kfree(user);
514calc_exit_2:
Steve Frencha8ee0342006-06-05 23:34:19 +0000515 return rc;
516}
517
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500518static int
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500519CalcNTLMv2_response(const struct cifsSesInfo *ses, char *ntlmv2_hash)
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500520{
521 int rc;
522 unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
523
524 if (!ses->server->secmech.sdeschmacmd5) {
525 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
526 return -1;
527 }
528
529 crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500530 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500531
532 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
533 if (rc) {
534 cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
535 return rc;
536 }
537
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500538 if (ses->server->secType == RawNTLMSSP)
539 memcpy(ses->auth_key.response + offset,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500540 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500541 else
542 memcpy(ses->auth_key.response + offset,
543 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500544 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
545 ses->auth_key.response + offset, ses->auth_key.len - offset);
546
547 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
548 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
549
550 return rc;
551}
552
553
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500554int
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500555setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000556{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000557 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500558 int baselen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500559 unsigned int tilen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500560 struct ntlmv2_resp *buf;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500561 char ntlmv2_hash[16];
562 unsigned char *tiblob = NULL; /* target info blob */
Steve French6d027cf2006-06-05 16:26:05 +0000563
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500564 if (ses->server->secType == RawNTLMSSP) {
565 if (!ses->domainName) {
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -0500566 rc = find_domain_name(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500567 if (rc) {
568 cERROR(1, "error %d finding domain name", rc);
569 goto setup_ntlmv2_rsp_ret;
570 }
571 }
572 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500573 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500574 if (rc) {
575 cERROR(1, "error %d building av pair blob", rc);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500576 goto setup_ntlmv2_rsp_ret;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500577 }
578 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000579
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500580 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500581 tilen = ses->auth_key.len;
582 tiblob = ses->auth_key.response;
583
584 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500585 if (!ses->auth_key.response) {
586 rc = ENOMEM;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500587 ses->auth_key.len = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500588 cERROR(1, "%s: Can't allocate auth blob", __func__);
589 goto setup_ntlmv2_rsp_ret;
590 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500591 ses->auth_key.len += baselen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500592
593 buf = (struct ntlmv2_resp *)
594 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
595 buf->blob_signature = cpu_to_le32(0x00000101);
596 buf->reserved = 0;
597 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
598 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
599 buf->reserved2 = 0;
600
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500601 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500602
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -0500603 /* calculate ntlmv2_hash */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500604 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500605 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000606 cERROR(1, "could not get v2 hash rc %d", rc);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500607 goto setup_ntlmv2_rsp_ret;
608 }
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -0500609
610 /* calculate first part of the client response (CR1) */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500611 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500612 if (rc) {
613 cERROR(1, "Could not calculate CR1 rc: %d", rc);
614 goto setup_ntlmv2_rsp_ret;
615 }
Steve Frenchb609f062007-07-09 07:55:14 +0000616
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500617 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500618 crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500619 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500620
621 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
622 if (rc) {
623 cERROR(1, "%s: Could not init hmacmd5\n", __func__);
624 goto setup_ntlmv2_rsp_ret;
625 }
626
627 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
628 ses->auth_key.response + CIFS_SESS_KEY_SIZE,
629 CIFS_HMAC_MD5_HASH_SIZE);
630
631 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
632 ses->auth_key.response);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500633
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500634setup_ntlmv2_rsp_ret:
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500635 kfree(tiblob);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500636
637 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000638}
639
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500640int
641calc_seckey(struct cifsSesInfo *ses)
642{
643 int rc;
644 struct crypto_blkcipher *tfm_arc4;
645 struct scatterlist sgin, sgout;
646 struct blkcipher_desc desc;
647 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
648
649 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
650
651 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
652 if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
653 cERROR(1, "could not allocate crypto API arc4\n");
654 return PTR_ERR(tfm_arc4);
655 }
656
657 desc.tfm = tfm_arc4;
658
659 crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
660 CIFS_SESS_KEY_SIZE);
661
662 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500663 sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500664
665 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
666 if (rc) {
667 cERROR(1, "could not encrypt session key rc: %d\n", rc);
668 crypto_free_blkcipher(tfm_arc4);
669 return rc;
670 }
671
672 /* make secondary_key/nonce as session key */
673 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
674 /* and make len as that of session key only */
675 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
676
677 crypto_free_blkcipher(tfm_arc4);
678
679 return 0;
680}
681
682void
683cifs_crypto_shash_release(struct TCP_Server_Info *server)
684{
685 if (server->secmech.md5)
686 crypto_free_shash(server->secmech.md5);
687
688 if (server->secmech.hmacmd5)
689 crypto_free_shash(server->secmech.hmacmd5);
690
691 kfree(server->secmech.sdeschmacmd5);
692
693 kfree(server->secmech.sdescmd5);
694}
695
696int
697cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
698{
699 int rc;
700 unsigned int size;
701
702 server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
703 if (!server->secmech.hmacmd5 ||
704 IS_ERR(server->secmech.hmacmd5)) {
705 cERROR(1, "could not allocate crypto hmacmd5\n");
706 return PTR_ERR(server->secmech.hmacmd5);
707 }
708
709 server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
710 if (!server->secmech.md5 || IS_ERR(server->secmech.md5)) {
711 cERROR(1, "could not allocate crypto md5\n");
712 rc = PTR_ERR(server->secmech.md5);
713 goto crypto_allocate_md5_fail;
714 }
715
716 size = sizeof(struct shash_desc) +
717 crypto_shash_descsize(server->secmech.hmacmd5);
718 server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
719 if (!server->secmech.sdeschmacmd5) {
720 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
721 rc = -ENOMEM;
722 goto crypto_allocate_hmacmd5_sdesc_fail;
723 }
724 server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
725 server->secmech.sdeschmacmd5->shash.flags = 0x0;
726
727
728 size = sizeof(struct shash_desc) +
729 crypto_shash_descsize(server->secmech.md5);
730 server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
731 if (!server->secmech.sdescmd5) {
732 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
733 rc = -ENOMEM;
734 goto crypto_allocate_md5_sdesc_fail;
735 }
736 server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
737 server->secmech.sdescmd5->shash.flags = 0x0;
738
739 return 0;
740
741crypto_allocate_md5_sdesc_fail:
742 kfree(server->secmech.sdeschmacmd5);
743
744crypto_allocate_hmacmd5_sdesc_fail:
745 crypto_free_shash(server->secmech.md5);
746
747crypto_allocate_md5_fail:
748 crypto_free_shash(server->secmech.hmacmd5);
749
750 return rc;
751}