blob: d3e14d1fc0b5e1ba2f8f3161bf27c4afaca4eb96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsencrypt.c
3 *
Steve French8b7a4542015-03-30 16:58:17 -05004 * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP
5 * for more detailed information
6 *
Steve French95dc8dd2013-07-04 10:35:21 -05007 * Copyright (C) International Business Machines Corp., 2005,2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Author(s): Steve French (sfrench@us.ibm.com)
9 *
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "cifspdu.h"
Steve Frenchffdd6e42007-06-24 21:15:44 +000028#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "cifs_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "cifs_unicode.h"
31#include "cifsproto.h"
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -050032#include "ntlmssp.h"
Steve French7c7b25b2006-06-01 19:20:10 +000033#include <linux/ctype.h>
Steve French6d027cf2006-06-05 16:26:05 +000034#include <linux/random.h>
Jeff Laytonfb308a62012-09-18 16:20:35 -070035#include <linux/highmem.h>
Herbert Xu9651ddb2016-01-24 21:17:17 +080036#include <crypto/skcipher.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070037#include <crypto/aead.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Al Viro16c568e2015-11-12 22:46:49 -050039int __cifs_calc_signature(struct smb_rqst *rqst,
Aurelien Aptel57f933c2018-06-04 22:29:34 +020040 int start,
Al Viro16c568e2015-11-12 22:46:49 -050041 struct TCP_Server_Info *server, char *signature,
42 struct shash_desc *shash)
43{
44 int i;
45 int rc;
46 struct kvec *iov = rqst->rq_iov;
47 int n_vec = rqst->rq_nvec;
48
Aurelien Aptel57f933c2018-06-04 22:29:34 +020049 for (i = start; i < n_vec; i++) {
Al Viro16c568e2015-11-12 22:46:49 -050050 if (iov[i].iov_len == 0)
51 continue;
52 if (iov[i].iov_base == NULL) {
53 cifs_dbg(VFS, "null iovec entry\n");
54 return -EIO;
55 }
Pavel Shilovsky738f9de2016-11-23 15:14:57 -080056 if (i == 1 && iov[1].iov_len <= 4)
57 break; /* nothing to sign or corrupt header */
58 rc = crypto_shash_update(shash,
59 iov[i].iov_base, iov[i].iov_len);
Al Viro16c568e2015-11-12 22:46:49 -050060 if (rc) {
61 cifs_dbg(VFS, "%s: Could not update with payload\n",
62 __func__);
63 return rc;
64 }
65 }
66
67 /* now hash over the rq_pages array */
68 for (i = 0; i < rqst->rq_npages; i++) {
69 void *kaddr = kmap(rqst->rq_pages[i]);
70 size_t len = rqst->rq_pagesz;
71
72 if (i == rqst->rq_npages - 1)
73 len = rqst->rq_tailsz;
74
75 crypto_shash_update(shash, kaddr, len);
76
77 kunmap(rqst->rq_pages[i]);
78 }
79
80 rc = crypto_shash_final(shash, signature);
81 if (rc)
82 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
83
84 return rc;
85}
86
Jeff Layton157c2492011-04-02 07:34:30 -040087/*
88 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
89 * The 16 byte signature must be allocated by the caller. Note we only use the
90 * 1st eight bytes and that the smb header signature field on input contains
91 * the sequence number before this function is called. Also, this function
92 * should be called with the server->srv_mutex held.
93 */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -070094static int cifs_calc_signature(struct smb_rqst *rqst,
Jeff Layton826a95e2011-10-11 06:41:32 -040095 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -080096{
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050097 int rc;
Steve French84afc292005-12-02 13:32:45 -080098
Al Viro16c568e2015-11-12 22:46:49 -050099 if (!rqst->rq_iov || !signature || !server)
Steve Frenche9917a02006-03-31 21:22:00 +0000100 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800101
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100102 rc = cifs_alloc_hash("md5", &server->secmech.md5,
103 &server->secmech.sdescmd5);
104 if (rc)
105 return -1;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500106
107 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
108 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500109 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500110 return rc;
111 }
112
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500113 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500114 server->session_key.response, server->session_key.len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500115 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500116 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500117 return rc;
118 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500119
Aurelien Aptel57f933c2018-06-04 22:29:34 +0200120 return __cifs_calc_signature(rqst, 1, server, signature,
Al Viro16c568e2015-11-12 22:46:49 -0500121 &server->secmech.sdescmd5->shash);
Steve French84afc292005-12-02 13:32:45 -0800122}
123
Jeff Laytona0f8b4f2011-01-07 11:30:28 -0500124/* must be called with server->srv_mutex held */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700125int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000126 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800127{
128 int rc = 0;
129 char smb_signature[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700130 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800131
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800132 if (rqst->rq_iov[0].iov_len != 4 ||
133 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
134 return -EIO;
135
Steve Frenchffdd6e42007-06-24 21:15:44 +0000136 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800137 return -EINVAL;
138
Jeff Layton998d6fc2011-07-26 12:21:17 -0400139 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
140 server->tcpStatus == CifsNeedNegotiate)
Steve French84afc292005-12-02 13:32:45 -0800141 return rc;
142
Jeff Layton998d6fc2011-07-26 12:21:17 -0400143 if (!server->session_estab) {
Jeff Laytonb4dacbc2011-10-11 06:41:32 -0400144 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
Jeff Layton998d6fc2011-07-26 12:21:17 -0400145 return rc;
146 }
147
Steve Frenchffdd6e42007-06-24 21:15:44 +0000148 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800149 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000150 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800151
Jeff Layton0124cc42013-04-03 11:55:03 -0400152 *pexpected_response_sequence_number = ++server->sequence_number;
153 ++server->sequence_number;
Steve French84afc292005-12-02 13:32:45 -0800154
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700155 rc = cifs_calc_signature(rqst, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000156 if (rc)
157 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
158 else
159 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800160
Steve Frenchffdd6e42007-06-24 21:15:44 +0000161 return rc;
Steve French84afc292005-12-02 13:32:45 -0800162}
163
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700164int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
165 __u32 *pexpected_response_sequence)
166{
167 struct smb_rqst rqst = { .rq_iov = iov,
168 .rq_nvec = n_vec };
169
170 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
171}
172
Jeff Layton826a95e2011-10-11 06:41:32 -0400173/* must be called with server->srv_mutex held */
174int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
175 __u32 *pexpected_response_sequence_number)
176{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800177 struct kvec iov[2];
Jeff Layton826a95e2011-10-11 06:41:32 -0400178
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800179 iov[0].iov_base = cifs_pdu;
180 iov[0].iov_len = 4;
181 iov[1].iov_base = (char *)cifs_pdu + 4;
182 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
Jeff Layton826a95e2011-10-11 06:41:32 -0400183
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800184 return cifs_sign_smbv(iov, 2, server,
Jeff Layton826a95e2011-10-11 06:41:32 -0400185 pexpected_response_sequence_number);
186}
187
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700188int cifs_verify_signature(struct smb_rqst *rqst,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500189 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000190 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000192 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 char server_response_sig[8];
194 char what_we_think_sig_should_be[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700195 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800197 if (rqst->rq_iov[0].iov_len != 4 ||
198 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
199 return -EIO;
200
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500201 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EINVAL;
203
Jeff Layton9c4843e2011-06-06 15:40:23 -0400204 if (!server->session_estab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206
207 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000208 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000209 (struct smb_com_lock_req *)cifs_pdu;
210 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212 }
213
Steve French50c2f752007-07-13 00:33:32 +0000214 /* BB what if signatures are supposed to be on for session but
215 server does not send one? BB */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000218 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500219 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
220 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* save off the origiginal signature so we can modify the smb and check
223 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000224 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Steve French50c2f752007-07-13 00:33:32 +0000226 cifs_pdu->Signature.Sequence.SequenceNumber =
227 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 cifs_pdu->Signature.Sequence.Reserved = 0;
229
Jeff Layton157c2492011-04-02 07:34:30 -0400230 mutex_lock(&server->srv_mutex);
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700231 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
Jeff Layton157c2492011-04-02 07:34:30 -0400232 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Steve French50c2f752007-07-13 00:33:32 +0000234 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return rc;
236
Steve French50c2f752007-07-13 00:33:32 +0000237/* cifs_dump_mem("what we think it should be: ",
238 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Steve French50c2f752007-07-13 00:33:32 +0000240 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -EACCES;
242 else
243 return 0;
244
245}
246
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500247/* first calculate 24 bytes ntlm response and then 16 byte session key */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500248int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600250 int rc = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500251 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
252 char temp_key[CIFS_SESS_KEY_SIZE];
253
254 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500257 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -0500258 if (!ses->auth_key.response)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500259 return -ENOMEM;
Joe Perchesf96637b2013-05-04 22:12:25 -0500260
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500261 ses->auth_key.len = temp_len;
262
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600263 rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500264 ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600265 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500266 cifs_dbg(FYI, "%s Can't generate NTLM response, error: %d\n",
267 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600268 return rc;
269 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500270
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500271 rc = E_md4hash(ses->password, temp_key, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600272 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500273 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
274 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600275 return rc;
276 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500277
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600278 rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
279 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500280 cifs_dbg(FYI, "%s Can't generate NTLM session key, error: %d\n",
281 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600282
283 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Steve French7c7b25b2006-06-01 19:20:10 +0000286#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French43988d72011-04-19 18:23:31 +0000287int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500288 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000289{
290 int i;
Steve French43988d72011-04-19 18:23:31 +0000291 int rc;
Aurelien Aptel97f4b722018-01-25 15:59:39 +0100292 char password_with_pad[CIFS_ENCPWD_SIZE] = {0};
Steve French7c7b25b2006-06-01 19:20:10 +0000293
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500294 if (password)
295 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000296
Jeff Layton04912d62010-04-24 07:57:45 -0400297 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500298 memcpy(lnm_session_key, password_with_pad,
299 CIFS_ENCPWD_SIZE);
Steve French43988d72011-04-19 18:23:31 +0000300 return 0;
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500301 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000302
Steve French7c7b25b2006-06-01 19:20:10 +0000303 /* calculate old style session key */
304 /* calling toupper is less broken than repeatedly
305 calling nls_toupper would be since that will never
306 work for UTF8, but neither handles multibyte code pages
307 but the only alternative would be converting to UCS-16 (Unicode)
308 (using a routine something like UniStrupr) then
309 uppercasing and then converting back from Unicode - which
310 would only worth doing it if we knew it were utf8. Basically
311 utf8 and other multibyte codepages each need their own strupper
312 function since a byte at a time will ont work. */
313
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000314 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000315 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000316
Steve French43988d72011-04-19 18:23:31 +0000317 rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500318
Steve French43988d72011-04-19 18:23:31 +0000319 return rc;
Steve French7c7b25b2006-06-01 19:20:10 +0000320}
321#endif /* CIFS_WEAK_PW_HASH */
322
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500323/* Build a proper attribute value/target info pairs blob.
324 * Fill in netbios and dns domain name and workstation name
325 * and client time (total five av pairs and + one end of fields indicator.
326 * Allocate domain name which gets freed when session struct is deallocated.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500327 */
328static int
Steve French96daf2b2011-05-27 04:34:02 +0000329build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500330{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500331 unsigned int dlen;
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500332 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500333 char *defdmname = "WORKGROUP";
334 unsigned char *blobptr;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500335 struct ntlmssp2_name *attrptr;
336
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500337 if (!ses->domainName) {
338 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
339 if (!ses->domainName)
340 return -ENOMEM;
341 }
342
343 dlen = strlen(ses->domainName);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500344
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500345 /*
346 * The length of this blob is two times the size of a
347 * structure (av pair) which holds name/size
348 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
349 * unicode length of a netbios domain name
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500350 */
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500351 ses->auth_key.len = size + 2 * dlen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500352 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
353 if (!ses->auth_key.response) {
354 ses->auth_key.len = 0;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500355 return -ENOMEM;
356 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500357
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500358 blobptr = ses->auth_key.response;
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500359 attrptr = (struct ntlmssp2_name *) blobptr;
360
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500361 /*
362 * As defined in MS-NTLM 3.3.2, just this av pair field
363 * is sufficient as part of the temp
364 */
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500365 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
366 attrptr->length = cpu_to_le16(2 * dlen);
367 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
Steve Frenchacbbb762012-01-18 22:32:33 -0600368 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500369
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500370 return 0;
371}
372
373/* Server has provided av pairs/target info in the type 2 challenge
374 * packet and we have plucked it and stored within smb session.
375 * We parse that blob here to find netbios domain name to be used
376 * as part of ntlmv2 authentication (in Target String), if not already
377 * specified on the command line.
378 * If this function returns without any error but without fetching
379 * domain name, authentication may fail against some server but
380 * may not fail against other (those who are not very particular
381 * about target string i.e. for some, just user name might suffice.
382 */
383static int
Steve French96daf2b2011-05-27 04:34:02 +0000384find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500385{
386 unsigned int attrsize;
387 unsigned int type;
388 unsigned int onesize = sizeof(struct ntlmssp2_name);
389 unsigned char *blobptr;
390 unsigned char *blobend;
391 struct ntlmssp2_name *attrptr;
392
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500393 if (!ses->auth_key.len || !ses->auth_key.response)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500394 return 0;
395
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500396 blobptr = ses->auth_key.response;
397 blobend = blobptr + ses->auth_key.len;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500398
399 while (blobptr + onesize < blobend) {
400 attrptr = (struct ntlmssp2_name *) blobptr;
401 type = le16_to_cpu(attrptr->type);
402 if (type == NTLMSSP_AV_EOL)
403 break;
404 blobptr += 2; /* advance attr type */
405 attrsize = le16_to_cpu(attrptr->length);
406 blobptr += 2; /* advance attr size */
407 if (blobptr + attrsize > blobend)
408 break;
409 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
Chen Gang057d6332013-07-19 09:01:36 +0800410 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500411 break;
412 if (!ses->domainName) {
413 ses->domainName =
414 kmalloc(attrsize + 1, GFP_KERNEL);
415 if (!ses->domainName)
416 return -ENOMEM;
Steve Frenchacbbb762012-01-18 22:32:33 -0600417 cifs_from_utf16(ses->domainName,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500418 (__le16 *)blobptr, attrsize, attrsize,
Steve Frenchb6938552014-09-25 13:20:05 -0500419 nls_cp, NO_MAP_UNI_RSVD);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500420 break;
421 }
422 }
423 blobptr += attrsize; /* advance attr value */
424 }
425
426 return 0;
427}
428
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200429/* Server has provided av pairs/target info in the type 2 challenge
430 * packet and we have plucked it and stored within smb session.
431 * We parse that blob here to find the server given timestamp
432 * as part of ntlmv2 authentication (or local current time as
433 * default in case of failure)
434 */
435static __le64
436find_timestamp(struct cifs_ses *ses)
437{
438 unsigned int attrsize;
439 unsigned int type;
440 unsigned int onesize = sizeof(struct ntlmssp2_name);
441 unsigned char *blobptr;
442 unsigned char *blobend;
443 struct ntlmssp2_name *attrptr;
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700444 struct timespec ts;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200445
446 if (!ses->auth_key.len || !ses->auth_key.response)
447 return 0;
448
449 blobptr = ses->auth_key.response;
450 blobend = blobptr + ses->auth_key.len;
451
452 while (blobptr + onesize < blobend) {
453 attrptr = (struct ntlmssp2_name *) blobptr;
454 type = le16_to_cpu(attrptr->type);
455 if (type == NTLMSSP_AV_EOL)
456 break;
457 blobptr += 2; /* advance attr type */
458 attrsize = le16_to_cpu(attrptr->length);
459 blobptr += 2; /* advance attr size */
460 if (blobptr + attrsize > blobend)
461 break;
462 if (type == NTLMSSP_AV_TIMESTAMP) {
463 if (attrsize == sizeof(u64))
464 return *((__le64 *)blobptr);
465 }
466 blobptr += attrsize; /* advance attr value */
467 }
468
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700469 ktime_get_real_ts(&ts);
470 return cpu_to_le64(cifs_UnixTimeToNT(ts));
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200471}
472
Steve French96daf2b2011-05-27 04:34:02 +0000473static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
Steve French50c2f752007-07-13 00:33:32 +0000474 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000475{
476 int rc = 0;
477 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500478 char nt_hash[CIFS_NTHASH_SIZE];
Steve Frenchfdf96a92013-06-25 14:03:16 -0500479 __le16 *user;
Steve French50c2f752007-07-13 00:33:32 +0000480 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500481 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000482
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500483 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500484 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500485 return -1;
486 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000487
488 /* calculate md4 hash of password */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500489 E_md4hash(ses->password, nt_hash, nls_cp);
Steve Frencha8ee0342006-06-05 23:34:19 +0000490
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500491 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500492 CIFS_NTHASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500493 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500494 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500495 return rc;
496 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500497
498 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
499 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500500 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500501 return rc;
502 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000503
Steve Frenchfdf96a92013-06-25 14:03:16 -0500504 /* convert ses->user_name to unicode */
Jeff Layton04febab2012-01-17 16:09:15 -0500505 len = ses->user_name ? strlen(ses->user_name) : 0;
Steve French1717ffc2006-06-08 05:41:32 +0000506 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500507 if (user == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500508 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500509 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500510 }
Jeff Layton04febab2012-01-17 16:09:15 -0500511
512 if (len) {
Steve Frenchfdf96a92013-06-25 14:03:16 -0500513 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
Jeff Layton04febab2012-01-17 16:09:15 -0500514 UniStrupr(user);
515 } else {
516 memset(user, '\0', 2);
517 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500518
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500519 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500520 (char *)user, 2 * len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500521 kfree(user);
522 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500523 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500524 return rc;
525 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000526
527 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000528 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000529 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000530
Steve French50c2f752007-07-13 00:33:32 +0000531 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500532 if (domain == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500533 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500534 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500535 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600536 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
537 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500538 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500539 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
540 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000541 kfree(domain);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500542 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500543 cifs_dbg(VFS, "%s: Could not update with domain\n",
544 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500545 return rc;
546 }
Steve French8b7a4542015-03-30 16:58:17 -0500547 } else {
548 /* We use ses->serverName if no domain name available */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500549 len = strlen(ses->serverName);
550
551 server = kmalloc(2 + (len * 2), GFP_KERNEL);
552 if (server == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500553 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500554 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500555 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600556 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500557 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500558 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500559 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
560 (char *)server, 2 * len);
561 kfree(server);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500562 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500563 cifs_dbg(VFS, "%s: Could not update with server\n",
564 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500565 return rc;
566 }
Steve French1717ffc2006-06-08 05:41:32 +0000567 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500568
569 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500570 ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500571 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500572 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500573
Steve Frencha8ee0342006-06-05 23:34:19 +0000574 return rc;
575}
576
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500577static int
Steve French96daf2b2011-05-27 04:34:02 +0000578CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500579{
580 int rc;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700581 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
582 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
583 unsigned int hash_len;
584
585 /* The MD5 hash starts at challenge_key.key */
586 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
587 offsetof(struct ntlmv2_resp, challenge.key[0]));
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500588
589 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500590 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500591 return -1;
592 }
593
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500594 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700595 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500596 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500597 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
598 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500599 return rc;
600 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500601
602 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
603 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500604 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500605 return rc;
606 }
607
Jeff Layton3f618222013-06-12 19:52:14 -0500608 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
Tim Gardner2c957dd2013-11-07 16:40:57 -0700609 memcpy(ntlmv2->challenge.key,
610 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500611 else
Tim Gardner2c957dd2013-11-07 16:40:57 -0700612 memcpy(ntlmv2->challenge.key,
613 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500614 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700615 ntlmv2->challenge.key, hash_len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500616 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500617 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500618 return rc;
619 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500620
Tim Gardner2c957dd2013-11-07 16:40:57 -0700621 /* Note that the MD5 digest over writes anon.challenge_key.key */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500622 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700623 ntlmv2->ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500624 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500625 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500626
627 return rc;
628}
629
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500630int
Steve French96daf2b2011-05-27 04:34:02 +0000631setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000632{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000633 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500634 int baselen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500635 unsigned int tilen;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700636 struct ntlmv2_resp *ntlmv2;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500637 char ntlmv2_hash[16];
638 unsigned char *tiblob = NULL; /* target info blob */
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200639 __le64 rsp_timestamp;
Steve French6d027cf2006-06-05 16:26:05 +0000640
Jeff Layton3f618222013-06-12 19:52:14 -0500641 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500642 if (!ses->domainName) {
Germano Percossi39566442016-12-15 12:31:18 +0530643 if (ses->domainAuto) {
644 rc = find_domain_name(ses, nls_cp);
645 if (rc) {
646 cifs_dbg(VFS, "error %d finding domain name\n",
647 rc);
648 goto setup_ntlmv2_rsp_ret;
649 }
650 } else {
651 ses->domainName = kstrdup("", GFP_KERNEL);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500652 }
653 }
654 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500655 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500656 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500657 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500658 goto setup_ntlmv2_rsp_ret;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500659 }
660 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000661
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200662 /* Must be within 5 minutes of the server (or in range +/-2h
663 * in case of Mac OS X), so simply carry over server timestamp
664 * (as Windows 7 does)
665 */
666 rsp_timestamp = find_timestamp(ses);
667
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500668 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500669 tilen = ses->auth_key.len;
670 tiblob = ses->auth_key.response;
671
672 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500673 if (!ses->auth_key.response) {
Anton Protopopov4b550af2016-02-10 12:50:21 -0500674 rc = -ENOMEM;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500675 ses->auth_key.len = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500676 goto setup_ntlmv2_rsp_ret;
677 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500678 ses->auth_key.len += baselen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500679
Tim Gardner2c957dd2013-11-07 16:40:57 -0700680 ntlmv2 = (struct ntlmv2_resp *)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500681 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
Tim Gardner2c957dd2013-11-07 16:40:57 -0700682 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
683 ntlmv2->reserved = 0;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200684 ntlmv2->time = rsp_timestamp;
685
Tim Gardner2c957dd2013-11-07 16:40:57 -0700686 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
687 ntlmv2->reserved2 = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500688
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500689 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500690
Rabin Vincentbd975d12016-07-19 09:26:21 +0200691 mutex_lock(&ses->server->srv_mutex);
692
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100693 rc = cifs_alloc_hash("hmac(md5)",
694 &ses->server->secmech.hmacmd5,
695 &ses->server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500696 if (rc) {
Rabin Vincentbd975d12016-07-19 09:26:21 +0200697 goto unlock;
Steve French95dc8dd2013-07-04 10:35:21 -0500698 }
699
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500700 /* calculate ntlmv2_hash */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500701 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500702 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500703 cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200704 goto unlock;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500705 }
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500706
707 /* calculate first part of the client response (CR1) */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500708 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500709 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500710 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200711 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500712 }
Steve Frenchb609f062007-07-09 07:55:14 +0000713
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500714 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500715 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500716 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500717 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500718 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
719 __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200720 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500721 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500722
723 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
724 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200726 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500727 }
728
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500729 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700730 ntlmv2->ntlmv2_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500731 CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500732 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500733 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200734 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500735 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500736
737 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
738 ses->auth_key.response);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500739 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500740 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500741
Rabin Vincentbd975d12016-07-19 09:26:21 +0200742unlock:
743 mutex_unlock(&ses->server->srv_mutex);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500744setup_ntlmv2_rsp_ret:
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500745 kfree(tiblob);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500746
747 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000748}
749
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500750int
Steve French96daf2b2011-05-27 04:34:02 +0000751calc_seckey(struct cifs_ses *ses)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500752{
753 int rc;
Herbert Xu9651ddb2016-01-24 21:17:17 +0800754 struct crypto_skcipher *tfm_arc4;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500755 struct scatterlist sgin, sgout;
Herbert Xu9651ddb2016-01-24 21:17:17 +0800756 struct skcipher_request *req;
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400757 unsigned char *sec_key;
758
759 sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
760 if (sec_key == NULL)
761 return -ENOMEM;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500762
763 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
764
Herbert Xu9651ddb2016-01-24 21:17:17 +0800765 tfm_arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
Shirish Pargaonkar7a8587e2011-01-29 13:54:58 -0600766 if (IS_ERR(tfm_arc4)) {
767 rc = PTR_ERR(tfm_arc4);
Joe Perchesf96637b2013-05-04 22:12:25 -0500768 cifs_dbg(VFS, "could not allocate crypto API arc4\n");
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400769 goto out;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500770 }
771
Herbert Xu9651ddb2016-01-24 21:17:17 +0800772 rc = crypto_skcipher_setkey(tfm_arc4, ses->auth_key.response,
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500773 CIFS_SESS_KEY_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500774 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500775 cifs_dbg(VFS, "%s: Could not set response as a key\n",
776 __func__);
Herbert Xu9651ddb2016-01-24 21:17:17 +0800777 goto out_free_cipher;
778 }
779
780 req = skcipher_request_alloc(tfm_arc4, GFP_KERNEL);
781 if (!req) {
782 rc = -ENOMEM;
783 cifs_dbg(VFS, "could not allocate crypto API arc4 request\n");
784 goto out_free_cipher;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500785 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500786
787 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500788 sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500789
Herbert Xu9651ddb2016-01-24 21:17:17 +0800790 skcipher_request_set_callback(req, 0, NULL, NULL);
791 skcipher_request_set_crypt(req, &sgin, &sgout, CIFS_CPHTXT_SIZE, NULL);
792
793 rc = crypto_skcipher_encrypt(req);
794 skcipher_request_free(req);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500795 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500796 cifs_dbg(VFS, "could not encrypt session key rc: %d\n", rc);
Herbert Xu9651ddb2016-01-24 21:17:17 +0800797 goto out_free_cipher;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500798 }
799
800 /* make secondary_key/nonce as session key */
801 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
802 /* and make len as that of session key only */
803 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
804
Herbert Xu9651ddb2016-01-24 21:17:17 +0800805out_free_cipher:
806 crypto_free_skcipher(tfm_arc4);
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400807out:
808 kfree(sec_key);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500809 return rc;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500810}
811
812void
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700813cifs_crypto_secmech_release(struct TCP_Server_Info *server)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500814{
Steve French95dc8dd2013-07-04 10:35:21 -0500815 if (server->secmech.cmacaes) {
Steve French429b46f2013-06-26 23:45:05 -0500816 crypto_free_shash(server->secmech.cmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500817 server->secmech.cmacaes = NULL;
818 }
Steve French429b46f2013-06-26 23:45:05 -0500819
Steve French95dc8dd2013-07-04 10:35:21 -0500820 if (server->secmech.hmacsha256) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700821 crypto_free_shash(server->secmech.hmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500822 server->secmech.hmacsha256 = NULL;
823 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700824
Steve French95dc8dd2013-07-04 10:35:21 -0500825 if (server->secmech.md5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500826 crypto_free_shash(server->secmech.md5);
Steve French95dc8dd2013-07-04 10:35:21 -0500827 server->secmech.md5 = NULL;
828 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500829
Gustavo A. R. Silva70e80652018-02-19 11:11:13 -0600830 if (server->secmech.sha512) {
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100831 crypto_free_shash(server->secmech.sha512);
832 server->secmech.sha512 = NULL;
833 }
834
Steve French95dc8dd2013-07-04 10:35:21 -0500835 if (server->secmech.hmacmd5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500836 crypto_free_shash(server->secmech.hmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500837 server->secmech.hmacmd5 = NULL;
838 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500839
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700840 if (server->secmech.ccmaesencrypt) {
841 crypto_free_aead(server->secmech.ccmaesencrypt);
842 server->secmech.ccmaesencrypt = NULL;
843 }
844
845 if (server->secmech.ccmaesdecrypt) {
846 crypto_free_aead(server->secmech.ccmaesdecrypt);
847 server->secmech.ccmaesdecrypt = NULL;
848 }
849
Steve French429b46f2013-06-26 23:45:05 -0500850 kfree(server->secmech.sdesccmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500851 server->secmech.sdesccmacaes = NULL;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700852 kfree(server->secmech.sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500853 server->secmech.sdeschmacsha256 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500854 kfree(server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500855 server->secmech.sdeschmacmd5 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500856 kfree(server->secmech.sdescmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500857 server->secmech.sdescmd5 = NULL;
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100858 kfree(server->secmech.sdescsha512);
859 server->secmech.sdescsha512 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500860}