blob: 97b7497c13ef52217f7c7de71dd6d298a6538393 [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>
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +020036#include <linux/fips.h>
37#include <crypto/arc4.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070038#include <crypto/aead.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Al Viro16c568e2015-11-12 22:46:49 -050040int __cifs_calc_signature(struct smb_rqst *rqst,
41 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;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100048 int is_smb2 = server->vals->header_preamble_size == 0;
Al Viro16c568e2015-11-12 22:46:49 -050049
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100050 /* iov[0] is actual data and not the rfc1002 length for SMB2+ */
51 if (is_smb2) {
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030052 if (iov[0].iov_len <= 4)
53 return -EIO;
54 i = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100055 } else {
56 if (n_vec < 2 || iov[0].iov_len != 4)
57 return -EIO;
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030058 i = 1; /* skip rfc1002 length */
Ronnie Sahlbergc713c872018-06-12 08:00:58 +100059 }
60
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030061 for (; i < n_vec; i++) {
Al Viro16c568e2015-11-12 22:46:49 -050062 if (iov[i].iov_len == 0)
63 continue;
64 if (iov[i].iov_base == NULL) {
65 cifs_dbg(VFS, "null iovec entry\n");
66 return -EIO;
67 }
Paulo Alcantara83ffdea2018-06-15 15:58:00 -030068
Pavel Shilovsky738f9de2016-11-23 15:14:57 -080069 rc = crypto_shash_update(shash,
70 iov[i].iov_base, iov[i].iov_len);
Al Viro16c568e2015-11-12 22:46:49 -050071 if (rc) {
72 cifs_dbg(VFS, "%s: Could not update with payload\n",
73 __func__);
74 return rc;
75 }
76 }
77
78 /* now hash over the rq_pages array */
79 for (i = 0; i < rqst->rq_npages; i++) {
Long Li4c0d2a52018-05-30 12:48:03 -070080 void *kaddr;
81 unsigned int len, offset;
Al Viro16c568e2015-11-12 22:46:49 -050082
Long Li4c0d2a52018-05-30 12:48:03 -070083 rqst_page_get_length(rqst, i, &len, &offset);
84
85 kaddr = (char *) kmap(rqst->rq_pages[i]) + offset;
Al Viro16c568e2015-11-12 22:46:49 -050086
Paulo Alcantaraa12d0c52018-06-23 14:52:25 -030087 rc = crypto_shash_update(shash, kaddr, len);
88 if (rc) {
89 cifs_dbg(VFS, "%s: Could not update with payload\n",
90 __func__);
91 kunmap(rqst->rq_pages[i]);
92 return rc;
93 }
Al Viro16c568e2015-11-12 22:46:49 -050094
95 kunmap(rqst->rq_pages[i]);
96 }
97
98 rc = crypto_shash_final(shash, signature);
99 if (rc)
100 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
101
102 return rc;
103}
104
Jeff Layton157c2492011-04-02 07:34:30 -0400105/*
106 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
107 * The 16 byte signature must be allocated by the caller. Note we only use the
108 * 1st eight bytes and that the smb header signature field on input contains
109 * the sequence number before this function is called. Also, this function
110 * should be called with the server->srv_mutex held.
111 */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700112static int cifs_calc_signature(struct smb_rqst *rqst,
Jeff Layton826a95e2011-10-11 06:41:32 -0400113 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800114{
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500115 int rc;
Steve French84afc292005-12-02 13:32:45 -0800116
Al Viro16c568e2015-11-12 22:46:49 -0500117 if (!rqst->rq_iov || !signature || !server)
Steve Frenche9917a02006-03-31 21:22:00 +0000118 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800119
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100120 rc = cifs_alloc_hash("md5", &server->secmech.md5,
121 &server->secmech.sdescmd5);
122 if (rc)
123 return -1;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500124
125 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
126 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500127 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500128 return rc;
129 }
130
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500131 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500132 server->session_key.response, server->session_key.len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500133 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500134 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500135 return rc;
136 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500137
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000138 return __cifs_calc_signature(rqst, server, signature,
Al Viro16c568e2015-11-12 22:46:49 -0500139 &server->secmech.sdescmd5->shash);
Steve French84afc292005-12-02 13:32:45 -0800140}
141
Jeff Laytona0f8b4f2011-01-07 11:30:28 -0500142/* must be called with server->srv_mutex held */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700143int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000144 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800145{
146 int rc = 0;
147 char smb_signature[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700148 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800149
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800150 if (rqst->rq_iov[0].iov_len != 4 ||
151 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
152 return -EIO;
153
Steve Frenchffdd6e42007-06-24 21:15:44 +0000154 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800155 return -EINVAL;
156
Jeff Layton998d6fc2011-07-26 12:21:17 -0400157 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
158 server->tcpStatus == CifsNeedNegotiate)
Steve French84afc292005-12-02 13:32:45 -0800159 return rc;
160
Jeff Layton998d6fc2011-07-26 12:21:17 -0400161 if (!server->session_estab) {
Jeff Laytonb4dacbc2011-10-11 06:41:32 -0400162 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
Jeff Layton998d6fc2011-07-26 12:21:17 -0400163 return rc;
164 }
165
Steve Frenchffdd6e42007-06-24 21:15:44 +0000166 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800167 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000168 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800169
Jeff Layton0124cc42013-04-03 11:55:03 -0400170 *pexpected_response_sequence_number = ++server->sequence_number;
171 ++server->sequence_number;
Steve French84afc292005-12-02 13:32:45 -0800172
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700173 rc = cifs_calc_signature(rqst, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000174 if (rc)
175 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
176 else
177 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800178
Steve Frenchffdd6e42007-06-24 21:15:44 +0000179 return rc;
Steve French84afc292005-12-02 13:32:45 -0800180}
181
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700182int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
183 __u32 *pexpected_response_sequence)
184{
185 struct smb_rqst rqst = { .rq_iov = iov,
186 .rq_nvec = n_vec };
187
188 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
189}
190
Jeff Layton826a95e2011-10-11 06:41:32 -0400191/* must be called with server->srv_mutex held */
192int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
193 __u32 *pexpected_response_sequence_number)
194{
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800195 struct kvec iov[2];
Jeff Layton826a95e2011-10-11 06:41:32 -0400196
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800197 iov[0].iov_base = cifs_pdu;
198 iov[0].iov_len = 4;
199 iov[1].iov_base = (char *)cifs_pdu + 4;
200 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
Jeff Layton826a95e2011-10-11 06:41:32 -0400201
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800202 return cifs_sign_smbv(iov, 2, server,
Jeff Layton826a95e2011-10-11 06:41:32 -0400203 pexpected_response_sequence_number);
204}
205
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700206int cifs_verify_signature(struct smb_rqst *rqst,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500207 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000208 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000210 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 char server_response_sig[8];
212 char what_we_think_sig_should_be[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700213 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800215 if (rqst->rq_iov[0].iov_len != 4 ||
216 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
217 return -EIO;
218
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500219 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -EINVAL;
221
Jeff Layton9c4843e2011-06-06 15:40:23 -0400222 if (!server->session_estab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224
225 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000226 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000227 (struct smb_com_lock_req *)cifs_pdu;
Colin Ian King58902552018-11-03 15:59:44 +0000228 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230 }
231
Steve French50c2f752007-07-13 00:33:32 +0000232 /* BB what if signatures are supposed to be on for session but
233 server does not send one? BB */
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000236 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500237 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
238 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* save off the origiginal signature so we can modify the smb and check
241 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000242 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Steve French50c2f752007-07-13 00:33:32 +0000244 cifs_pdu->Signature.Sequence.SequenceNumber =
245 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 cifs_pdu->Signature.Sequence.Reserved = 0;
247
Jeff Layton157c2492011-04-02 07:34:30 -0400248 mutex_lock(&server->srv_mutex);
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700249 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
Jeff Layton157c2492011-04-02 07:34:30 -0400250 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Steve French50c2f752007-07-13 00:33:32 +0000252 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return rc;
254
Steve French50c2f752007-07-13 00:33:32 +0000255/* cifs_dump_mem("what we think it should be: ",
256 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Steve French50c2f752007-07-13 00:33:32 +0000258 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EACCES;
260 else
261 return 0;
262
263}
264
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500265/* first calculate 24 bytes ntlm response and then 16 byte session key */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500266int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600268 int rc = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500269 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
270 char temp_key[CIFS_SESS_KEY_SIZE];
271
272 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EINVAL;
274
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500275 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -0500276 if (!ses->auth_key.response)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500277 return -ENOMEM;
Joe Perchesf96637b2013-05-04 22:12:25 -0500278
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500279 ses->auth_key.len = temp_len;
280
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600281 rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500282 ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600283 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500284 cifs_dbg(FYI, "%s Can't generate NTLM response, error: %d\n",
285 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600286 return rc;
287 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500288
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500289 rc = E_md4hash(ses->password, temp_key, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600290 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500291 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
292 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600293 return rc;
294 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500295
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600296 rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
297 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500298 cifs_dbg(FYI, "%s Can't generate NTLM session key, error: %d\n",
299 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600300
301 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Steve French7c7b25b2006-06-01 19:20:10 +0000304#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French43988d72011-04-19 18:23:31 +0000305int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500306 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000307{
Ronnie Sahlberg52baa512018-12-12 11:50:00 +1000308 int i, len;
Steve French43988d72011-04-19 18:23:31 +0000309 int rc;
Aurelien Aptel97f4b722018-01-25 15:59:39 +0100310 char password_with_pad[CIFS_ENCPWD_SIZE] = {0};
Steve French7c7b25b2006-06-01 19:20:10 +0000311
Ronnie Sahlberg52baa512018-12-12 11:50:00 +1000312 if (password) {
313 for (len = 0; len < CIFS_ENCPWD_SIZE; len++)
314 if (!password[len])
315 break;
316
317 memcpy(password_with_pad, password, len);
318 }
Steve French7c7b25b2006-06-01 19:20:10 +0000319
Jeff Layton04912d62010-04-24 07:57:45 -0400320 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500321 memcpy(lnm_session_key, password_with_pad,
322 CIFS_ENCPWD_SIZE);
Steve French43988d72011-04-19 18:23:31 +0000323 return 0;
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500324 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000325
Steve French7c7b25b2006-06-01 19:20:10 +0000326 /* calculate old style session key */
327 /* calling toupper is less broken than repeatedly
328 calling nls_toupper would be since that will never
329 work for UTF8, but neither handles multibyte code pages
330 but the only alternative would be converting to UCS-16 (Unicode)
331 (using a routine something like UniStrupr) then
332 uppercasing and then converting back from Unicode - which
333 would only worth doing it if we knew it were utf8. Basically
334 utf8 and other multibyte codepages each need their own strupper
335 function since a byte at a time will ont work. */
336
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000337 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000338 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000339
Steve French43988d72011-04-19 18:23:31 +0000340 rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500341
Steve French43988d72011-04-19 18:23:31 +0000342 return rc;
Steve French7c7b25b2006-06-01 19:20:10 +0000343}
344#endif /* CIFS_WEAK_PW_HASH */
345
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500346/* Build a proper attribute value/target info pairs blob.
347 * Fill in netbios and dns domain name and workstation name
348 * and client time (total five av pairs and + one end of fields indicator.
349 * Allocate domain name which gets freed when session struct is deallocated.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500350 */
351static int
Steve French96daf2b2011-05-27 04:34:02 +0000352build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500353{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500354 unsigned int dlen;
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500355 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500356 char *defdmname = "WORKGROUP";
357 unsigned char *blobptr;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500358 struct ntlmssp2_name *attrptr;
359
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500360 if (!ses->domainName) {
361 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
362 if (!ses->domainName)
363 return -ENOMEM;
364 }
365
366 dlen = strlen(ses->domainName);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500367
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500368 /*
369 * The length of this blob is two times the size of a
370 * structure (av pair) which holds name/size
371 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
372 * unicode length of a netbios domain name
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500373 */
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500374 ses->auth_key.len = size + 2 * dlen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500375 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
376 if (!ses->auth_key.response) {
377 ses->auth_key.len = 0;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500378 return -ENOMEM;
379 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500380
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500381 blobptr = ses->auth_key.response;
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500382 attrptr = (struct ntlmssp2_name *) blobptr;
383
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500384 /*
385 * As defined in MS-NTLM 3.3.2, just this av pair field
386 * is sufficient as part of the temp
387 */
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500388 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
389 attrptr->length = cpu_to_le16(2 * dlen);
390 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
Steve Frenchacbbb762012-01-18 22:32:33 -0600391 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500392
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500393 return 0;
394}
395
396/* Server has provided av pairs/target info in the type 2 challenge
397 * packet and we have plucked it and stored within smb session.
398 * We parse that blob here to find netbios domain name to be used
399 * as part of ntlmv2 authentication (in Target String), if not already
400 * specified on the command line.
401 * If this function returns without any error but without fetching
402 * domain name, authentication may fail against some server but
403 * may not fail against other (those who are not very particular
404 * about target string i.e. for some, just user name might suffice.
405 */
406static int
Steve French96daf2b2011-05-27 04:34:02 +0000407find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500408{
409 unsigned int attrsize;
410 unsigned int type;
411 unsigned int onesize = sizeof(struct ntlmssp2_name);
412 unsigned char *blobptr;
413 unsigned char *blobend;
414 struct ntlmssp2_name *attrptr;
415
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500416 if (!ses->auth_key.len || !ses->auth_key.response)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500417 return 0;
418
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500419 blobptr = ses->auth_key.response;
420 blobend = blobptr + ses->auth_key.len;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500421
422 while (blobptr + onesize < blobend) {
423 attrptr = (struct ntlmssp2_name *) blobptr;
424 type = le16_to_cpu(attrptr->type);
425 if (type == NTLMSSP_AV_EOL)
426 break;
427 blobptr += 2; /* advance attr type */
428 attrsize = le16_to_cpu(attrptr->length);
429 blobptr += 2; /* advance attr size */
430 if (blobptr + attrsize > blobend)
431 break;
432 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
Chen Gang057d6332013-07-19 09:01:36 +0800433 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500434 break;
435 if (!ses->domainName) {
436 ses->domainName =
437 kmalloc(attrsize + 1, GFP_KERNEL);
438 if (!ses->domainName)
439 return -ENOMEM;
Steve Frenchacbbb762012-01-18 22:32:33 -0600440 cifs_from_utf16(ses->domainName,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500441 (__le16 *)blobptr, attrsize, attrsize,
Steve Frenchb6938552014-09-25 13:20:05 -0500442 nls_cp, NO_MAP_UNI_RSVD);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500443 break;
444 }
445 }
446 blobptr += attrsize; /* advance attr value */
447 }
448
449 return 0;
450}
451
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200452/* Server has provided av pairs/target info in the type 2 challenge
453 * packet and we have plucked it and stored within smb session.
454 * We parse that blob here to find the server given timestamp
455 * as part of ntlmv2 authentication (or local current time as
456 * default in case of failure)
457 */
458static __le64
459find_timestamp(struct cifs_ses *ses)
460{
461 unsigned int attrsize;
462 unsigned int type;
463 unsigned int onesize = sizeof(struct ntlmssp2_name);
464 unsigned char *blobptr;
465 unsigned char *blobend;
466 struct ntlmssp2_name *attrptr;
Arnd Bergmann95390202018-06-19 17:27:58 +0200467 struct timespec64 ts;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200468
469 if (!ses->auth_key.len || !ses->auth_key.response)
470 return 0;
471
472 blobptr = ses->auth_key.response;
473 blobend = blobptr + ses->auth_key.len;
474
475 while (blobptr + onesize < blobend) {
476 attrptr = (struct ntlmssp2_name *) blobptr;
477 type = le16_to_cpu(attrptr->type);
478 if (type == NTLMSSP_AV_EOL)
479 break;
480 blobptr += 2; /* advance attr type */
481 attrsize = le16_to_cpu(attrptr->length);
482 blobptr += 2; /* advance attr size */
483 if (blobptr + attrsize > blobend)
484 break;
485 if (type == NTLMSSP_AV_TIMESTAMP) {
486 if (attrsize == sizeof(u64))
487 return *((__le64 *)blobptr);
488 }
489 blobptr += attrsize; /* advance attr value */
490 }
491
Arnd Bergmann95390202018-06-19 17:27:58 +0200492 ktime_get_real_ts64(&ts);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700493 return cpu_to_le64(cifs_UnixTimeToNT(ts));
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200494}
495
Steve French96daf2b2011-05-27 04:34:02 +0000496static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
Steve French50c2f752007-07-13 00:33:32 +0000497 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000498{
499 int rc = 0;
500 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500501 char nt_hash[CIFS_NTHASH_SIZE];
Steve Frenchfdf96a92013-06-25 14:03:16 -0500502 __le16 *user;
Steve French50c2f752007-07-13 00:33:32 +0000503 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500504 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000505
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500506 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500507 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500508 return -1;
509 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000510
511 /* calculate md4 hash of password */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500512 E_md4hash(ses->password, nt_hash, nls_cp);
Steve Frencha8ee0342006-06-05 23:34:19 +0000513
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500514 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500515 CIFS_NTHASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500516 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500517 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500518 return rc;
519 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500520
521 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
522 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500523 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500524 return rc;
525 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000526
Steve Frenchfdf96a92013-06-25 14:03:16 -0500527 /* convert ses->user_name to unicode */
Jeff Layton04febab2012-01-17 16:09:15 -0500528 len = ses->user_name ? strlen(ses->user_name) : 0;
Steve French1717ffc2006-06-08 05:41:32 +0000529 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500530 if (user == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500531 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500532 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500533 }
Jeff Layton04febab2012-01-17 16:09:15 -0500534
535 if (len) {
Steve Frenchfdf96a92013-06-25 14:03:16 -0500536 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
Jeff Layton04febab2012-01-17 16:09:15 -0500537 UniStrupr(user);
538 } else {
539 memset(user, '\0', 2);
540 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500541
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500542 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500543 (char *)user, 2 * len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500544 kfree(user);
545 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500546 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500547 return rc;
548 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000549
550 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000551 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000552 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000553
Steve French50c2f752007-07-13 00:33:32 +0000554 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500555 if (domain == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500556 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500557 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500558 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600559 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
560 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500561 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500562 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
563 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000564 kfree(domain);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500565 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500566 cifs_dbg(VFS, "%s: Could not update with domain\n",
567 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500568 return rc;
569 }
Steve French8b7a4542015-03-30 16:58:17 -0500570 } else {
571 /* We use ses->serverName if no domain name available */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500572 len = strlen(ses->serverName);
573
574 server = kmalloc(2 + (len * 2), GFP_KERNEL);
575 if (server == NULL) {
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500576 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500577 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500578 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600579 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500580 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500581 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500582 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
583 (char *)server, 2 * len);
584 kfree(server);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500585 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500586 cifs_dbg(VFS, "%s: Could not update with server\n",
587 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500588 return rc;
589 }
Steve French1717ffc2006-06-08 05:41:32 +0000590 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500591
592 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500593 ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500594 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500595 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500596
Steve Frencha8ee0342006-06-05 23:34:19 +0000597 return rc;
598}
599
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500600static int
Steve French96daf2b2011-05-27 04:34:02 +0000601CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500602{
603 int rc;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700604 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
605 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
606 unsigned int hash_len;
607
608 /* The MD5 hash starts at challenge_key.key */
609 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
610 offsetof(struct ntlmv2_resp, challenge.key[0]));
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500611
612 if (!ses->server->secmech.sdeschmacmd5) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500613 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500614 return -1;
615 }
616
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500617 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700618 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500619 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500620 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
621 __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500622 return rc;
623 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500624
625 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
626 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500627 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500628 return rc;
629 }
630
Jeff Layton3f618222013-06-12 19:52:14 -0500631 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
Tim Gardner2c957dd2013-11-07 16:40:57 -0700632 memcpy(ntlmv2->challenge.key,
633 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500634 else
Tim Gardner2c957dd2013-11-07 16:40:57 -0700635 memcpy(ntlmv2->challenge.key,
636 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500637 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700638 ntlmv2->challenge.key, hash_len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500639 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500640 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500641 return rc;
642 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500643
Tim Gardner2c957dd2013-11-07 16:40:57 -0700644 /* Note that the MD5 digest over writes anon.challenge_key.key */
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500645 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700646 ntlmv2->ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500647 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500648 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500649
650 return rc;
651}
652
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500653int
Steve French96daf2b2011-05-27 04:34:02 +0000654setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000655{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000656 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500657 int baselen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500658 unsigned int tilen;
Tim Gardner2c957dd2013-11-07 16:40:57 -0700659 struct ntlmv2_resp *ntlmv2;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500660 char ntlmv2_hash[16];
661 unsigned char *tiblob = NULL; /* target info blob */
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200662 __le64 rsp_timestamp;
Steve French6d027cf2006-06-05 16:26:05 +0000663
Jeff Layton3f618222013-06-12 19:52:14 -0500664 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500665 if (!ses->domainName) {
Germano Percossi39566442016-12-15 12:31:18 +0530666 if (ses->domainAuto) {
667 rc = find_domain_name(ses, nls_cp);
668 if (rc) {
669 cifs_dbg(VFS, "error %d finding domain name\n",
670 rc);
671 goto setup_ntlmv2_rsp_ret;
672 }
673 } else {
674 ses->domainName = kstrdup("", GFP_KERNEL);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500675 }
676 }
677 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500678 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500679 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500680 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500681 goto setup_ntlmv2_rsp_ret;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500682 }
683 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000684
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200685 /* Must be within 5 minutes of the server (or in range +/-2h
686 * in case of Mac OS X), so simply carry over server timestamp
687 * (as Windows 7 does)
688 */
689 rsp_timestamp = find_timestamp(ses);
690
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500691 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500692 tilen = ses->auth_key.len;
693 tiblob = ses->auth_key.response;
694
695 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500696 if (!ses->auth_key.response) {
Anton Protopopov4b550af2016-02-10 12:50:21 -0500697 rc = -ENOMEM;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500698 ses->auth_key.len = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500699 goto setup_ntlmv2_rsp_ret;
700 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500701 ses->auth_key.len += baselen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500702
Tim Gardner2c957dd2013-11-07 16:40:57 -0700703 ntlmv2 = (struct ntlmv2_resp *)
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500704 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
Tim Gardner2c957dd2013-11-07 16:40:57 -0700705 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
706 ntlmv2->reserved = 0;
Peter Seiderer98ce94c2015-09-17 21:40:12 +0200707 ntlmv2->time = rsp_timestamp;
708
Tim Gardner2c957dd2013-11-07 16:40:57 -0700709 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
710 ntlmv2->reserved2 = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500711
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500712 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500713
Rabin Vincentbd975d12016-07-19 09:26:21 +0200714 mutex_lock(&ses->server->srv_mutex);
715
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100716 rc = cifs_alloc_hash("hmac(md5)",
717 &ses->server->secmech.hmacmd5,
718 &ses->server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500719 if (rc) {
Rabin Vincentbd975d12016-07-19 09:26:21 +0200720 goto unlock;
Steve French95dc8dd2013-07-04 10:35:21 -0500721 }
722
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500723 /* calculate ntlmv2_hash */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500724 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500725 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500726 cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200727 goto unlock;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500728 }
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500729
730 /* calculate first part of the client response (CR1) */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500731 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500732 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500733 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200734 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500735 }
Steve Frenchb609f062007-07-09 07:55:14 +0000736
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500737 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500738 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500739 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500740 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500741 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
742 __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200743 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500744 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500745
746 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
747 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500748 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200749 goto unlock;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500750 }
751
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500752 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Tim Gardner2c957dd2013-11-07 16:40:57 -0700753 ntlmv2->ntlmv2_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500754 CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500755 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500756 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Rabin Vincentbd975d12016-07-19 09:26:21 +0200757 goto unlock;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500758 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500759
760 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
761 ses->auth_key.response);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500762 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500763 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500764
Rabin Vincentbd975d12016-07-19 09:26:21 +0200765unlock:
766 mutex_unlock(&ses->server->srv_mutex);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500767setup_ntlmv2_rsp_ret:
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500768 kfree(tiblob);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500769
770 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000771}
772
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500773int
Steve French96daf2b2011-05-27 04:34:02 +0000774calc_seckey(struct cifs_ses *ses)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500775{
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +0200776 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
777 struct arc4_ctx *ctx_arc4;
Sachin Prabhu5f4b5562016-10-17 16:40:22 -0400778
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +0200779 if (fips_enabled)
780 return -ENODEV;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500781
782 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
783
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +0200784 ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL);
785 if (!ctx_arc4) {
786 cifs_dbg(VFS, "could not allocate arc4 context\n");
787 return -ENOMEM;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500788 }
789
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +0200790 arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
791 arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
792 CIFS_CPHTXT_SIZE);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500793
794 /* make secondary_key/nonce as session key */
795 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
796 /* and make len as that of session key only */
797 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
798
Ard Biesheuvel97a5fee2019-06-12 18:19:59 +0200799 memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
800 kzfree(ctx_arc4);
801 return 0;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500802}
803
804void
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700805cifs_crypto_secmech_release(struct TCP_Server_Info *server)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500806{
Steve French95dc8dd2013-07-04 10:35:21 -0500807 if (server->secmech.cmacaes) {
Steve French429b46f2013-06-26 23:45:05 -0500808 crypto_free_shash(server->secmech.cmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500809 server->secmech.cmacaes = NULL;
810 }
Steve French429b46f2013-06-26 23:45:05 -0500811
Steve French95dc8dd2013-07-04 10:35:21 -0500812 if (server->secmech.hmacsha256) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700813 crypto_free_shash(server->secmech.hmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500814 server->secmech.hmacsha256 = NULL;
815 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700816
Steve French95dc8dd2013-07-04 10:35:21 -0500817 if (server->secmech.md5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500818 crypto_free_shash(server->secmech.md5);
Steve French95dc8dd2013-07-04 10:35:21 -0500819 server->secmech.md5 = NULL;
820 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500821
Gustavo A. R. Silva70e80652018-02-19 11:11:13 -0600822 if (server->secmech.sha512) {
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100823 crypto_free_shash(server->secmech.sha512);
824 server->secmech.sha512 = NULL;
825 }
826
Steve French95dc8dd2013-07-04 10:35:21 -0500827 if (server->secmech.hmacmd5) {
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500828 crypto_free_shash(server->secmech.hmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500829 server->secmech.hmacmd5 = NULL;
830 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500831
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700832 if (server->secmech.ccmaesencrypt) {
833 crypto_free_aead(server->secmech.ccmaesencrypt);
834 server->secmech.ccmaesencrypt = NULL;
835 }
836
837 if (server->secmech.ccmaesdecrypt) {
838 crypto_free_aead(server->secmech.ccmaesdecrypt);
839 server->secmech.ccmaesdecrypt = NULL;
840 }
841
Steve French429b46f2013-06-26 23:45:05 -0500842 kfree(server->secmech.sdesccmacaes);
Steve French95dc8dd2013-07-04 10:35:21 -0500843 server->secmech.sdesccmacaes = NULL;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700844 kfree(server->secmech.sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -0500845 server->secmech.sdeschmacsha256 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500846 kfree(server->secmech.sdeschmacmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500847 server->secmech.sdeschmacmd5 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500848 kfree(server->secmech.sdescmd5);
Steve French95dc8dd2013-07-04 10:35:21 -0500849 server->secmech.sdescmd5 = NULL;
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100850 kfree(server->secmech.sdescsha512);
851 server->secmech.sdescsha512 = NULL;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500852}