blob: 387c88704c526cde72d1f1f185ec0d32f0563239 [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
44smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
45{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010046 return cifs_alloc_hash("hmac(sha256)",
47 &server->secmech.hmacsha256,
48 &server->secmech.sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050049}
50
51static int
52smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
53{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010054 struct cifs_secmech *p = &server->secmech;
Steve French95dc8dd2013-07-04 10:35:21 -050055 int rc;
56
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010057 rc = cifs_alloc_hash("hmac(sha256)",
58 &p->hmacsha256,
59 &p->sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050060 if (rc)
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010061 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050062
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010063 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
64 if (rc)
65 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050066
67 return 0;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010068err:
69 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
70 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050071}
72
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010073int
74smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
75{
76 struct cifs_secmech *p = &server->secmech;
77 int rc = 0;
78
79 rc = cifs_alloc_hash("hmac(sha256)",
80 &p->hmacsha256,
81 &p->sdeschmacsha256);
82 if (rc)
83 return rc;
84
85 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
86 if (rc)
87 goto err;
88
89 rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
90 if (rc)
91 goto err;
92
93 return 0;
94
95err:
96 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
97 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
98 return rc;
99}
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100100
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200101
102static
103int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
104{
105 struct cifs_chan *chan;
106 struct cifs_ses *ses = NULL;
107 int i;
108 int rc = 0;
109
110 spin_lock(&cifs_tcp_ses_lock);
111
112 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
113 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
114 if (ses->Suid == ses_id)
115 goto found;
116 }
117 }
118 cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
119 __func__, ses_id);
120 rc = -ENOENT;
121 goto out;
122
123found:
124 if (ses->binding) {
125 /*
126 * If we are in the process of binding a new channel
127 * to an existing session, use the master connection
128 * session key
129 */
130 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
131 goto out;
132 }
133
134 /*
135 * Otherwise, use the channel key.
136 */
137
138 for (i = 0; i < ses->chan_count; i++) {
139 chan = ses->chans + i;
140 if (chan->server == server) {
141 memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
142 goto out;
143 }
144 }
145
146 cifs_dbg(VFS,
147 "%s: Could not find channel signing key for session 0x%llx\n",
148 __func__, ses_id);
149 rc = -ENOENT;
150
151out:
152 spin_unlock(&cifs_tcp_ses_lock);
153 return rc;
154}
155
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800156static struct cifs_ses *
157smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
158{
159 struct cifs_ses *ses;
160
161 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
162 if (ses->Suid != ses_id)
163 continue;
164 return ses;
165 }
166
167 return NULL;
168}
169
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700170struct cifs_ses *
171smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500172{
173 struct cifs_ses *ses;
174
175 spin_lock(&cifs_tcp_ses_lock);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800176 ses = smb2_find_smb_ses_unlocked(server, ses_id);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500177 spin_unlock(&cifs_tcp_ses_lock);
178
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800179 return ses;
180}
181
182static struct cifs_tcon *
183smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
184{
185 struct cifs_tcon *tcon;
186
187 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
188 if (tcon->tid != tid)
189 continue;
190 ++tcon->tc_count;
191 return tcon;
192 }
193
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500194 return NULL;
195}
196
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800197/*
198 * Obtain tcon corresponding to the tid in the given
199 * cifs_ses
200 */
201
202struct cifs_tcon *
203smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
204{
205 struct cifs_ses *ses;
206 struct cifs_tcon *tcon;
207
208 spin_lock(&cifs_tcp_ses_lock);
209 ses = smb2_find_smb_ses_unlocked(server, ses_id);
210 if (!ses) {
211 spin_unlock(&cifs_tcp_ses_lock);
212 return NULL;
213 }
214 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
215 spin_unlock(&cifs_tcp_ses_lock);
216
217 return tcon;
218}
219
Steve French38107d42012-12-08 22:08:06 -0600220int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700221smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700222{
Al Viro16c568e2015-11-12 22:46:49 -0500223 int rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700224 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
225 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700226 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000227 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500228 struct cifs_ses *ses;
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200229 struct shash_desc *shash;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300230 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500231
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700232 ses = smb2_find_smb_ses(server, shdr->SessionId);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500233 if (!ses) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000234 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500235 return 0;
236 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700237
238 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700239 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700240
Steve French95dc8dd2013-07-04 10:35:21 -0500241 rc = smb2_crypto_shash_allocate(server);
242 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000243 cifs_server_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500244 return rc;
245 }
246
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700247 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300248 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700249 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000250 cifs_server_dbg(VFS, "%s: Could not update with response\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700251 return rc;
252 }
253
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200254 shash = &server->secmech.sdeschmacsha256->shash;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300255 rc = crypto_shash_init(shash);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700256 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000257 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700258 return rc;
259 }
260
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300261 /*
262 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
263 * data, that is, iov[0] should not contain a rfc1002 length.
264 *
265 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
266 * __cifs_calc_signature().
267 */
268 drqst = *rqst;
269 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
270 rc = crypto_shash_update(shash, iov[0].iov_base,
271 iov[0].iov_len);
272 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000273 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300274 __func__);
275 return rc;
276 }
277 drqst.rq_iov++;
278 drqst.rq_nvec--;
279 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700280
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300281 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500282 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700283 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700284
285 return rc;
286}
287
Steve French373512e2015-12-18 13:05:30 -0600288static int generate_key(struct cifs_ses *ses, struct kvec label,
289 struct kvec context, __u8 *key, unsigned int key_size)
Steve French429b46f2013-06-26 23:45:05 -0500290{
291 unsigned char zero = 0x0;
292 __u8 i[4] = {0, 0, 0, 1};
293 __u8 L[4] = {0, 0, 0, 128};
294 int rc = 0;
295 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
296 unsigned char *hashptr = prfhash;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000297 struct TCP_Server_Info *server = ses->server;
Steve French429b46f2013-06-26 23:45:05 -0500298
299 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600300 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500301
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000302 rc = smb3_crypto_shash_allocate(server);
Steve French95dc8dd2013-07-04 10:35:21 -0500303 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000304 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500305 goto smb3signkey_ret;
306 }
307
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000308 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500309 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500310 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000311 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500312 goto smb3signkey_ret;
313 }
314
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000315 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500316 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000317 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500318 goto smb3signkey_ret;
319 }
320
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000321 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500322 i, 4);
323 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000324 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500325 goto smb3signkey_ret;
326 }
327
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000328 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600329 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500330 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000331 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500332 goto smb3signkey_ret;
333 }
334
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000335 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500336 &zero, 1);
337 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000338 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500339 goto smb3signkey_ret;
340 }
341
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000342 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600343 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500344 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000345 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500346 goto smb3signkey_ret;
347 }
348
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000349 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500350 L, 4);
351 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000352 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500353 goto smb3signkey_ret;
354 }
355
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000356 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500357 hashptr);
358 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000359 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500360 goto smb3signkey_ret;
361 }
362
Steve French373512e2015-12-18 13:05:30 -0600363 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500364
365smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500366 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500367}
368
Steve French373512e2015-12-18 13:05:30 -0600369struct derivation {
370 struct kvec label;
371 struct kvec context;
372};
373
374struct derivation_triplet {
375 struct derivation signing;
376 struct derivation encryption;
377 struct derivation decryption;
378};
379
380static int
381generate_smb3signingkey(struct cifs_ses *ses,
382 const struct derivation_triplet *ptriplet)
383{
384 int rc;
385
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200386 /*
387 * All channels use the same encryption/decryption keys but
388 * they have their own signing key.
389 *
390 * When we generate the keys, check if it is for a new channel
391 * (binding) in which case we only need to generate a signing
392 * key and store it in the channel as to not overwrite the
393 * master connection signing key stored in the session
394 */
Steve French373512e2015-12-18 13:05:30 -0600395
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200396 if (ses->binding) {
397 rc = generate_key(ses, ptriplet->signing.label,
398 ptriplet->signing.context,
399 cifs_ses_binding_channel(ses)->signkey,
400 SMB3_SIGN_KEY_SIZE);
401 if (rc)
402 return rc;
403 } else {
404 rc = generate_key(ses, ptriplet->signing.label,
405 ptriplet->signing.context,
406 ses->smb3signingkey,
407 SMB3_SIGN_KEY_SIZE);
408 if (rc)
409 return rc;
Paulo Alcantara (SUSE)ff6b6f32019-11-22 12:30:57 -0300410
411 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
412 SMB3_SIGN_KEY_SIZE);
413
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200414 rc = generate_key(ses, ptriplet->encryption.label,
415 ptriplet->encryption.context,
416 ses->smb3encryptionkey,
417 SMB3_SIGN_KEY_SIZE);
418 rc = generate_key(ses, ptriplet->decryption.label,
419 ptriplet->decryption.context,
420 ses->smb3decryptionkey,
421 SMB3_SIGN_KEY_SIZE);
422 if (rc)
423 return rc;
424 }
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200425
426 if (rc)
427 return rc;
428
429#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
430 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
431 /*
432 * The session id is opaque in terms of endianness, so we can't
433 * print it as a long long. we dump it as we got it on the wire
434 */
435 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
436 &ses->Suid);
437 cifs_dbg(VFS, "Session Key %*ph\n",
438 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
439 cifs_dbg(VFS, "Signing Key %*ph\n",
440 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
441 cifs_dbg(VFS, "ServerIn Key %*ph\n",
442 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
443 cifs_dbg(VFS, "ServerOut Key %*ph\n",
444 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
445#endif
446 return rc;
Steve French373512e2015-12-18 13:05:30 -0600447}
448
449int
450generate_smb30signingkey(struct cifs_ses *ses)
451
452{
453 struct derivation_triplet triplet;
454 struct derivation *d;
455
456 d = &triplet.signing;
457 d->label.iov_base = "SMB2AESCMAC";
458 d->label.iov_len = 12;
459 d->context.iov_base = "SmbSign";
460 d->context.iov_len = 8;
461
462 d = &triplet.encryption;
463 d->label.iov_base = "SMB2AESCCM";
464 d->label.iov_len = 11;
465 d->context.iov_base = "ServerIn ";
466 d->context.iov_len = 10;
467
468 d = &triplet.decryption;
469 d->label.iov_base = "SMB2AESCCM";
470 d->label.iov_len = 11;
471 d->context.iov_base = "ServerOut";
472 d->context.iov_len = 10;
473
474 return generate_smb3signingkey(ses, &triplet);
475}
476
477int
478generate_smb311signingkey(struct cifs_ses *ses)
479
480{
481 struct derivation_triplet triplet;
482 struct derivation *d;
483
484 d = &triplet.signing;
Steve French06e22902017-09-25 20:11:58 -0500485 d->label.iov_base = "SMBSigningKey";
486 d->label.iov_len = 14;
487 d->context.iov_base = ses->preauth_sha_hash;
488 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600489
490 d = &triplet.encryption;
Steve French06e22902017-09-25 20:11:58 -0500491 d->label.iov_base = "SMBC2SCipherKey";
492 d->label.iov_len = 16;
493 d->context.iov_base = ses->preauth_sha_hash;
494 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600495
496 d = &triplet.decryption;
Steve French06e22902017-09-25 20:11:58 -0500497 d->label.iov_base = "SMBS2CCipherKey";
498 d->label.iov_len = 16;
499 d->context.iov_base = ses->preauth_sha_hash;
500 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600501
502 return generate_smb3signingkey(ses, &triplet);
503}
504
Steve French429b46f2013-06-26 23:45:05 -0500505int
Steve French38107d42012-12-08 22:08:06 -0600506smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
507{
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300508 int rc;
Steve French429b46f2013-06-26 23:45:05 -0500509 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
510 unsigned char *sigptr = smb3_signature;
511 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000512 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300513 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
514 struct smb_rqst drqst;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200515 u8 key[SMB3_SIGN_KEY_SIZE];
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500516
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200517 rc = smb2_get_sign_key(shdr->SessionId, server, key);
518 if (rc)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500519 return 0;
Steve French429b46f2013-06-26 23:45:05 -0500520
521 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700522 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500523
524 rc = crypto_shash_setkey(server->secmech.cmacaes,
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200525 key, SMB2_CMACAES_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500526 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000527 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500528 return rc;
529 }
530
Steve French95dc8dd2013-07-04 10:35:21 -0500531 /*
532 * we already allocate sdesccmacaes when we init smb3 signing key,
533 * so unlike smb2 case we do not have to check here if secmech are
534 * initialized
535 */
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300536 rc = crypto_shash_init(shash);
Steve French429b46f2013-06-26 23:45:05 -0500537 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000538 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500539 return rc;
540 }
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100541
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300542 /*
543 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
544 * data, that is, iov[0] should not contain a rfc1002 length.
545 *
546 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
547 * __cifs_calc_signature().
548 */
549 drqst = *rqst;
550 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
551 rc = crypto_shash_update(shash, iov[0].iov_base,
552 iov[0].iov_len);
553 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000554 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300555 __func__);
556 return rc;
557 }
558 drqst.rq_iov++;
559 drqst.rq_nvec--;
560 }
Steve French429b46f2013-06-26 23:45:05 -0500561
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300562 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500563 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700564 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500565
566 return rc;
Steve French38107d42012-12-08 22:08:06 -0600567}
568
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700569/* must be called with server->srv_mutex held */
570static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700571smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700572{
573 int rc = 0;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200574 struct smb2_sync_hdr *shdr;
575 struct smb2_sess_setup_req *ssr;
576 bool is_binding;
577 bool is_signed;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700578
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200579 shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
580 ssr = (struct smb2_sess_setup_req *)shdr;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700581
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200582 is_binding = shdr->Command == SMB2_SESSION_SETUP &&
583 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
584 is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
585
586 if (!is_signed)
587 return 0;
588 if (server->tcpStatus == CifsNeedNegotiate)
589 return 0;
590 if (!is_binding && !server->session_estab) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700591 strncpy(shdr->Signature, "BSRSPYL", 8);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200592 return 0;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700593 }
594
Steve French38107d42012-12-08 22:08:06 -0600595 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700596
597 return rc;
598}
599
600int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700601smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700602{
603 unsigned int rc;
604 char server_response_sig[16];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800605 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000606 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700607
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700608 if ((shdr->Command == SMB2_NEGOTIATE) ||
609 (shdr->Command == SMB2_SESSION_SETUP) ||
610 (shdr->Command == SMB2_OPLOCK_BREAK) ||
Steve French4f5c10f2019-09-03 21:18:49 -0500611 server->ignore_signature ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700612 (!server->session_estab))
613 return 0;
614
615 /*
616 * BB what if signatures are supposed to be on for session but
617 * server does not send one? BB
618 */
619
620 /* Do not need to verify session setups with signature "BSRSPYL " */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700621 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700623 shdr->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700624
625 /*
626 * Save off the origiginal signature so we can modify the smb and check
627 * our calculated signature against what the server sent.
628 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700629 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700630
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700631 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700632
633 mutex_lock(&server->srv_mutex);
Steve French38107d42012-12-08 22:08:06 -0600634 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700635 mutex_unlock(&server->srv_mutex);
636
637 if (rc)
638 return rc;
639
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700640 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700641 return -EACCES;
642 else
643 return 0;
644}
645
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400646/*
647 * Set message id for the request. Should be called after wait_for_free_request
648 * and when srv_mutex is held.
649 */
650static inline void
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700651smb2_seq_num_into_buf(struct TCP_Server_Info *server,
652 struct smb2_sync_hdr *shdr)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400653{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700654 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400655
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700656 shdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400657 /* skip message numbers according to CreditCharge field */
658 for (i = 1; i < num; i++)
659 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400660}
661
662static struct mid_q_entry *
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700663smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400664 struct TCP_Server_Info *server)
665{
666 struct mid_q_entry *temp;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800667 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400668
669 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500670 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400671 return NULL;
672 }
673
674 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +1000675 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +0200676 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +1000677 temp->mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800678 temp->credits = credits > 0 ? credits : 1;
NeilBrowna6f74e82017-04-10 12:08:53 +1000679 temp->pid = current->pid;
680 temp->command = shdr->Command; /* Always LE */
681 temp->when_alloc = jiffies;
682 temp->server = server;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400683
NeilBrowna6f74e82017-04-10 12:08:53 +1000684 /*
685 * The default is for the mid to be synchronous, so the
686 * default callback just wakes up the current task.
687 */
688 temp->callback = cifs_wake_up_task;
689 temp->callback_data = current;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400690
691 atomic_inc(&midCount);
692 temp->mid_state = MID_REQUEST_ALLOCATED;
Steve French53a3e0d2019-02-26 21:26:20 -0600693 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
694 le16_to_cpu(shdr->Command), temp->mid);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400695 return temp;
696}
697
698static int
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200699smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
700 struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400701{
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200702 if (server->tcpStatus == CifsExiting)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400703 return -ENOENT;
704
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200705 if (server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500706 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400707 return -EAGAIN;
708 }
709
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200710 if (server->tcpStatus == CifsNeedNegotiate &&
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800711 shdr->Command != SMB2_NEGOTIATE)
712 return -EAGAIN;
713
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500714 if (ses->status == CifsNew) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700715 if ((shdr->Command != SMB2_SESSION_SETUP) &&
716 (shdr->Command != SMB2_NEGOTIATE))
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400717 return -EAGAIN;
718 /* else ok - we are setting up session */
719 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500720
721 if (ses->status == CifsExiting) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700722 if (shdr->Command != SMB2_LOGOFF)
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500723 return -EAGAIN;
724 /* else ok - we are shutting down the session */
725 }
726
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200727 *mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400728 if (*mid == NULL)
729 return -ENOMEM;
730 spin_lock(&GlobalMid_Lock);
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200731 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400732 spin_unlock(&GlobalMid_Lock);
Steve French53a3e0d2019-02-26 21:26:20 -0600733
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400734 return 0;
735}
736
737int
738smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
739 bool log_error)
740{
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000741 unsigned int len = mid->resp_buf_size;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000742 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800743 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000744 .rq_nvec = 1 };
Jeff Layton0b688cf2012-09-18 16:20:34 -0700745
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800746 iov[0].iov_base = (char *)mid->resp_buf;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000747 iov[0].iov_len = len;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400748
749 dump_smb(mid->resp_buf, min_t(u32, 80, len));
750 /* convert the length into a more usable form */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800751 if (len > 24 && server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700752 int rc;
753
Jeff Layton0b688cf2012-09-18 16:20:34 -0700754 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700755 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000756 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500757 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700758 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400759
760 return map_smb2_to_linux_error(mid->resp_buf, log_error);
761}
762
Jeff Laytonfec344e2012-09-18 16:20:35 -0700763struct mid_q_entry *
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200764smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
765 struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400766{
767 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800768 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000769 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400770 struct mid_q_entry *mid;
771
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200772 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400773
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200774 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800775 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200776 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700777 return ERR_PTR(rc);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800778 }
779
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200780 rc = smb2_sign_rqst(rqst, server);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700781 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200782 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700783 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700784 return ERR_PTR(rc);
785 }
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800786
Jeff Laytonfec344e2012-09-18 16:20:35 -0700787 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400788}
789
Jeff Laytonfec344e2012-09-18 16:20:35 -0700790struct mid_q_entry *
791smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400792{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700793 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 Shilovskyc95b8ee2012-07-11 14:45:28 +0400796 struct mid_q_entry *mid;
797
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800798 if (server->tcpStatus == CifsNeedNegotiate &&
799 shdr->Command != SMB2_NEGOTIATE)
800 return ERR_PTR(-EAGAIN);
801
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700802 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400803
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700804 mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800805 if (mid == NULL) {
806 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700807 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800808 }
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400809
Jeff Laytonfec344e2012-09-18 16:20:35 -0700810 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400811 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800812 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400813 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700814 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700815 }
816
Jeff Laytonfec344e2012-09-18 16:20:35 -0700817 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400818}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700819
820int
821smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
822{
823 struct crypto_aead *tfm;
824
825 if (!server->secmech.ccmaesencrypt) {
Steve French2b2f7542019-06-07 15:16:10 -0500826 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
827 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
828 else
829 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700830 if (IS_ERR(tfm)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000831 cifs_server_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700832 __func__);
833 return PTR_ERR(tfm);
834 }
835 server->secmech.ccmaesencrypt = tfm;
836 }
837
838 if (!server->secmech.ccmaesdecrypt) {
Steve French2b2f7542019-06-07 15:16:10 -0500839 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
840 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
841 else
842 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700843 if (IS_ERR(tfm)) {
844 crypto_free_aead(server->secmech.ccmaesencrypt);
845 server->secmech.ccmaesencrypt = NULL;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000846 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700847 __func__);
848 return PTR_ERR(tfm);
849 }
850 server->secmech.ccmaesdecrypt = tfm;
851 }
852
853 return 0;
854}