blob: 051d00011ca3f3202f96d9273302c4a7a4a7b088 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsencrypt.c
3 *
Steve French12b3b8f2006-02-09 21:12:47 +00004 * Copyright (C) International Business Machines Corp., 2005,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "cifspdu.h"
Steve Frenchffdd6e42007-06-24 21:15:44 +000025#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifs_debug.h"
27#include "md5.h"
28#include "cifs_unicode.h"
29#include "cifsproto.h"
Steve French9fbc5902010-08-20 20:42:26 +000030#include "ntlmssp.h"
Steve French7c7b25b2006-06-01 19:20:10 +000031#include <linux/ctype.h>
Steve French6d027cf2006-06-05 16:26:05 +000032#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Steve Frenchffdd6e42007-06-24 21:15:44 +000034/* Calculate and return the CIFS signature based on the mac key and SMB PDU */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* the 16 byte signature must be allocated by the caller */
36/* Note we only use the 1st eight bytes */
Steve Frenchffdd6e42007-06-24 21:15:44 +000037/* Note that the smb header signature field on input contains the
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 sequence number before this function is called */
39
40extern void mdfour(unsigned char *out, unsigned char *in, int n);
41extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
Jeff Layton4e53a3f2008-12-05 20:41:21 -050042extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
Steve Frenchffdd6e42007-06-24 21:15:44 +000043 unsigned char *p24);
Steve French50c2f752007-07-13 00:33:32 +000044
Steve Frenchffdd6e42007-06-24 21:15:44 +000045static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
Steve French9fbc5902010-08-20 20:42:26 +000046 struct TCP_Server_Info *server, char *signature)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Steve French9fbc5902010-08-20 20:42:26 +000048 int rc = 0;
49 struct {
50 struct shash_desc shash;
51 char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
52 } sdesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Steve French9fbc5902010-08-20 20:42:26 +000054 if (cifs_pdu == NULL || server == NULL || signature == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -EINVAL;
56
Steve French9fbc5902010-08-20 20:42:26 +000057 sdesc.shash.tfm = server->ntlmssp.md5;
58 sdesc.shash.flags = 0x0;
Steve Frenchb609f062007-07-09 07:55:14 +000059
Steve French9fbc5902010-08-20 20:42:26 +000060 rc = crypto_shash_init(&sdesc.shash);
61 if (rc) {
62 cERROR(1, "could not initialize master crypto API hmacmd5\n");
63 return rc;
64 }
65
66 if (server->secType == RawNTLMSSP)
67 crypto_shash_update(&sdesc.shash,
68 server->session_key.data.ntlmv2.key,
69 CIFS_NTLMV2_SESSKEY_SIZE);
70 else
71 crypto_shash_update(&sdesc.shash,
72 (char *)&server->session_key.data,
73 server->session_key.len);
74
75 crypto_shash_update(&sdesc.shash,
76 cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
77
78 rc = crypto_shash_final(&sdesc.shash, signature);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81}
82
Steve French9fbc5902010-08-20 20:42:26 +000083
Steve Frenchffdd6e42007-06-24 21:15:44 +000084int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
85 __u32 *pexpected_response_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int rc = 0;
88 char smb_signature[20];
89
Steve Frenchffdd6e42007-06-24 21:15:44 +000090 if ((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EINVAL;
92
Steve Frenchffdd6e42007-06-24 21:15:44 +000093 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return rc;
95
96 spin_lock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +000097 cifs_pdu->Signature.Sequence.SequenceNumber =
98 cpu_to_le32(server->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French50c2f752007-07-13 00:33:32 +0000100
Steve Frenchad009ac2005-04-28 22:41:05 -0700101 *pexpected_response_sequence_number = server->sequence_number++;
102 server->sequence_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_unlock(&GlobalMid_Lock);
104
Steve French9fbc5902010-08-20 20:42:26 +0000105 rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000106 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
108 else
109 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
110
111 return rc;
112}
113
Steve Frenchffdd6e42007-06-24 21:15:44 +0000114static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
Steve French9fbc5902010-08-20 20:42:26 +0000115 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800116{
Steve Frenche9917a02006-03-31 21:22:00 +0000117 int i;
Steve French9fbc5902010-08-20 20:42:26 +0000118 int rc = 0;
119 struct {
120 struct shash_desc shash;
121 char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
122 } sdesc;
Steve French84afc292005-12-02 13:32:45 -0800123
Steve French9fbc5902010-08-20 20:42:26 +0000124 if (iov == NULL || server == NULL || signature == NULL)
Steve Frenche9917a02006-03-31 21:22:00 +0000125 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800126
Steve French9fbc5902010-08-20 20:42:26 +0000127 sdesc.shash.tfm = server->ntlmssp.md5;
128 sdesc.shash.flags = 0x0;
129
130 rc = crypto_shash_init(&sdesc.shash);
131 if (rc) {
132 cERROR(1, "could not initialize master crypto API hmacmd5\n");
133 return rc;
134 }
135
136 if (server->secType == RawNTLMSSP)
137 crypto_shash_update(&sdesc.shash,
138 server->session_key.data.ntlmv2.key,
139 CIFS_NTLMV2_SESSKEY_SIZE);
140 else
141 crypto_shash_update(&sdesc.shash,
142 (char *)&server->session_key.data,
143 server->session_key.len);
144
Steve French50c2f752007-07-13 00:33:32 +0000145 for (i = 0; i < n_vec; i++) {
Jeff Layton745542e2007-11-03 04:34:04 +0000146 if (iov[i].iov_len == 0)
147 continue;
Steve Frenchffdd6e42007-06-24 21:15:44 +0000148 if (iov[i].iov_base == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000149 cERROR(1, "null iovec entry");
Steve Frenche9917a02006-03-31 21:22:00 +0000150 return -EIO;
Jeff Layton745542e2007-11-03 04:34:04 +0000151 }
Steve Frenchffdd6e42007-06-24 21:15:44 +0000152 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +0000153 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000154 if (i == 0) {
Steve French63d25832007-11-05 21:46:10 +0000155 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
Steve Frenche9917a02006-03-31 21:22:00 +0000156 break; /* nothing to sign or corrupt header */
Steve French9fbc5902010-08-20 20:42:26 +0000157 crypto_shash_update(&sdesc.shash,
158 iov[i].iov_base + 4, iov[i].iov_len - 4);
Steve Frenche9917a02006-03-31 21:22:00 +0000159 } else
Steve French9fbc5902010-08-20 20:42:26 +0000160 crypto_shash_update(&sdesc.shash,
161 iov[i].iov_base, iov[i].iov_len);
Steve Frenche9917a02006-03-31 21:22:00 +0000162 }
Steve French84afc292005-12-02 13:32:45 -0800163
Steve French9fbc5902010-08-20 20:42:26 +0000164 rc = crypto_shash_final(&sdesc.shash, signature);
Steve French84afc292005-12-02 13:32:45 -0800165
Steve Frenche9917a02006-03-31 21:22:00 +0000166 return 0;
Steve French84afc292005-12-02 13:32:45 -0800167}
168
Steve Frenchffdd6e42007-06-24 21:15:44 +0000169int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000170 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800171{
172 int rc = 0;
173 char smb_signature[20];
Steve Frenchffdd6e42007-06-24 21:15:44 +0000174 struct smb_hdr *cifs_pdu = iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800175
Steve Frenchffdd6e42007-06-24 21:15:44 +0000176 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800177 return -EINVAL;
178
Steve Frenchffdd6e42007-06-24 21:15:44 +0000179 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Steve French84afc292005-12-02 13:32:45 -0800180 return rc;
181
Steve Frenchffdd6e42007-06-24 21:15:44 +0000182 spin_lock(&GlobalMid_Lock);
183 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800184 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000185 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800186
Steve Frenchffdd6e42007-06-24 21:15:44 +0000187 *pexpected_response_sequence_number = server->sequence_number++;
188 server->sequence_number++;
189 spin_unlock(&GlobalMid_Lock);
Steve French84afc292005-12-02 13:32:45 -0800190
Steve French9fbc5902010-08-20 20:42:26 +0000191 rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000192 if (rc)
193 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
194 else
195 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800196
Steve Frenchffdd6e42007-06-24 21:15:44 +0000197 return rc;
Steve French84afc292005-12-02 13:32:45 -0800198}
199
Steve Frenchb609f062007-07-09 07:55:14 +0000200int cifs_verify_signature(struct smb_hdr *cifs_pdu,
Steve French9fbc5902010-08-20 20:42:26 +0000201 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000202 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Steve French9fbc5902010-08-20 20:42:26 +0000204 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 char server_response_sig[8];
206 char what_we_think_sig_should_be[20];
207
Steve French9fbc5902010-08-20 20:42:26 +0000208 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EINVAL;
210
211 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
212 return 0;
213
214 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000215 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000216 (struct smb_com_lock_req *)cifs_pdu;
217 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219 }
220
Steve French50c2f752007-07-13 00:33:32 +0000221 /* BB what if signatures are supposed to be on for session but
222 server does not send one? BB */
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000225 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000226 cFYI(1, "dummy signature received for smb command 0x%x",
227 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* save off the origiginal signature so we can modify the smb and check
230 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000231 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Steve French50c2f752007-07-13 00:33:32 +0000233 cifs_pdu->Signature.Sequence.SequenceNumber =
234 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cifs_pdu->Signature.Sequence.Reserved = 0;
236
Steve French9fbc5902010-08-20 20:42:26 +0000237 rc = cifs_calculate_signature(cifs_pdu, server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 what_we_think_sig_should_be);
239
Steve French50c2f752007-07-13 00:33:32 +0000240 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return rc;
242
Steve French50c2f752007-07-13 00:33:32 +0000243/* cifs_dump_mem("what we think it should be: ",
244 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Steve French50c2f752007-07-13 00:33:32 +0000246 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EACCES;
248 else
249 return 0;
250
251}
252
253/* We fill in key by putting in 40 byte array which was allocated by caller */
Steve French9fbc5902010-08-20 20:42:26 +0000254int cifs_calculate_session_key(struct session_key *key, const char *rn,
Steve Frenchb609f062007-07-09 07:55:14 +0000255 const char *password)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 char temp_key[16];
258 if ((key == NULL) || (rn == NULL))
259 return -EINVAL;
260
261 E_md4hash(password, temp_key);
Steve Frenchb609f062007-07-09 07:55:14 +0000262 mdfour(key->data.ntlm, temp_key, 16);
263 memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
264 key->len = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266}
267
Steve French7c7b25b2006-06-01 19:20:10 +0000268#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500269void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
270 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000271{
272 int i;
273 char password_with_pad[CIFS_ENCPWD_SIZE];
274
275 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500276 if (password)
277 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000278
Jeff Layton04912d62010-04-24 07:57:45 -0400279 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500280 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
281 memcpy(lnm_session_key, password_with_pad,
282 CIFS_ENCPWD_SIZE);
283 return;
284 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000285
Steve French7c7b25b2006-06-01 19:20:10 +0000286 /* calculate old style session key */
287 /* calling toupper is less broken than repeatedly
288 calling nls_toupper would be since that will never
289 work for UTF8, but neither handles multibyte code pages
290 but the only alternative would be converting to UCS-16 (Unicode)
291 (using a routine something like UniStrupr) then
292 uppercasing and then converting back from Unicode - which
293 would only worth doing it if we knew it were utf8. Basically
294 utf8 and other multibyte codepages each need their own strupper
295 function since a byte at a time will ont work. */
296
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000297 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000298 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000299
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500300 SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
301
Steve French7c7b25b2006-06-01 19:20:10 +0000302 /* clear password before we return/free memory */
303 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
304}
305#endif /* CIFS_WEAK_PW_HASH */
306
Steve French50c2f752007-07-13 00:33:32 +0000307static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
308 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000309{
310 int rc = 0;
311 int len;
Steve French9fbc5902010-08-20 20:42:26 +0000312 char nt_hash[CIFS_NTHASH_SIZE];
Steve French50c2f752007-07-13 00:33:32 +0000313 wchar_t *user;
314 wchar_t *domain;
Steve French9fbc5902010-08-20 20:42:26 +0000315 wchar_t *server;
316 struct {
317 struct shash_desc shash;
318 char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
319 } sdesc;
Steve Frencha8ee0342006-06-05 23:34:19 +0000320
321 /* calculate md4 hash of password */
322 E_md4hash(ses->password, nt_hash);
323
Steve French9fbc5902010-08-20 20:42:26 +0000324 sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
325 sdesc.shash.flags = 0x0;
326
327 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash,
328 CIFS_NTHASH_SIZE);
329
330 rc = crypto_shash_init(&sdesc.shash);
331 if (rc) {
332 cERROR(1, "could not initialize master crypto API hmacmd5\n");
333 return rc;
334 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000335
336 /* convert ses->userName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000337 len = strlen(ses->userName);
338 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Steve French50c2f752007-07-13 00:33:32 +0000339 if (user == NULL)
Steve French1717ffc2006-06-08 05:41:32 +0000340 goto calc_exit_2;
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000341 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
Steve French1717ffc2006-06-08 05:41:32 +0000342 UniStrupr(user);
Steve French9fbc5902010-08-20 20:42:26 +0000343
344 crypto_shash_update(&sdesc.shash, (char *)user, 2 * len);
Steve Frencha8ee0342006-06-05 23:34:19 +0000345
346 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000347 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000348 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000349
Steve French50c2f752007-07-13 00:33:32 +0000350 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
351 if (domain == NULL)
Steve French1717ffc2006-06-08 05:41:32 +0000352 goto calc_exit_1;
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000353 len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
354 nls_cp);
Steve Frenchb609f062007-07-09 07:55:14 +0000355 /* the following line was removed since it didn't work well
356 with lower cased domain name that passed as an option.
357 Maybe converting the domain name earlier makes sense */
358 /* UniStrupr(domain); */
Steve Frencha8ee0342006-06-05 23:34:19 +0000359
Steve French9fbc5902010-08-20 20:42:26 +0000360 crypto_shash_update(&sdesc.shash, (char *)domain, 2 * len);
Steve French50c2f752007-07-13 00:33:32 +0000361
Steve French1717ffc2006-06-08 05:41:32 +0000362 kfree(domain);
Steve French9fbc5902010-08-20 20:42:26 +0000363 } else if (ses->serverName) {
364 len = strlen(ses->serverName);
365
366 server = kmalloc(2 + (len * 2), GFP_KERNEL);
367 if (server == NULL)
368 goto calc_exit_1;
369 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
370 nls_cp);
371 /* the following line was removed since it didn't work well
372 with lower cased domain name that passed as an option.
373 Maybe converting the domain name earlier makes sense */
374 /* UniStrupr(domain); */
375
376 crypto_shash_update(&sdesc.shash, (char *)server, 2 * len);
377
378 kfree(server);
Steve French1717ffc2006-06-08 05:41:32 +0000379 }
380calc_exit_1:
381 kfree(user);
382calc_exit_2:
Steve French50c2f752007-07-13 00:33:32 +0000383 /* BB FIXME what about bytes 24 through 40 of the signing key?
Steve French1717ffc2006-06-08 05:41:32 +0000384 compare with the NTLM example */
Steve French9fbc5902010-08-20 20:42:26 +0000385 rc = crypto_shash_final(&sdesc.shash, ses->server->ntlmv2_hash);
Steve Frencha8ee0342006-06-05 23:34:19 +0000386
387 return rc;
388}
389
Steve French9fbc5902010-08-20 20:42:26 +0000390static int
391find_domain_name(struct cifsSesInfo *ses)
392{
393 int rc = 0;
394 unsigned int attrsize;
395 unsigned int type;
396 unsigned char *blobptr;
397 struct ntlmssp2_name *attrptr;
398
399 if (ses->server->tiblob) {
400 blobptr = ses->server->tiblob;
401 attrptr = (struct ntlmssp2_name *) blobptr;
402
403 while ((type = attrptr->type) != 0) {
404 blobptr += 2; /* advance attr type */
405 attrsize = attrptr->length;
406 blobptr += 2; /* advance attr size */
407 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
408 if (!ses->domainName) {
409 ses->domainName =
410 kmalloc(attrptr->length + 1,
411 GFP_KERNEL);
412 if (!ses->domainName)
413 return -ENOMEM;
414 cifs_from_ucs2(ses->domainName,
415 (__le16 *)blobptr,
416 attrptr->length,
417 attrptr->length,
418 load_nls_default(), false);
419 }
420 }
421 blobptr += attrsize; /* advance attr value */
422 attrptr = (struct ntlmssp2_name *) blobptr;
423 }
424 } else {
425 ses->server->tilen = 2 * sizeof(struct ntlmssp2_name);
426 ses->server->tiblob = kmalloc(ses->server->tilen, GFP_KERNEL);
427 if (!ses->server->tiblob) {
428 ses->server->tilen = 0;
429 cERROR(1, "Challenge target info allocation failure");
430 return -ENOMEM;
431 }
432 memset(ses->server->tiblob, 0x0, ses->server->tilen);
433 attrptr = (struct ntlmssp2_name *) ses->server->tiblob;
434 attrptr->type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
435 }
436
437 return rc;
438}
439
440static int
441CalcNTLMv2_response(const struct TCP_Server_Info *server,
442 char *v2_session_response)
Steve French6d027cf2006-06-05 16:26:05 +0000443{
Steve Frencha8ee0342006-06-05 23:34:19 +0000444 int rc;
Steve French9fbc5902010-08-20 20:42:26 +0000445 struct {
446 struct shash_desc shash;
447 char ctx[crypto_shash_descsize(server->ntlmssp.hmacmd5)];
448 } sdesc;
449
450 sdesc.shash.tfm = server->ntlmssp.hmacmd5;
451 sdesc.shash.flags = 0x0;
452
453 crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash,
454 CIFS_HMAC_MD5_HASH_SIZE);
455
456 rc = crypto_shash_init(&sdesc.shash);
457 if (rc) {
458 cERROR(1, "could not initialize master crypto API hmacmd5\n");
459 return rc;
460 }
461
462 memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
463 server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
464 crypto_shash_update(&sdesc.shash,
465 v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
466 sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE);
467
468 if (server->tilen)
469 crypto_shash_update(&sdesc.shash,
470 server->tiblob, server->tilen);
471
472 rc = crypto_shash_final(&sdesc.shash, v2_session_response);
473
474 return rc;
475}
476
477int
478setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
479 const struct nls_table *nls_cp)
480{
481 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +0000482 struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
Steve French9fbc5902010-08-20 20:42:26 +0000483 struct {
484 struct shash_desc shash;
485 char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
486 } sdesc;
Steve French6d027cf2006-06-05 16:26:05 +0000487
488 buf->blob_signature = cpu_to_le32(0x00000101);
489 buf->reserved = 0;
490 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
Steve French1717ffc2006-06-08 05:41:32 +0000491 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
Steve French6d027cf2006-06-05 16:26:05 +0000492 buf->reserved2 = 0;
Steve French9fbc5902010-08-20 20:42:26 +0000493
494 if (!ses->domainName) {
495 rc = find_domain_name(ses);
496 if (rc) {
497 cERROR(1, "could not get domain/server name rc %d", rc);
498 return rc;
499 }
500 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000501
Steve French6d027cf2006-06-05 16:26:05 +0000502 /* calculate buf->ntlmv2_hash */
Steve French1717ffc2006-06-08 05:41:32 +0000503 rc = calc_ntlmv2_hash(ses, nls_cp);
Steve French9fbc5902010-08-20 20:42:26 +0000504 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000505 cERROR(1, "could not get v2 hash rc %d", rc);
Steve French9fbc5902010-08-20 20:42:26 +0000506 return rc;
507 }
508 rc = CalcNTLMv2_response(ses->server, resp_buf);
509 if (rc) {
510 cERROR(1, "could not get v2 hash rc %d", rc);
511 return rc;
512 }
Steve Frenchb609f062007-07-09 07:55:14 +0000513
Steve French9fbc5902010-08-20 20:42:26 +0000514 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5,
515 ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Steve Frenchb609f062007-07-09 07:55:14 +0000516
Steve French9fbc5902010-08-20 20:42:26 +0000517 sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
518 sdesc.shash.flags = 0x0;
519
520 rc = crypto_shash_init(&sdesc.shash);
521 if (rc) {
522 cERROR(1, "could not initialize master crypto API hmacmd5\n");
523 return rc;
524 }
525
526 crypto_shash_update(&sdesc.shash, resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
527
528 rc = crypto_shash_final(&sdesc.shash,
529 ses->server->session_key.data.ntlmv2.key);
530
531 memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
532 sizeof(struct ntlmv2_resp));
533 ses->server->session_key.len = 16 + sizeof(struct ntlmv2_resp);
534
535 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000536}
537
Steve French9fbc5902010-08-20 20:42:26 +0000538int
539calc_seckey(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Steve French9fbc5902010-08-20 20:42:26 +0000541 int rc;
542 unsigned char sec_key[CIFS_NTLMV2_SESSKEY_SIZE];
543 struct crypto_blkcipher *tfm_arc4;
544 struct scatterlist sgin, sgout;
545 struct blkcipher_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Steve French9fbc5902010-08-20 20:42:26 +0000547 get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Steve French9fbc5902010-08-20 20:42:26 +0000549 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
550 0, CRYPTO_ALG_ASYNC);
551 if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
552 cERROR(1, "could not allocate " "master crypto API arc4\n");
553 return 1;
554 }
555
556 crypto_blkcipher_setkey(tfm_arc4,
557 server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
558 sg_init_one(&sgin, sec_key, CIFS_CPHTXT_SIZE);
559 sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
560 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
561
562 if (!rc)
563 memcpy(server->session_key.data.ntlmv2.key,
564 sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
565
566 crypto_free_blkcipher(tfm_arc4);
567
568 return 0;
569}
570
571void
572cifs_crypto_shash_release(struct TCP_Server_Info *server)
573{
574 if (server->ntlmssp.md5)
575 crypto_free_shash(server->ntlmssp.md5);
576
577 if (server->ntlmssp.hmacmd5)
578 crypto_free_shash(server->ntlmssp.hmacmd5);
579}
580
581int
582cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
583{
584 server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
585 if (!server->ntlmssp.hmacmd5 ||
586 IS_ERR(server->ntlmssp.hmacmd5)) {
587 cERROR(1, "could not allocate master crypto API hmacmd5\n");
588 return 1;
589 }
590
591 server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0);
592 if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) {
593 crypto_free_shash(server->ntlmssp.hmacmd5);
594 cERROR(1, "could not allocate master crypto API md5\n");
595 return 1;
596 }
597
598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}