blob: ebccd71cc60a3887f398829897cfc4c1956bfa91 [file] [log] [blame]
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001/*
2 * fs/cifs/smb2transport.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002, 2011
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
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>
26#include <linux/list.h>
27#include <linux/wait.h>
28#include <linux/net.h>
29#include <linux/delay.h>
30#include <linux/uaccess.h>
31#include <asm/processor.h>
32#include <linux/mempool.h>
Jeff Laytonfb308a62012-09-18 16:20:35 -070033#include <linux/highmem.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070034#include <crypto/aead.h>
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040035#include "smb2pdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "smb2proto.h"
39#include "cifs_debug.h"
40#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070041#include "smb2glob.h"
42
Steve French95dc8dd2013-07-04 10:35:21 -050043static int
Steve French95dc8dd2013-07-04 10:35:21 -050044smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
45{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010046 struct cifs_secmech *p = &server->secmech;
Steve French95dc8dd2013-07-04 10:35:21 -050047 int rc;
48
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010049 rc = cifs_alloc_hash("hmac(sha256)",
50 &p->hmacsha256,
51 &p->sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050052 if (rc)
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010053 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050054
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010055 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
56 if (rc)
57 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050058
59 return 0;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010060err:
61 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
62 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050063}
64
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010065int
66smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
67{
68 struct cifs_secmech *p = &server->secmech;
69 int rc = 0;
70
71 rc = cifs_alloc_hash("hmac(sha256)",
72 &p->hmacsha256,
73 &p->sdeschmacsha256);
74 if (rc)
75 return rc;
76
77 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
78 if (rc)
79 goto err;
80
81 rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
82 if (rc)
83 goto err;
84
85 return 0;
86
87err:
88 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
89 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
90 return rc;
91}
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010092
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020093
94static
95int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
96{
97 struct cifs_chan *chan;
98 struct cifs_ses *ses = NULL;
Aurelien Aptelcc95b672020-02-06 13:49:26 +010099 struct TCP_Server_Info *it = NULL;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200100 int i;
101 int rc = 0;
102
103 spin_lock(&cifs_tcp_ses_lock);
104
Aurelien Aptelcc95b672020-02-06 13:49:26 +0100105 list_for_each_entry(it, &cifs_tcp_ses_list, tcp_ses_list) {
106 list_for_each_entry(ses, &it->smb_ses_list, smb_ses_list) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200107 if (ses->Suid == ses_id)
108 goto found;
109 }
110 }
111 cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
112 __func__, ses_id);
113 rc = -ENOENT;
114 goto out;
115
116found:
117 if (ses->binding) {
118 /*
119 * If we are in the process of binding a new channel
120 * to an existing session, use the master connection
121 * session key
122 */
123 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
124 goto out;
125 }
126
127 /*
128 * Otherwise, use the channel key.
129 */
130
131 for (i = 0; i < ses->chan_count; i++) {
132 chan = ses->chans + i;
133 if (chan->server == server) {
134 memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
135 goto out;
136 }
137 }
138
139 cifs_dbg(VFS,
140 "%s: Could not find channel signing key for session 0x%llx\n",
141 __func__, ses_id);
142 rc = -ENOENT;
143
144out:
145 spin_unlock(&cifs_tcp_ses_lock);
146 return rc;
147}
148
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800149static struct cifs_ses *
150smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
151{
152 struct cifs_ses *ses;
153
154 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
155 if (ses->Suid != ses_id)
156 continue;
157 return ses;
158 }
159
160 return NULL;
161}
162
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700163struct cifs_ses *
164smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500165{
166 struct cifs_ses *ses;
167
168 spin_lock(&cifs_tcp_ses_lock);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800169 ses = smb2_find_smb_ses_unlocked(server, ses_id);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500170 spin_unlock(&cifs_tcp_ses_lock);
171
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800172 return ses;
173}
174
175static struct cifs_tcon *
176smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
177{
178 struct cifs_tcon *tcon;
179
180 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
181 if (tcon->tid != tid)
182 continue;
183 ++tcon->tc_count;
184 return tcon;
185 }
186
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500187 return NULL;
188}
189
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800190/*
191 * Obtain tcon corresponding to the tid in the given
192 * cifs_ses
193 */
194
195struct cifs_tcon *
196smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
197{
198 struct cifs_ses *ses;
199 struct cifs_tcon *tcon;
200
201 spin_lock(&cifs_tcp_ses_lock);
202 ses = smb2_find_smb_ses_unlocked(server, ses_id);
203 if (!ses) {
204 spin_unlock(&cifs_tcp_ses_lock);
205 return NULL;
206 }
207 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
208 spin_unlock(&cifs_tcp_ses_lock);
209
210 return tcon;
211}
212
Steve French38107d42012-12-08 22:08:06 -0600213int
Long Lieda1c542020-03-31 16:21:43 -0700214smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
215 bool allocate_crypto)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700216{
Al Viro16c568e2015-11-12 22:46:49 -0500217 int rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700218 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
219 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700220 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000221 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500222 struct cifs_ses *ses;
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200223 struct shash_desc *shash;
Long Lieda1c542020-03-31 16:21:43 -0700224 struct crypto_shash *hash;
225 struct sdesc *sdesc = NULL;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300226 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500227
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700228 ses = smb2_find_smb_ses(server, shdr->SessionId);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500229 if (!ses) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000230 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500231 return 0;
232 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700233
234 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700235 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700236
Long Lieda1c542020-03-31 16:21:43 -0700237 if (allocate_crypto) {
238 rc = cifs_alloc_hash("hmac(sha256)", &hash, &sdesc);
239 if (rc) {
240 cifs_server_dbg(VFS,
241 "%s: sha256 alloc failed\n", __func__);
242 return rc;
243 }
244 shash = &sdesc->shash;
245 } else {
246 hash = server->secmech.hmacsha256;
247 shash = &server->secmech.sdeschmacsha256->shash;
Steve French95dc8dd2013-07-04 10:35:21 -0500248 }
249
Long Lieda1c542020-03-31 16:21:43 -0700250 rc = crypto_shash_setkey(hash, ses->auth_key.response,
251 SMB2_NTLMV2_SESSKEY_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700252 if (rc) {
Long Lieda1c542020-03-31 16:21:43 -0700253 cifs_server_dbg(VFS,
254 "%s: Could not update with response\n",
255 __func__);
256 goto out;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700257 }
258
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300259 rc = crypto_shash_init(shash);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700260 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000261 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700262 goto out;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700263 }
264
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300265 /*
266 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
267 * data, that is, iov[0] should not contain a rfc1002 length.
268 *
269 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
270 * __cifs_calc_signature().
271 */
272 drqst = *rqst;
273 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
274 rc = crypto_shash_update(shash, iov[0].iov_base,
275 iov[0].iov_len);
276 if (rc) {
Long Lieda1c542020-03-31 16:21:43 -0700277 cifs_server_dbg(VFS,
278 "%s: Could not update with payload\n",
279 __func__);
280 goto out;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300281 }
282 drqst.rq_iov++;
283 drqst.rq_nvec--;
284 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700285
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300286 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500287 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700288 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700289
Long Lieda1c542020-03-31 16:21:43 -0700290out:
291 if (allocate_crypto)
292 cifs_free_hash(&hash, &sdesc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700293 return rc;
294}
295
Steve French373512e2015-12-18 13:05:30 -0600296static int generate_key(struct cifs_ses *ses, struct kvec label,
297 struct kvec context, __u8 *key, unsigned int key_size)
Steve French429b46f2013-06-26 23:45:05 -0500298{
299 unsigned char zero = 0x0;
300 __u8 i[4] = {0, 0, 0, 1};
301 __u8 L[4] = {0, 0, 0, 128};
302 int rc = 0;
303 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
304 unsigned char *hashptr = prfhash;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000305 struct TCP_Server_Info *server = ses->server;
Steve French429b46f2013-06-26 23:45:05 -0500306
307 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600308 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500309
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000310 rc = smb3_crypto_shash_allocate(server);
Steve French95dc8dd2013-07-04 10:35:21 -0500311 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000312 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500313 goto smb3signkey_ret;
314 }
315
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000316 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500317 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500318 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000319 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500320 goto smb3signkey_ret;
321 }
322
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000323 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500324 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000325 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500326 goto smb3signkey_ret;
327 }
328
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000329 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500330 i, 4);
331 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000332 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500333 goto smb3signkey_ret;
334 }
335
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000336 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600337 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500338 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000339 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500340 goto smb3signkey_ret;
341 }
342
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000343 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500344 &zero, 1);
345 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000346 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500347 goto smb3signkey_ret;
348 }
349
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000350 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600351 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500352 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000353 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500354 goto smb3signkey_ret;
355 }
356
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000357 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500358 L, 4);
359 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000360 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500361 goto smb3signkey_ret;
362 }
363
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000364 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500365 hashptr);
366 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000367 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500368 goto smb3signkey_ret;
369 }
370
Steve French373512e2015-12-18 13:05:30 -0600371 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500372
373smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500374 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500375}
376
Steve French373512e2015-12-18 13:05:30 -0600377struct derivation {
378 struct kvec label;
379 struct kvec context;
380};
381
382struct derivation_triplet {
383 struct derivation signing;
384 struct derivation encryption;
385 struct derivation decryption;
386};
387
388static int
389generate_smb3signingkey(struct cifs_ses *ses,
390 const struct derivation_triplet *ptriplet)
391{
392 int rc;
393
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200394 /*
395 * All channels use the same encryption/decryption keys but
396 * they have their own signing key.
397 *
398 * When we generate the keys, check if it is for a new channel
399 * (binding) in which case we only need to generate a signing
400 * key and store it in the channel as to not overwrite the
401 * master connection signing key stored in the session
402 */
Steve French373512e2015-12-18 13:05:30 -0600403
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200404 if (ses->binding) {
405 rc = generate_key(ses, ptriplet->signing.label,
406 ptriplet->signing.context,
407 cifs_ses_binding_channel(ses)->signkey,
408 SMB3_SIGN_KEY_SIZE);
409 if (rc)
410 return rc;
411 } else {
412 rc = generate_key(ses, ptriplet->signing.label,
413 ptriplet->signing.context,
414 ses->smb3signingkey,
415 SMB3_SIGN_KEY_SIZE);
416 if (rc)
417 return rc;
Paulo Alcantara (SUSE)ff6b6f32019-11-22 12:30:57 -0300418
419 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
420 SMB3_SIGN_KEY_SIZE);
421
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200422 rc = generate_key(ses, ptriplet->encryption.label,
423 ptriplet->encryption.context,
424 ses->smb3encryptionkey,
425 SMB3_SIGN_KEY_SIZE);
426 rc = generate_key(ses, ptriplet->decryption.label,
427 ptriplet->decryption.context,
428 ses->smb3decryptionkey,
429 SMB3_SIGN_KEY_SIZE);
430 if (rc)
431 return rc;
432 }
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200433
434 if (rc)
435 return rc;
436
437#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
438 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
439 /*
440 * The session id is opaque in terms of endianness, so we can't
441 * print it as a long long. we dump it as we got it on the wire
442 */
443 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
444 &ses->Suid);
445 cifs_dbg(VFS, "Session Key %*ph\n",
446 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
447 cifs_dbg(VFS, "Signing Key %*ph\n",
448 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
449 cifs_dbg(VFS, "ServerIn Key %*ph\n",
450 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
451 cifs_dbg(VFS, "ServerOut Key %*ph\n",
452 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
453#endif
454 return rc;
Steve French373512e2015-12-18 13:05:30 -0600455}
456
457int
458generate_smb30signingkey(struct cifs_ses *ses)
459
460{
461 struct derivation_triplet triplet;
462 struct derivation *d;
463
464 d = &triplet.signing;
465 d->label.iov_base = "SMB2AESCMAC";
466 d->label.iov_len = 12;
467 d->context.iov_base = "SmbSign";
468 d->context.iov_len = 8;
469
470 d = &triplet.encryption;
471 d->label.iov_base = "SMB2AESCCM";
472 d->label.iov_len = 11;
473 d->context.iov_base = "ServerIn ";
474 d->context.iov_len = 10;
475
476 d = &triplet.decryption;
477 d->label.iov_base = "SMB2AESCCM";
478 d->label.iov_len = 11;
479 d->context.iov_base = "ServerOut";
480 d->context.iov_len = 10;
481
482 return generate_smb3signingkey(ses, &triplet);
483}
484
485int
486generate_smb311signingkey(struct cifs_ses *ses)
487
488{
489 struct derivation_triplet triplet;
490 struct derivation *d;
491
492 d = &triplet.signing;
Steve French06e22902017-09-25 20:11:58 -0500493 d->label.iov_base = "SMBSigningKey";
494 d->label.iov_len = 14;
495 d->context.iov_base = ses->preauth_sha_hash;
496 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600497
498 d = &triplet.encryption;
Steve French06e22902017-09-25 20:11:58 -0500499 d->label.iov_base = "SMBC2SCipherKey";
500 d->label.iov_len = 16;
501 d->context.iov_base = ses->preauth_sha_hash;
502 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600503
504 d = &triplet.decryption;
Steve French06e22902017-09-25 20:11:58 -0500505 d->label.iov_base = "SMBS2CCipherKey";
506 d->label.iov_len = 16;
507 d->context.iov_base = ses->preauth_sha_hash;
508 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600509
510 return generate_smb3signingkey(ses, &triplet);
511}
512
Steve French429b46f2013-06-26 23:45:05 -0500513int
Long Lieda1c542020-03-31 16:21:43 -0700514smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
515 bool allocate_crypto)
Steve French38107d42012-12-08 22:08:06 -0600516{
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300517 int rc;
Steve French429b46f2013-06-26 23:45:05 -0500518 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
519 unsigned char *sigptr = smb3_signature;
520 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000521 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Long Lieda1c542020-03-31 16:21:43 -0700522 struct shash_desc *shash;
523 struct crypto_shash *hash;
524 struct sdesc *sdesc = NULL;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300525 struct smb_rqst drqst;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200526 u8 key[SMB3_SIGN_KEY_SIZE];
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500527
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200528 rc = smb2_get_sign_key(shdr->SessionId, server, key);
529 if (rc)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500530 return 0;
Steve French429b46f2013-06-26 23:45:05 -0500531
Long Lieda1c542020-03-31 16:21:43 -0700532 if (allocate_crypto) {
533 rc = cifs_alloc_hash("cmac(aes)", &hash, &sdesc);
534 if (rc)
535 return rc;
536
537 shash = &sdesc->shash;
538 } else {
539 hash = server->secmech.cmacaes;
540 shash = &server->secmech.sdesccmacaes->shash;
541 }
542
Steve French429b46f2013-06-26 23:45:05 -0500543 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700544 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500545
Long Lieda1c542020-03-31 16:21:43 -0700546 rc = crypto_shash_setkey(hash, key, SMB2_CMACAES_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500547 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000548 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700549 goto out;
Steve French429b46f2013-06-26 23:45:05 -0500550 }
551
Steve French95dc8dd2013-07-04 10:35:21 -0500552 /*
553 * we already allocate sdesccmacaes when we init smb3 signing key,
554 * so unlike smb2 case we do not have to check here if secmech are
555 * initialized
556 */
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300557 rc = crypto_shash_init(shash);
Steve French429b46f2013-06-26 23:45:05 -0500558 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000559 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700560 goto out;
Steve French429b46f2013-06-26 23:45:05 -0500561 }
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100562
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300563 /*
564 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
565 * data, that is, iov[0] should not contain a rfc1002 length.
566 *
567 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
568 * __cifs_calc_signature().
569 */
570 drqst = *rqst;
571 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
572 rc = crypto_shash_update(shash, iov[0].iov_base,
573 iov[0].iov_len);
574 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000575 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300576 __func__);
Long Lieda1c542020-03-31 16:21:43 -0700577 goto out;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300578 }
579 drqst.rq_iov++;
580 drqst.rq_nvec--;
581 }
Steve French429b46f2013-06-26 23:45:05 -0500582
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300583 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500584 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700585 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500586
Long Lieda1c542020-03-31 16:21:43 -0700587out:
588 if (allocate_crypto)
589 cifs_free_hash(&hash, &sdesc);
Steve French429b46f2013-06-26 23:45:05 -0500590 return rc;
Steve French38107d42012-12-08 22:08:06 -0600591}
592
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700593/* must be called with server->srv_mutex held */
594static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700595smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700596{
597 int rc = 0;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200598 struct smb2_sync_hdr *shdr;
599 struct smb2_sess_setup_req *ssr;
600 bool is_binding;
601 bool is_signed;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700602
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200603 shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
604 ssr = (struct smb2_sess_setup_req *)shdr;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700605
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200606 is_binding = shdr->Command == SMB2_SESSION_SETUP &&
607 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
608 is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
609
610 if (!is_signed)
611 return 0;
612 if (server->tcpStatus == CifsNeedNegotiate)
613 return 0;
614 if (!is_binding && !server->session_estab) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700615 strncpy(shdr->Signature, "BSRSPYL", 8);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200616 return 0;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700617 }
618
Long Lieda1c542020-03-31 16:21:43 -0700619 rc = server->ops->calc_signature(rqst, server, false);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700620
621 return rc;
622}
623
624int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700625smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700626{
627 unsigned int rc;
Steve Frenchedad7342020-03-27 12:47:41 -0500628 char server_response_sig[SMB2_SIGNATURE_SIZE];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800629 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000630 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700631
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700632 if ((shdr->Command == SMB2_NEGOTIATE) ||
633 (shdr->Command == SMB2_SESSION_SETUP) ||
634 (shdr->Command == SMB2_OPLOCK_BREAK) ||
Steve French4f5c10f2019-09-03 21:18:49 -0500635 server->ignore_signature ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700636 (!server->session_estab))
637 return 0;
638
639 /*
640 * BB what if signatures are supposed to be on for session but
641 * server does not send one? BB
642 */
643
644 /* Do not need to verify session setups with signature "BSRSPYL " */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700645 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500646 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700647 shdr->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700648
649 /*
650 * Save off the origiginal signature so we can modify the smb and check
651 * our calculated signature against what the server sent.
652 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700653 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700654
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700655 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700656
Long Lieda1c542020-03-31 16:21:43 -0700657 rc = server->ops->calc_signature(rqst, server, true);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700658
659 if (rc)
660 return rc;
661
Steve Frenchf460c502020-03-29 16:44:43 -0500662 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE)) {
Steve French9692ea92020-04-15 01:12:34 -0500663 cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx\n",
664 shdr->Command, shdr->MessageId);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700665 return -EACCES;
Steve Frenchf460c502020-03-29 16:44:43 -0500666 } else
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700667 return 0;
668}
669
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400670/*
671 * Set message id for the request. Should be called after wait_for_free_request
672 * and when srv_mutex is held.
673 */
674static inline void
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700675smb2_seq_num_into_buf(struct TCP_Server_Info *server,
676 struct smb2_sync_hdr *shdr)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400677{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700678 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400679
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700680 shdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400681 /* skip message numbers according to CreditCharge field */
682 for (i = 1; i < num; i++)
683 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400684}
685
686static struct mid_q_entry *
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700687smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400688 struct TCP_Server_Info *server)
689{
690 struct mid_q_entry *temp;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800691 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400692
693 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500694 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400695 return NULL;
696 }
697
698 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +1000699 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +0200700 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +1000701 temp->mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800702 temp->credits = credits > 0 ? credits : 1;
NeilBrowna6f74e82017-04-10 12:08:53 +1000703 temp->pid = current->pid;
704 temp->command = shdr->Command; /* Always LE */
705 temp->when_alloc = jiffies;
706 temp->server = server;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400707
NeilBrowna6f74e82017-04-10 12:08:53 +1000708 /*
709 * The default is for the mid to be synchronous, so the
710 * default callback just wakes up the current task.
711 */
Vincent Whitchurchf1f27ad2020-01-23 17:09:06 +0100712 get_task_struct(current);
713 temp->creator = current;
NeilBrowna6f74e82017-04-10 12:08:53 +1000714 temp->callback = cifs_wake_up_task;
715 temp->callback_data = current;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400716
717 atomic_inc(&midCount);
718 temp->mid_state = MID_REQUEST_ALLOCATED;
Steve French53a3e0d2019-02-26 21:26:20 -0600719 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
720 le16_to_cpu(shdr->Command), temp->mid);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400721 return temp;
722}
723
724static int
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200725smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
726 struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400727{
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200728 if (server->tcpStatus == CifsExiting)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400729 return -ENOENT;
730
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200731 if (server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500732 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400733 return -EAGAIN;
734 }
735
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200736 if (server->tcpStatus == CifsNeedNegotiate &&
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800737 shdr->Command != SMB2_NEGOTIATE)
738 return -EAGAIN;
739
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500740 if (ses->status == CifsNew) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700741 if ((shdr->Command != SMB2_SESSION_SETUP) &&
742 (shdr->Command != SMB2_NEGOTIATE))
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400743 return -EAGAIN;
744 /* else ok - we are setting up session */
745 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500746
747 if (ses->status == CifsExiting) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700748 if (shdr->Command != SMB2_LOGOFF)
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500749 return -EAGAIN;
750 /* else ok - we are shutting down the session */
751 }
752
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200753 *mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400754 if (*mid == NULL)
755 return -ENOMEM;
756 spin_lock(&GlobalMid_Lock);
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200757 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400758 spin_unlock(&GlobalMid_Lock);
Steve French53a3e0d2019-02-26 21:26:20 -0600759
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400760 return 0;
761}
762
763int
764smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
765 bool log_error)
766{
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000767 unsigned int len = mid->resp_buf_size;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000768 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800769 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000770 .rq_nvec = 1 };
Jeff Layton0b688cf2012-09-18 16:20:34 -0700771
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800772 iov[0].iov_base = (char *)mid->resp_buf;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000773 iov[0].iov_len = len;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400774
775 dump_smb(mid->resp_buf, min_t(u32, 80, len));
776 /* convert the length into a more usable form */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800777 if (len > 24 && server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700778 int rc;
779
Jeff Layton0b688cf2012-09-18 16:20:34 -0700780 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700781 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000782 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500783 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700784 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400785
786 return map_smb2_to_linux_error(mid->resp_buf, log_error);
787}
788
Jeff Laytonfec344e2012-09-18 16:20:35 -0700789struct mid_q_entry *
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200790smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
791 struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400792{
793 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800794 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000795 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400796 struct mid_q_entry *mid;
797
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200798 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400799
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200800 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800801 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200802 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700803 return ERR_PTR(rc);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800804 }
805
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200806 rc = smb2_sign_rqst(rqst, server);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700807 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200808 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700809 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700810 return ERR_PTR(rc);
811 }
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800812
Jeff Laytonfec344e2012-09-18 16:20:35 -0700813 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400814}
815
Jeff Laytonfec344e2012-09-18 16:20:35 -0700816struct mid_q_entry *
817smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400818{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700819 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800820 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000821 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400822 struct mid_q_entry *mid;
823
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800824 if (server->tcpStatus == CifsNeedNegotiate &&
825 shdr->Command != SMB2_NEGOTIATE)
826 return ERR_PTR(-EAGAIN);
827
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700828 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400829
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700830 mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800831 if (mid == NULL) {
832 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700833 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800834 }
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400835
Jeff Laytonfec344e2012-09-18 16:20:35 -0700836 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400837 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800838 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400839 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700840 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700841 }
842
Jeff Laytonfec344e2012-09-18 16:20:35 -0700843 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400844}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700845
846int
847smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
848{
849 struct crypto_aead *tfm;
850
851 if (!server->secmech.ccmaesencrypt) {
Steve French63ca5652020-10-15 23:41:40 -0500852 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
853 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve French2b2f7542019-06-07 15:16:10 -0500854 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
855 else
856 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700857 if (IS_ERR(tfm)) {
Steve French63ca5652020-10-15 23:41:40 -0500858 cifs_server_dbg(VFS, "%s: Failed alloc encrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700859 __func__);
860 return PTR_ERR(tfm);
861 }
862 server->secmech.ccmaesencrypt = tfm;
863 }
864
865 if (!server->secmech.ccmaesdecrypt) {
Steve French63ca5652020-10-15 23:41:40 -0500866 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
867 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve French2b2f7542019-06-07 15:16:10 -0500868 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
869 else
870 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700871 if (IS_ERR(tfm)) {
872 crypto_free_aead(server->secmech.ccmaesencrypt);
873 server->secmech.ccmaesencrypt = NULL;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000874 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700875 __func__);
876 return PTR_ERR(tfm);
877 }
878 server->secmech.ccmaesdecrypt = tfm;
879 }
880
881 return 0;
882}