blob: d2a05e46d6f52cee5026ea42227291a7cb124597 [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,
40 struct TCP_Server_Info *server, char *signature,
41 struct shash_desc *shash)
42{
43 int i;
44 int rc;
45 struct kvec *iov = rqst->rq_iov;
46 int n_vec = rqst->rq_nvec;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100047 int is_smb2 = server->vals->header_preamble_size == 0;
Al Viro16c568e2015-11-12 22:46:49 -050048
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100049 /* iov[0] is actual data and not the rfc1002 length for SMB2+ */
50 if (is_smb2) {
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030051 if (iov[0].iov_len <= 4)
52 return -EIO;
53 i = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100054 } else {
55 if (n_vec < 2 || iov[0].iov_len != 4)
56 return -EIO;
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030057 i = 1; /* skip rfc1002 length */
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100058 }
59
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030060 for (; i < n_vec; i++) {
Al Viro16c568e2015-11-12 22:46:49 -050061 if (iov[i].iov_len == 0)
62 continue;
63 if (iov[i].iov_base == NULL) {
64 cifs_dbg(VFS, "null iovec entry\n");
65 return -EIO;
66 }
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030067
Pavel Shilovsky738f9de2016-11-23 15:14:57 -080068 rc = crypto_shash_update(shash,
69 iov[i].iov_base, iov[i].iov_len);
Al Viro16c568e2015-11-12 22:46:49 -050070 if (rc) {
71 cifs_dbg(VFS, "%s: Could not update with payload\n",
72 __func__);
73 return rc;
74 }
75 }
76
77 /* now hash over the rq_pages array */
78 for (i = 0; i < rqst->rq_npages; i++) {
Long Li4c0d2a52018-05-30 12:48:03 -070079 void *kaddr;
80 unsigned int len, offset;
Al Viro16c568e2015-11-12 22:46:49 -050081
Long Li4c0d2a52018-05-30 12:48:03 -070082 rqst_page_get_length(rqst, i, &len, &offset);
83
84 kaddr = (char *) kmap(rqst->rq_pages[i]) + offset;
Al Viro16c568e2015-11-12 22:46:49 -050085
Paulo Alcantaraa12d0c52018-06-23 14:52:25 -030086 rc = crypto_shash_update(shash, kaddr, len);
87 if (rc) {
88 cifs_dbg(VFS, "%s: Could not update with payload\n",
89 __func__);
90 kunmap(rqst->rq_pages[i]);
91 return rc;
92 }
Al Viro16c568e2015-11-12 22:46:49 -050093
94 kunmap(rqst->rq_pages[i]);
95 }
96
97 rc = crypto_shash_final(shash, signature);
98 if (rc)
99 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
100
101 return rc;
102}
103
Jeff Layton157c2492011-04-02 07:34:30 -0400104/*
105 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
106 * The 16 byte signature must be allocated by the caller. Note we only use the
107 * 1st eight bytes and that the smb header signature field on input contains
108 * the sequence number before this function is called. Also, this function
109 * should be called with the server->srv_mutex held.
110 */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700111static int cifs_calc_signature(struct smb_rqst *rqst,
Jeff Layton826a95e2011-10-11 06:41:32 -0400112 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800113{
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500114 int rc;
Steve French84afc292005-12-02 13:32:45 -0800115
Al Viro16c568e2015-11-12 22:46:49 -0500116 if (!rqst->rq_iov || !signature || !server)
Steve Frenche9917a02006-03-31 21:22:00 +0000117 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800118
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100119 rc = cifs_alloc_hash("md5", &server->secmech.md5,
120 &server->secmech.sdescmd5);
121 if (rc)
122 return -1;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500123
124 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
125 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500126 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500127 return rc;
128 }
129
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500130 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500131 server->session_key.response, server->session_key.len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500132 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500133 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500134 return rc;
135 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500136
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000137 return __cifs_calc_signature(rqst, server, signature,
Al Viro16c568e2015-11-12 22:46:49 -0500138 &server->secmech.sdescmd5->shash);
Steve French84afc292005-12-02 13:32:45 -0800139}
140
Jeff Laytona0f8b4f2011-01-07 11:30:28 -0500141/* must be called with server->srv_mutex held */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700142int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000143 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800144{
145 int rc = 0;
146 char smb_signature[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700147 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800148
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800149 if (rqst->rq_iov[0].iov_len != 4 ||
150 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
151 return -EIO;
152
Steve Frenchffdd6e42007-06-24 21:15:44 +0000153 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800154 return -EINVAL;
155
Jeff Layton998d6fc2011-07-26 12:21:17 -0400156 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
157 server->tcpStatus == CifsNeedNegotiate)
Steve French84afc292005-12-02 13:32:45 -0800158 return rc;
159
Jeff Layton998d6fc2011-07-26 12:21:17 -0400160 if (!server->session_estab) {
Jeff Laytonb4dacbc2011-10-11 06:41:32 -0400161 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
Jeff Layton998d6fc2011-07-26 12:21:17 -0400162 return rc;
163 }
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
Jeff Layton0124cc42013-04-03 11:55:03 -0400169 *pexpected_response_sequence_number = ++server->sequence_number;
170 ++server->sequence_number;
Steve French84afc292005-12-02 13:32:45 -0800171
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700172 rc = cifs_calc_signature(rqst, 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
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700181int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
182 __u32 *pexpected_response_sequence)
183{
184 struct smb_rqst rqst = { .rq_iov = iov,
185 .rq_nvec = n_vec };
186
187 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
188}
189
Jeff Layton826a95e2011-10-11 06:41:32 -0400190/* must be called with server->srv_mutex held */
191int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
192 __u32 *pexpected_response_sequence_number)
193{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800194 struct kvec iov[2];
Jeff Layton826a95e2011-10-11 06:41:32 -0400195
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800196 iov[0].iov_base = cifs_pdu;
197 iov[0].iov_len = 4;
198 iov[1].iov_base = (char *)cifs_pdu + 4;
199 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
Jeff Layton826a95e2011-10-11 06:41:32 -0400200
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800201 return cifs_sign_smbv(iov, 2, server,
Jeff Layton826a95e2011-10-11 06:41:32 -0400202 pexpected_response_sequence_number);
203}
204
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700205int cifs_verify_signature(struct smb_rqst *rqst,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500206 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000207 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000209 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 char server_response_sig[8];
211 char what_we_think_sig_should_be[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700212 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800214 if (rqst->rq_iov[0].iov_len != 4 ||
215 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
216 return -EIO;
217
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500218 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
220
Jeff Layton9c4843e2011-06-06 15:40:23 -0400221 if (!server->session_estab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223
224 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000225 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000226 (struct smb_com_lock_req *)cifs_pdu;
Colin Ian King58902552018-11-03 15:59:44 +0000227 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229 }
230
Steve French50c2f752007-07-13 00:33:32 +0000231 /* BB what if signatures are supposed to be on for session but
232 server does not send one? BB */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000235 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500236 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
237 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* save off the origiginal signature so we can modify the smb and check
240 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000241 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Steve French50c2f752007-07-13 00:33:32 +0000243 cifs_pdu->Signature.Sequence.SequenceNumber =
244 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 cifs_pdu->Signature.Sequence.Reserved = 0;
246
Jeff Layton157c2492011-04-02 07:34:30 -0400247 mutex_lock(&server->srv_mutex);
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700248 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
Jeff Layton157c2492011-04-02 07:34:30 -0400249 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Steve French50c2f752007-07-13 00:33:32 +0000251 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return rc;
253
Steve French50c2f752007-07-13 00:33:32 +0000254/* cifs_dump_mem("what we think it should be: ",
255 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Steve French50c2f752007-07-13 00:33:32 +0000257 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EACCES;
259 else
260 return 0;
261
262}
263
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500264/* first calculate 24 bytes ntlm response and then 16 byte session key */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500265int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600267 int rc = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500268 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
269 char temp_key[CIFS_SESS_KEY_SIZE];
270
271 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EINVAL;
273
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500274 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -0500275 if (!ses->auth_key.response)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500276 return -ENOMEM;
Joe Perchesf96637b2013-05-04 22:12:25 -0500277
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500278 ses->auth_key.len = temp_len;
279
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600280 rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500281 ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600282 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500283 cifs_dbg(FYI, "%s Can't generate NTLM response, error: %d\n",
284 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600285 return rc;
286 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500287
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500288 rc = E_md4hash(ses->password, temp_key, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600289 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500290 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
291 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600292 return rc;
293 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500294
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600295 rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
296 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500297 cifs_dbg(FYI, "%s Can't generate NTLM session key, error: %d\n",
298 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600299
300 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Steve French7c7b25b2006-06-01 19:20:10 +0000303#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French43988d72011-04-19 18:23:31 +0000304int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500305 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000306{
Ronnie Sahlberg52baa512018-12-12 11:50:00 +1000307 int i, len;
Steve French43988d72011-04-19 18:23:31 +0000308 int rc;
Aurelien Aptel97f4b722018-01-25 15:59:39 +0100309 char password_with_pad[CIFS_ENCPWD_SIZE] = {0};
Steve French7c7b25b2006-06-01 19:20:10 +0000310
Ronnie Sahlberg52baa512018-12-12 11:50:00 +1000311 if (password) {
312 for (len = 0; len < CIFS_ENCPWD_SIZE; len++)
313 if (!password[len])
314 break;
315
316 memcpy(password_with_pad, password, len);
317 }
Steve French7c7b25b2006-06-01 19:20:10 +0000318
Jeff Layton04912d62010-04-24 07:57:45 -0400319 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500320 memcpy(lnm_session_key, password_with_pad,
321 CIFS_ENCPWD_SIZE);
Steve French43988d72011-04-19 18:23:31 +0000322 return 0;
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500323 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000324
Steve French7c7b25b2006-06-01 19:20:10 +0000325 /* calculate old style session key */
326 /* calling toupper is less broken than repeatedly
327 calling nls_toupper would be since that will never
328 work for UTF8, but neither handles multibyte code pages
329 but the only alternative would be converting to UCS-16 (Unicode)
330 (using a routine something like UniStrupr) then
331 uppercasing and then converting back from Unicode - which
332 would only worth doing it if we knew it were utf8. Basically
333 utf8 and other multibyte codepages each need their own strupper
334 function since a byte at a time will ont work. */
335
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000336 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000337 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000338
Steve French43988d72011-04-19 18:23:31 +0000339 rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500340
Steve French43988d72011-04-19 18:23:31 +0000341 return rc;
Steve French7c7b25b2006-06-01 19:20:10 +0000342}
343#endif /* CIFS_WEAK_PW_HASH */
344
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500345/* Build a proper attribute value/target info pairs blob.
346 * Fill in netbios and dns domain name and workstation name
347 * and client time (total five av pairs and + one end of fields indicator.
348 * Allocate domain name which gets freed when session struct is deallocated.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500349 */
350static int
Steve French96daf2b2011-05-27 04:34:02 +0000351build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500352{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500353 unsigned int dlen;
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500354 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500355 char *defdmname = "WORKGROUP";
356 unsigned char *blobptr;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500357 struct ntlmssp2_name *attrptr;
358
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500359 if (!ses->domainName) {
360 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
361 if (!ses->domainName)
362 return -ENOMEM;
363 }
364
365 dlen = strlen(ses->domainName);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500366
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500367 /*
368 * The length of this blob is two times the size of a
369 * structure (av pair) which holds name/size
370 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
371 * unicode length of a netbios domain name
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500372 */
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500373 ses->auth_key.len = size + 2 * dlen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500374 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
375 if (!ses->auth_key.response) {
376 ses->auth_key.len = 0;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500377 return -ENOMEM;
378 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500379
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500380 blobptr = ses->auth_key.response;
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500381 attrptr = (struct ntlmssp2_name *) blobptr;
382
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500383 /*
384 * As defined in MS-NTLM 3.3.2, just this av pair field
385 * is sufficient as part of the temp
386 */
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500387 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
388 attrptr->length = cpu_to_le16(2 * dlen);
389 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
Steve Frenchacbbb762012-01-18 22:32:33 -0600390 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500391
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500392 return 0;
393}
394
395/* Server has provided av pairs/target info in the type 2 challenge
396 * packet and we have plucked it and stored within smb session.
397 * We parse that blob here to find netbios domain name to be used
398 * as part of ntlmv2 authentication (in Target String), if not already
399 * specified on the command line.
400 * If this function returns without any error but without fetching
401 * domain name, authentication may fail against some server but
402 * may not fail against other (those who are not very particular
403 * about target string i.e. for some, just user name might suffice.
404 */
405static int
Steve French96daf2b2011-05-27 04:34:02 +0000406find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500407{
408 unsigned int attrsize;
409 unsigned int type;
410 unsigned int onesize = sizeof(struct ntlmssp2_name);
411 unsigned char *blobptr;
412 unsigned char *blobend;
413 struct ntlmssp2_name *attrptr;
414
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500415 if (!ses->auth_key.len || !ses->auth_key.response)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500416 return 0;
417
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500418 blobptr = ses->auth_key.response;
419 blobend = blobptr + ses->auth_key.len;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500420
421 while (blobptr + onesize < blobend) {
422 attrptr = (struct ntlmssp2_name *) blobptr;
423 type = le16_to_cpu(attrptr->type);
424 if (type == NTLMSSP_AV_EOL)
425 break;
426 blobptr += 2; /* advance attr type */
427 attrsize = le16_to_cpu(attrptr->length);
428 blobptr += 2; /* advance attr size */
429 if (blobptr + attrsize > blobend)
430 break;
431 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
Chen Gang057d6332013-07-19 09:01:36 +0800432 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500433 break;
434 if (!ses->domainName) {
435 ses->domainName =
436 kmalloc(attrsize + 1, GFP_KERNEL);
437 if (!ses->domainName)
438 return -ENOMEM;
Steve Frenchacbbb762012-01-18 22:32:33 -0600439 cifs_from_utf16(ses->domainName,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500440 (__le16 *)blobptr, attrsize, attrsize,
Steve Frenchb6938552014-09-25 13:20:05 -0500441 nls_cp, NO_MAP_UNI_RSVD);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500442 break;
443 }
444 }
445 blobptr += attrsize; /* advance attr value */
446 }
447
448 return 0;
449}
450
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200451/* Server has provided av pairs/target info in the type 2 challenge
452 * packet and we have plucked it and stored within smb session.
453 * We parse that blob here to find the server given timestamp
454 * as part of ntlmv2 authentication (or local current time as
455 * default in case of failure)
456 */
457static __le64
458find_timestamp(struct cifs_ses *ses)
459{
460 unsigned int attrsize;
461 unsigned int type;
462 unsigned int onesize = sizeof(struct ntlmssp2_name);
463 unsigned char *blobptr;
464 unsigned char *blobend;
465 struct ntlmssp2_name *attrptr;
Arnd Bergmann95390202018-06-19 17:27:58 +0200466 struct timespec64 ts;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200467
468 if (!ses->auth_key.len || !ses->auth_key.response)
469 return 0;
470
471 blobptr = ses->auth_key.response;
472 blobend = blobptr + ses->auth_key.len;
473
474 while (blobptr + onesize < blobend) {
475 attrptr = (struct ntlmssp2_name *) blobptr;
476 type = le16_to_cpu(attrptr->type);
477 if (type == NTLMSSP_AV_EOL)
478 break;
479 blobptr += 2; /* advance attr type */
480 attrsize = le16_to_cpu(attrptr->length);
481 blobptr += 2; /* advance attr size */
482 if (blobptr + attrsize > blobend)
483 break;
484 if (type == NTLMSSP_AV_TIMESTAMP) {
485 if (attrsize == sizeof(u64))
486 return *((__le64 *)blobptr);
487 }
488 blobptr += attrsize; /* advance attr value */
489 }
490
Arnd Bergmann95390202018-06-19 17:27:58 +0200491 ktime_get_real_ts64(&ts);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700492 return cpu_to_le64(cifs_UnixTimeToNT(ts));
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200493}
494
Steve French96daf2b2011-05-27 04:34:02 +0000495static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
Steve French50c2f752007-07-13 00:33:32 +0000496 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000497{
498 int rc = 0;
499 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500500 char nt_hash[CIFS_NTHASH_SIZE];
Steve Frenchfdf96a92013-06-25 14:03:16 -0500501 __le16 *user;
Steve French50c2f752007-07-13 00:33:32 +0000502 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500503 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000504
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500505 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500506 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500507 return -1;
508 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000509
510 /* calculate md4 hash of password */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500511 E_md4hash(ses->password, nt_hash, nls_cp);
Steve Frencha8ee0342006-06-05 23:34:19 +0000512
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500513 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500514 CIFS_NTHASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500515 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500516 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500517 return rc;
518 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500519
520 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
521 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500522 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500523 return rc;
524 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000525
Steve Frenchfdf96a92013-06-25 14:03:16 -0500526 /* convert ses->user_name to unicode */
Jeff Layton04febab2012-01-17 16:09:15 -0500527 len = ses->user_name ? strlen(ses->user_name) : 0;
Steve French1717ffc2006-06-08 05:41:32 +0000528 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500529 if (user == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500530 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500531 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500532 }
Jeff Layton04febab2012-01-17 16:09:15 -0500533
534 if (len) {
Steve Frenchfdf96a92013-06-25 14:03:16 -0500535 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
Jeff Layton04febab2012-01-17 16:09:15 -0500536 UniStrupr(user);
537 } else {
538 memset(user, '\0', 2);
539 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500540
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500541 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500542 (char *)user, 2 * len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500543 kfree(user);
544 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500545 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500546 return rc;
547 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000548
549 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000550 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000551 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000552
Steve French50c2f752007-07-13 00:33:32 +0000553 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500554 if (domain == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500555 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500556 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500557 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600558 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
559 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500560 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500561 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
562 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000563 kfree(domain);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500564 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500565 cifs_dbg(VFS, "%s: Could not update with domain\n",
566 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500567 return rc;
568 }
Steve French8b7a4542015-03-30 16:58:17 -0500569 } else {
570 /* We use ses->serverName if no domain name available */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500571 len = strlen(ses->serverName);
572
573 server = kmalloc(2 + (len * 2), GFP_KERNEL);
574 if (server == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500575 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500576 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500577 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600578 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500579 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500580 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500581 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
582 (char *)server, 2 * len);
583 kfree(server);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500584 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500585 cifs_dbg(VFS, "%s: Could not update with server\n",
586 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500587 return rc;
588 }
Steve French1717ffc2006-06-08 05:41:32 +0000589 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500590
591 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500592 ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500593 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500594 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500595
Steve Frencha8ee0342006-06-05 23:34:19 +0000596 return rc;
597}
598
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500599static int
Steve French96daf2b2011-05-27 04:34:02 +0000600CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500601{
602 int rc;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700603 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
604 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
605 unsigned int hash_len;
606
607 /* The MD5 hash starts at challenge_key.key */
608 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
609 offsetof(struct ntlmv2_resp, challenge.key[0]));
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500610
611 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500612 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500613 return -1;
614 }
615
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500616 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700617 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500618 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500619 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
620 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500621 return rc;
622 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500623
624 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
625 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500626 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500627 return rc;
628 }
629
Jeff Layton3f618222013-06-12 19:52:14 -0500630 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
Tim Gardner2c957dd2013-11-07 16:40:57 -0700631 memcpy(ntlmv2->challenge.key,
632 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500633 else
Tim Gardner2c957dd2013-11-07 16:40:57 -0700634 memcpy(ntlmv2->challenge.key,
635 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500636 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700637 ntlmv2->challenge.key, hash_len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500638 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500639 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500640 return rc;
641 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500642
Tim Gardner2c957dd2013-11-07 16:40:57 -0700643 /* Note that the MD5 digest over writes anon.challenge_key.key */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500644 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700645 ntlmv2->ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500646 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500647 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500648
649 return rc;
650}
651
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500652int
Steve French96daf2b2011-05-27 04:34:02 +0000653setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000654{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000655 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500656 int baselen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500657 unsigned int tilen;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700658 struct ntlmv2_resp *ntlmv2;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500659 char ntlmv2_hash[16];
660 unsigned char *tiblob = NULL; /* target info blob */
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200661 __le64 rsp_timestamp;
Steve French6d027cf2006-06-05 16:26:05 +0000662
Jeff Layton3f618222013-06-12 19:52:14 -0500663 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500664 if (!ses->domainName) {
Germano Percossi39566442016-12-15 12:31:18 +0530665 if (ses->domainAuto) {
666 rc = find_domain_name(ses, nls_cp);
667 if (rc) {
668 cifs_dbg(VFS, "error %d finding domain name\n",
669 rc);
670 goto setup_ntlmv2_rsp_ret;
671 }
672 } else {
673 ses->domainName = kstrdup("", GFP_KERNEL);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500674 }
675 }
676 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500677 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500678 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500679 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500680 goto setup_ntlmv2_rsp_ret;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500681 }
682 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000683
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200684 /* Must be within 5 minutes of the server (or in range +/-2h
685 * in case of Mac OS X), so simply carry over server timestamp
686 * (as Windows 7 does)
687 */
688 rsp_timestamp = find_timestamp(ses);
689
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500690 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500691 tilen = ses->auth_key.len;
692 tiblob = ses->auth_key.response;
693
694 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500695 if (!ses->auth_key.response) {
Anton Protopopov4b550af2016-02-10 12:50:21 -0500696 rc = -ENOMEM;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500697 ses->auth_key.len = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500698 goto setup_ntlmv2_rsp_ret;
699 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500700 ses->auth_key.len += baselen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500701
Tim Gardner2c957dd2013-11-07 16:40:57 -0700702 ntlmv2 = (struct ntlmv2_resp *)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500703 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
Tim Gardner2c957dd2013-11-07 16:40:57 -0700704 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
705 ntlmv2->reserved = 0;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200706 ntlmv2->time = rsp_timestamp;
707
Tim Gardner2c957dd2013-11-07 16:40:57 -0700708 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
709 ntlmv2->reserved2 = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500710
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500711 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500712
Rabin Vincentbd975d12016-07-19 09:26:21 +0200713 mutex_lock(&ses->server->srv_mutex);
714
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100715 rc = cifs_alloc_hash("hmac(md5)",
716 &ses->server->secmech.hmacmd5,
717 &ses->server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500718 if (rc) {
Rabin Vincentbd975d12016-07-19 09:26:21 +0200719 goto unlock;
Steve French95dc8dd2013-07-04 10:35:21 -0500720 }
721
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500722 /* calculate ntlmv2_hash */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500723 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500724 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200726 goto unlock;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500727 }
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500728
729 /* calculate first part of the client response (CR1) */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500730 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500731 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500732 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200733 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500734 }
Steve Frenchb609f062007-07-09 07:55:14 +0000735
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500736 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500737 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500738 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500739 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500740 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
741 __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200742 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500743 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500744
745 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
746 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500747 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200748 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500749 }
750
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500751 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700752 ntlmv2->ntlmv2_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500753 CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500754 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500755 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200756 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500757 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500758
759 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
760 ses->auth_key.response);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500761 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500762 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500763
Rabin Vincentbd975d12016-07-19 09:26:21 +0200764unlock:
765 mutex_unlock(&ses->server->srv_mutex);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500766setup_ntlmv2_rsp_ret:
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500767 kfree(tiblob);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500768
769 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000770}
771
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500772int
Steve French96daf2b2011-05-27 04:34:02 +0000773calc_seckey(struct cifs_ses *ses)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500774{
775 int rc;
Herbert Xu9651ddb2016-01-24 21:17:17 +0800776 struct crypto_skcipher *tfm_arc4;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500777 struct scatterlist sgin, sgout;
Herbert Xu9651ddb2016-01-24 21:17:17 +0800778 struct skcipher_request *req;
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400779 unsigned char *sec_key;
780
781 sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
782 if (sec_key == NULL)
783 return -ENOMEM;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500784
785 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
786
Herbert Xu9651ddb2016-01-24 21:17:17 +0800787 tfm_arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
Shirish Pargaonkar7a8587e2011-01-29 13:54:58 -0600788 if (IS_ERR(tfm_arc4)) {
789 rc = PTR_ERR(tfm_arc4);
Joe Perchesf96637b2013-05-04 22:12:25 -0500790 cifs_dbg(VFS, "could not allocate crypto API arc4\n");
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400791 goto out;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500792 }
793
Herbert Xu9651ddb2016-01-24 21:17:17 +0800794 rc = crypto_skcipher_setkey(tfm_arc4, ses->auth_key.response,
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500795 CIFS_SESS_KEY_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500796 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500797 cifs_dbg(VFS, "%s: Could not set response as a key\n",
798 __func__);
Herbert Xu9651ddb2016-01-24 21:17:17 +0800799 goto out_free_cipher;
800 }
801
802 req = skcipher_request_alloc(tfm_arc4, GFP_KERNEL);
803 if (!req) {
804 rc = -ENOMEM;
805 cifs_dbg(VFS, "could not allocate crypto API arc4 request\n");
806 goto out_free_cipher;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500807 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500808
809 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500810 sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500811
Herbert Xu9651ddb2016-01-24 21:17:17 +0800812 skcipher_request_set_callback(req, 0, NULL, NULL);
813 skcipher_request_set_crypt(req, &sgin, &sgout, CIFS_CPHTXT_SIZE, NULL);
814
815 rc = crypto_skcipher_encrypt(req);
816 skcipher_request_free(req);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500817 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500818 cifs_dbg(VFS, "could not encrypt session key rc: %d\n", rc);
Herbert Xu9651ddb2016-01-24 21:17:17 +0800819 goto out_free_cipher;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500820 }
821
822 /* make secondary_key/nonce as session key */
823 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
824 /* and make len as that of session key only */
825 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
826
Herbert Xu9651ddb2016-01-24 21:17:17 +0800827out_free_cipher:
828 crypto_free_skcipher(tfm_arc4);
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400829out:
830 kfree(sec_key);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500831 return rc;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500832}
833
834void
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700835cifs_crypto_secmech_release(struct TCP_Server_Info *server)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500836{
Steve French95dc8dd2013-07-04 10:35:21 -0500837 if (server->secmech.cmacaes) {
Steve French429b46f2013-06-26 23:45:05 -0500838 crypto_free_shash(server->secmech.cmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500839 server->secmech.cmacaes = NULL;
840 }
Steve French429b46f2013-06-26 23:45:05 -0500841
Steve French95dc8dd2013-07-04 10:35:21 -0500842 if (server->secmech.hmacsha256) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700843 crypto_free_shash(server->secmech.hmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500844 server->secmech.hmacsha256 = NULL;
845 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700846
Steve French95dc8dd2013-07-04 10:35:21 -0500847 if (server->secmech.md5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500848 crypto_free_shash(server->secmech.md5);
Steve French95dc8dd2013-07-04 10:35:21 -0500849 server->secmech.md5 = NULL;
850 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500851
Gustavo A. R. Silva70e80652018-02-19 11:11:13 -0600852 if (server->secmech.sha512) {
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100853 crypto_free_shash(server->secmech.sha512);
854 server->secmech.sha512 = NULL;
855 }
856
Steve French95dc8dd2013-07-04 10:35:21 -0500857 if (server->secmech.hmacmd5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500858 crypto_free_shash(server->secmech.hmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500859 server->secmech.hmacmd5 = NULL;
860 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500861
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700862 if (server->secmech.ccmaesencrypt) {
863 crypto_free_aead(server->secmech.ccmaesencrypt);
864 server->secmech.ccmaesencrypt = NULL;
865 }
866
867 if (server->secmech.ccmaesdecrypt) {
868 crypto_free_aead(server->secmech.ccmaesdecrypt);
869 server->secmech.ccmaesdecrypt = NULL;
870 }
871
Steve French429b46f2013-06-26 23:45:05 -0500872 kfree(server->secmech.sdesccmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500873 server->secmech.sdesccmacaes = NULL;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700874 kfree(server->secmech.sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500875 server->secmech.sdeschmacsha256 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500876 kfree(server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500877 server->secmech.sdeschmacmd5 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500878 kfree(server->secmech.sdescmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500879 server->secmech.sdescmd5 = NULL;
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100880 kfree(server->secmech.sdescsha512);
881 server->secmech.sdescsha512 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500882}