blob: 709f2296bdb4930b2eee0dcb7dd341f94ca42e9b [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{
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050048 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Steve French9fbc5902010-08-20 20:42:26 +000050 if (cifs_pdu == NULL || server == NULL || signature == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return -EINVAL;
52
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050053 if (!server->ntlmssp.sdescmd5) {
54 cERROR(1,
55 "cifs_calculate_signature: can't generate signature\n");
56 return -1;
57 }
Steve Frenchb609f062007-07-09 07:55:14 +000058
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050059 rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
Steve French9fbc5902010-08-20 20:42:26 +000060 if (rc) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050061 cERROR(1, "cifs_calculate_signature: oould not init md5\n");
Steve French9fbc5902010-08-20 20:42:26 +000062 return rc;
63 }
64
65 if (server->secType == RawNTLMSSP)
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050066 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +000067 server->session_key.data.ntlmv2.key,
68 CIFS_NTLMV2_SESSKEY_SIZE);
69 else
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050070 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +000071 (char *)&server->session_key.data,
72 server->session_key.len);
73
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050074 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +000075 cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
76
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050077 rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
Steve French9fbc5902010-08-20 20:42:26 +000078
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -050079 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Steve French9fbc5902010-08-20 20:42:26 +000082
Steve Frenchffdd6e42007-06-24 21:15:44 +000083int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
84 __u32 *pexpected_response_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int rc = 0;
87 char smb_signature[20];
88
Steve Frenchffdd6e42007-06-24 21:15:44 +000089 if ((cifs_pdu == NULL) || (server == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EINVAL;
91
Steve Frenchffdd6e42007-06-24 21:15:44 +000092 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return rc;
94
95 spin_lock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +000096 cifs_pdu->Signature.Sequence.SequenceNumber =
97 cpu_to_le32(server->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French50c2f752007-07-13 00:33:32 +000099
Steve Frenchad009ac2005-04-28 22:41:05 -0700100 *pexpected_response_sequence_number = server->sequence_number++;
101 server->sequence_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_unlock(&GlobalMid_Lock);
103
Steve French9fbc5902010-08-20 20:42:26 +0000104 rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000105 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
107 else
108 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
109
110 return rc;
111}
112
Steve Frenchffdd6e42007-06-24 21:15:44 +0000113static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
Steve French9fbc5902010-08-20 20:42:26 +0000114 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -0800115{
Steve Frenche9917a02006-03-31 21:22:00 +0000116 int i;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500117 int rc;
Steve French84afc292005-12-02 13:32:45 -0800118
Steve French9fbc5902010-08-20 20:42:26 +0000119 if (iov == NULL || server == NULL || signature == NULL)
Steve Frenche9917a02006-03-31 21:22:00 +0000120 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -0800121
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500122 if (!server->ntlmssp.sdescmd5) {
123 cERROR(1, "cifs_calc_signature2: can't generate signature\n");
124 return -1;
125 }
Steve French9fbc5902010-08-20 20:42:26 +0000126
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500127 rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
Steve French9fbc5902010-08-20 20:42:26 +0000128 if (rc) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500129 cERROR(1, "cifs_calc_signature2: oould not init md5\n");
Steve French9fbc5902010-08-20 20:42:26 +0000130 return rc;
131 }
132
133 if (server->secType == RawNTLMSSP)
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500134 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000135 server->session_key.data.ntlmv2.key,
136 CIFS_NTLMV2_SESSKEY_SIZE);
137 else
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500138 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000139 (char *)&server->session_key.data,
140 server->session_key.len);
141
Steve French50c2f752007-07-13 00:33:32 +0000142 for (i = 0; i < n_vec; i++) {
Jeff Layton745542e2007-11-03 04:34:04 +0000143 if (iov[i].iov_len == 0)
144 continue;
Steve Frenchffdd6e42007-06-24 21:15:44 +0000145 if (iov[i].iov_base == NULL) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500146 cERROR(1, "cifs_calc_signature2: null iovec entry");
Steve Frenche9917a02006-03-31 21:22:00 +0000147 return -EIO;
Jeff Layton745542e2007-11-03 04:34:04 +0000148 }
Steve Frenchffdd6e42007-06-24 21:15:44 +0000149 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +0000150 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +0000151 if (i == 0) {
Steve French63d25832007-11-05 21:46:10 +0000152 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
Steve Frenche9917a02006-03-31 21:22:00 +0000153 break; /* nothing to sign or corrupt header */
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500154 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000155 iov[i].iov_base + 4, iov[i].iov_len - 4);
Steve Frenche9917a02006-03-31 21:22:00 +0000156 } else
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500157 crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000158 iov[i].iov_base, iov[i].iov_len);
Steve Frenche9917a02006-03-31 21:22:00 +0000159 }
Steve French84afc292005-12-02 13:32:45 -0800160
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500161 rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
Steve French84afc292005-12-02 13:32:45 -0800162
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500163 return rc;
Steve French84afc292005-12-02 13:32:45 -0800164}
165
Steve Frenchffdd6e42007-06-24 21:15:44 +0000166int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000167 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800168{
169 int rc = 0;
170 char smb_signature[20];
Steve Frenchffdd6e42007-06-24 21:15:44 +0000171 struct smb_hdr *cifs_pdu = iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800172
Steve Frenchffdd6e42007-06-24 21:15:44 +0000173 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800174 return -EINVAL;
175
Steve Frenchffdd6e42007-06-24 21:15:44 +0000176 if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
Steve French84afc292005-12-02 13:32:45 -0800177 return rc;
178
Steve Frenchffdd6e42007-06-24 21:15:44 +0000179 spin_lock(&GlobalMid_Lock);
180 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800181 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000182 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800183
Steve Frenchffdd6e42007-06-24 21:15:44 +0000184 *pexpected_response_sequence_number = server->sequence_number++;
185 server->sequence_number++;
186 spin_unlock(&GlobalMid_Lock);
Steve French84afc292005-12-02 13:32:45 -0800187
Steve French9fbc5902010-08-20 20:42:26 +0000188 rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000189 if (rc)
190 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
191 else
192 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800193
Steve Frenchffdd6e42007-06-24 21:15:44 +0000194 return rc;
Steve French84afc292005-12-02 13:32:45 -0800195}
196
Steve Frenchb609f062007-07-09 07:55:14 +0000197int cifs_verify_signature(struct smb_hdr *cifs_pdu,
Steve French9fbc5902010-08-20 20:42:26 +0000198 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000199 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Steve French9fbc5902010-08-20 20:42:26 +0000201 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 char server_response_sig[8];
203 char what_we_think_sig_should_be[20];
204
Steve French9fbc5902010-08-20 20:42:26 +0000205 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -EINVAL;
207
208 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
209 return 0;
210
211 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000212 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000213 (struct smb_com_lock_req *)cifs_pdu;
214 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216 }
217
Steve French50c2f752007-07-13 00:33:32 +0000218 /* BB what if signatures are supposed to be on for session but
219 server does not send one? BB */
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000222 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000223 cFYI(1, "dummy signature received for smb command 0x%x",
224 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* save off the origiginal signature so we can modify the smb and check
227 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000228 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Steve French50c2f752007-07-13 00:33:32 +0000230 cifs_pdu->Signature.Sequence.SequenceNumber =
231 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cifs_pdu->Signature.Sequence.Reserved = 0;
233
Steve French9fbc5902010-08-20 20:42:26 +0000234 rc = cifs_calculate_signature(cifs_pdu, server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 what_we_think_sig_should_be);
236
Steve French50c2f752007-07-13 00:33:32 +0000237 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return rc;
239
Steve French50c2f752007-07-13 00:33:32 +0000240/* cifs_dump_mem("what we think it should be: ",
241 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Steve French50c2f752007-07-13 00:33:32 +0000243 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EACCES;
245 else
246 return 0;
247
248}
249
250/* We fill in key by putting in 40 byte array which was allocated by caller */
Steve French9fbc5902010-08-20 20:42:26 +0000251int cifs_calculate_session_key(struct session_key *key, const char *rn,
Steve Frenchb609f062007-07-09 07:55:14 +0000252 const char *password)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 char temp_key[16];
255 if ((key == NULL) || (rn == NULL))
256 return -EINVAL;
257
258 E_md4hash(password, temp_key);
Steve Frenchb609f062007-07-09 07:55:14 +0000259 mdfour(key->data.ntlm, temp_key, 16);
260 memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
261 key->len = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
Steve French7c7b25b2006-06-01 19:20:10 +0000265#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500266void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
267 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000268{
269 int i;
270 char password_with_pad[CIFS_ENCPWD_SIZE];
271
272 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500273 if (password)
274 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000275
Jeff Layton04912d62010-04-24 07:57:45 -0400276 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500277 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
278 memcpy(lnm_session_key, password_with_pad,
279 CIFS_ENCPWD_SIZE);
280 return;
281 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000282
Steve French7c7b25b2006-06-01 19:20:10 +0000283 /* calculate old style session key */
284 /* calling toupper is less broken than repeatedly
285 calling nls_toupper would be since that will never
286 work for UTF8, but neither handles multibyte code pages
287 but the only alternative would be converting to UCS-16 (Unicode)
288 (using a routine something like UniStrupr) then
289 uppercasing and then converting back from Unicode - which
290 would only worth doing it if we knew it were utf8. Basically
291 utf8 and other multibyte codepages each need their own strupper
292 function since a byte at a time will ont work. */
293
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000294 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000295 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000296
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500297 SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
298
Steve French7c7b25b2006-06-01 19:20:10 +0000299 /* clear password before we return/free memory */
300 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
301}
302#endif /* CIFS_WEAK_PW_HASH */
303
Steve French50c2f752007-07-13 00:33:32 +0000304static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
305 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000306{
307 int rc = 0;
308 int len;
Steve French9fbc5902010-08-20 20:42:26 +0000309 char nt_hash[CIFS_NTHASH_SIZE];
Steve French50c2f752007-07-13 00:33:32 +0000310 wchar_t *user;
311 wchar_t *domain;
Steve French9fbc5902010-08-20 20:42:26 +0000312 wchar_t *server;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500313
314 if (!ses->server->ntlmssp.sdeschmacmd5) {
315 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
316 return -1;
317 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000318
319 /* calculate md4 hash of password */
320 E_md4hash(ses->password, nt_hash);
321
Steve French9fbc5902010-08-20 20:42:26 +0000322 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash,
323 CIFS_NTHASH_SIZE);
324
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500325 rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
Steve French9fbc5902010-08-20 20:42:26 +0000326 if (rc) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500327 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
Steve French9fbc5902010-08-20 20:42:26 +0000328 return rc;
329 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000330
331 /* convert ses->userName to unicode and uppercase */
Steve French1717ffc2006-06-08 05:41:32 +0000332 len = strlen(ses->userName);
333 user = kmalloc(2 + (len * 2), GFP_KERNEL);
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500334 if (user == NULL) {
335 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
336 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000337 goto calc_exit_2;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500338 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000339 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
Steve French1717ffc2006-06-08 05:41:32 +0000340 UniStrupr(user);
Steve French9fbc5902010-08-20 20:42:26 +0000341
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500342 crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
343 (char *)user, 2 * len);
Steve Frencha8ee0342006-06-05 23:34:19 +0000344
345 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000346 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000347 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000348
Steve French50c2f752007-07-13 00:33:32 +0000349 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500350 if (domain == NULL) {
351 cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
352 rc = -ENOMEM;
Steve French1717ffc2006-06-08 05:41:32 +0000353 goto calc_exit_1;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500354 }
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000355 len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
356 nls_cp);
Steve Frenchb609f062007-07-09 07:55:14 +0000357 /* the following line was removed since it didn't work well
358 with lower cased domain name that passed as an option.
359 Maybe converting the domain name earlier makes sense */
360 /* UniStrupr(domain); */
Steve Frencha8ee0342006-06-05 23:34:19 +0000361
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500362 crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
363 (char *)domain, 2 * len);
Steve French50c2f752007-07-13 00:33:32 +0000364
Steve French1717ffc2006-06-08 05:41:32 +0000365 kfree(domain);
Steve French9fbc5902010-08-20 20:42:26 +0000366 } else if (ses->serverName) {
367 len = strlen(ses->serverName);
368
369 server = kmalloc(2 + (len * 2), GFP_KERNEL);
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500370 if (server == NULL) {
371 cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
372 rc = -ENOMEM;
Steve French9fbc5902010-08-20 20:42:26 +0000373 goto calc_exit_1;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500374 }
Steve French9fbc5902010-08-20 20:42:26 +0000375 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
376 nls_cp);
377 /* the following line was removed since it didn't work well
378 with lower cased domain name that passed as an option.
379 Maybe converting the domain name earlier makes sense */
380 /* UniStrupr(domain); */
381
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500382 crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
383 (char *)server, 2 * len);
Steve French9fbc5902010-08-20 20:42:26 +0000384
385 kfree(server);
Steve French1717ffc2006-06-08 05:41:32 +0000386 }
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500387
388 rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
389 ses->server->ntlmv2_hash);
390
Steve French1717ffc2006-06-08 05:41:32 +0000391calc_exit_1:
392 kfree(user);
393calc_exit_2:
Steve French50c2f752007-07-13 00:33:32 +0000394 /* BB FIXME what about bytes 24 through 40 of the signing key?
Steve French1717ffc2006-06-08 05:41:32 +0000395 compare with the NTLM example */
Steve Frencha8ee0342006-06-05 23:34:19 +0000396
397 return rc;
398}
399
Steve French9fbc5902010-08-20 20:42:26 +0000400static int
401find_domain_name(struct cifsSesInfo *ses)
402{
403 int rc = 0;
404 unsigned int attrsize;
405 unsigned int type;
406 unsigned char *blobptr;
407 struct ntlmssp2_name *attrptr;
408
409 if (ses->server->tiblob) {
410 blobptr = ses->server->tiblob;
411 attrptr = (struct ntlmssp2_name *) blobptr;
412
413 while ((type = attrptr->type) != 0) {
414 blobptr += 2; /* advance attr type */
415 attrsize = attrptr->length;
416 blobptr += 2; /* advance attr size */
417 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
418 if (!ses->domainName) {
419 ses->domainName =
420 kmalloc(attrptr->length + 1,
421 GFP_KERNEL);
422 if (!ses->domainName)
423 return -ENOMEM;
424 cifs_from_ucs2(ses->domainName,
425 (__le16 *)blobptr,
426 attrptr->length,
427 attrptr->length,
428 load_nls_default(), false);
429 }
430 }
431 blobptr += attrsize; /* advance attr value */
432 attrptr = (struct ntlmssp2_name *) blobptr;
433 }
434 } else {
435 ses->server->tilen = 2 * sizeof(struct ntlmssp2_name);
436 ses->server->tiblob = kmalloc(ses->server->tilen, GFP_KERNEL);
437 if (!ses->server->tiblob) {
438 ses->server->tilen = 0;
439 cERROR(1, "Challenge target info allocation failure");
440 return -ENOMEM;
441 }
442 memset(ses->server->tiblob, 0x0, ses->server->tilen);
443 attrptr = (struct ntlmssp2_name *) ses->server->tiblob;
444 attrptr->type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
445 }
446
447 return rc;
448}
449
450static int
451CalcNTLMv2_response(const struct TCP_Server_Info *server,
452 char *v2_session_response)
Steve French6d027cf2006-06-05 16:26:05 +0000453{
Steve Frencha8ee0342006-06-05 23:34:19 +0000454 int rc;
Steve French9fbc5902010-08-20 20:42:26 +0000455
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500456 if (!server->ntlmssp.sdeschmacmd5) {
457 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
458 return -1;
459 }
Steve French9fbc5902010-08-20 20:42:26 +0000460
461 crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash,
462 CIFS_HMAC_MD5_HASH_SIZE);
463
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500464 rc = crypto_shash_init(&server->ntlmssp.sdeschmacmd5->shash);
Steve French9fbc5902010-08-20 20:42:26 +0000465 if (rc) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500466 cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
Steve French9fbc5902010-08-20 20:42:26 +0000467 return rc;
468 }
469
470 memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
471 server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500472 crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000473 v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
474 sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE);
475
476 if (server->tilen)
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500477 crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000478 server->tiblob, server->tilen);
479
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500480 rc = crypto_shash_final(&server->ntlmssp.sdeschmacmd5->shash,
481 v2_session_response);
Steve French9fbc5902010-08-20 20:42:26 +0000482
483 return rc;
484}
485
486int
487setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
488 const struct nls_table *nls_cp)
489{
490 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +0000491 struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
Steve French6d027cf2006-06-05 16:26:05 +0000492
493 buf->blob_signature = cpu_to_le32(0x00000101);
494 buf->reserved = 0;
495 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
Steve French1717ffc2006-06-08 05:41:32 +0000496 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
Steve French6d027cf2006-06-05 16:26:05 +0000497 buf->reserved2 = 0;
Steve French9fbc5902010-08-20 20:42:26 +0000498
499 if (!ses->domainName) {
500 rc = find_domain_name(ses);
501 if (rc) {
502 cERROR(1, "could not get domain/server name rc %d", rc);
503 return rc;
504 }
505 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000506
Steve French6d027cf2006-06-05 16:26:05 +0000507 /* calculate buf->ntlmv2_hash */
Steve French1717ffc2006-06-08 05:41:32 +0000508 rc = calc_ntlmv2_hash(ses, nls_cp);
Steve French9fbc5902010-08-20 20:42:26 +0000509 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000510 cERROR(1, "could not get v2 hash rc %d", rc);
Steve French9fbc5902010-08-20 20:42:26 +0000511 return rc;
512 }
513 rc = CalcNTLMv2_response(ses->server, resp_buf);
514 if (rc) {
515 cERROR(1, "could not get v2 hash rc %d", rc);
516 return rc;
517 }
Steve Frenchb609f062007-07-09 07:55:14 +0000518
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500519 if (!ses->server->ntlmssp.sdeschmacmd5) {
520 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
521 return -1;
522 }
523
Steve French9fbc5902010-08-20 20:42:26 +0000524 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5,
525 ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Steve Frenchb609f062007-07-09 07:55:14 +0000526
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500527 rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
Steve French9fbc5902010-08-20 20:42:26 +0000528 if (rc) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500529 cERROR(1, "setup_ntlmv2_rsp: could not init hmacmd5\n");
Steve French9fbc5902010-08-20 20:42:26 +0000530 return rc;
531 }
532
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500533 crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
534 resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
Steve French9fbc5902010-08-20 20:42:26 +0000535
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500536 rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
Steve French9fbc5902010-08-20 20:42:26 +0000537 ses->server->session_key.data.ntlmv2.key);
538
539 memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
540 sizeof(struct ntlmv2_resp));
541 ses->server->session_key.len = 16 + sizeof(struct ntlmv2_resp);
542
543 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000544}
545
Steve French9fbc5902010-08-20 20:42:26 +0000546int
547calc_seckey(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Steve French9fbc5902010-08-20 20:42:26 +0000549 int rc;
550 unsigned char sec_key[CIFS_NTLMV2_SESSKEY_SIZE];
551 struct crypto_blkcipher *tfm_arc4;
552 struct scatterlist sgin, sgout;
553 struct blkcipher_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Steve French9fbc5902010-08-20 20:42:26 +0000555 get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Steve French9fbc5902010-08-20 20:42:26 +0000557 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
558 0, CRYPTO_ALG_ASYNC);
559 if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
560 cERROR(1, "could not allocate " "master crypto API arc4\n");
561 return 1;
562 }
563
Shirish Pargaonkar3ec6bbc2010-08-23 11:04:07 -0500564 desc.tfm = tfm_arc4;
565
Steve French9fbc5902010-08-20 20:42:26 +0000566 crypto_blkcipher_setkey(tfm_arc4,
567 server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
568 sg_init_one(&sgin, sec_key, CIFS_CPHTXT_SIZE);
569 sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
570 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
571
572 if (!rc)
573 memcpy(server->session_key.data.ntlmv2.key,
574 sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
575
576 crypto_free_blkcipher(tfm_arc4);
577
578 return 0;
579}
580
581void
582cifs_crypto_shash_release(struct TCP_Server_Info *server)
583{
584 if (server->ntlmssp.md5)
585 crypto_free_shash(server->ntlmssp.md5);
586
587 if (server->ntlmssp.hmacmd5)
588 crypto_free_shash(server->ntlmssp.hmacmd5);
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500589
590 kfree(server->ntlmssp.sdeschmacmd5);
591
592 kfree(server->ntlmssp.sdescmd5);
Steve French9fbc5902010-08-20 20:42:26 +0000593}
594
595int
596cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
597{
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500598 int rc;
599 unsigned int size;
600
Steve French9fbc5902010-08-20 20:42:26 +0000601 server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
602 if (!server->ntlmssp.hmacmd5 ||
603 IS_ERR(server->ntlmssp.hmacmd5)) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500604 cERROR(1, "could not allocate crypto hmacmd5\n");
Steve French9fbc5902010-08-20 20:42:26 +0000605 return 1;
606 }
607
608 server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0);
609 if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) {
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500610 cERROR(1, "could not allocate crypto md5\n");
611 rc = 1;
612 goto cifs_crypto_shash_allocate_ret1;
Steve French9fbc5902010-08-20 20:42:26 +0000613 }
614
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500615 size = sizeof(struct shash_desc) +
616 crypto_shash_descsize(server->ntlmssp.hmacmd5);
617 server->ntlmssp.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
618 if (!server->ntlmssp.sdeschmacmd5) {
619 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
620 rc = -ENOMEM;
621 goto cifs_crypto_shash_allocate_ret2;
622 }
623 server->ntlmssp.sdeschmacmd5->shash.tfm = server->ntlmssp.hmacmd5;
624 server->ntlmssp.sdeschmacmd5->shash.flags = 0x0;
625
626
627 size = sizeof(struct shash_desc) +
628 crypto_shash_descsize(server->ntlmssp.md5);
629 server->ntlmssp.sdescmd5 = kmalloc(size, GFP_KERNEL);
630 if (!server->ntlmssp.sdescmd5) {
631 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
632 rc = -ENOMEM;
633 goto cifs_crypto_shash_allocate_ret3;
634 }
635 server->ntlmssp.sdescmd5->shash.tfm = server->ntlmssp.md5;
636 server->ntlmssp.sdescmd5->shash.flags = 0x0;
637
Steve French9fbc5902010-08-20 20:42:26 +0000638 return 0;
shirishpargaonkar@gmail.com2d20ca82010-08-24 11:53:48 -0500639
640cifs_crypto_shash_allocate_ret3:
641 kfree(server->ntlmssp.sdeschmacmd5);
642
643cifs_crypto_shash_allocate_ret2:
644 crypto_free_shash(server->ntlmssp.md5);
645
646cifs_crypto_shash_allocate_ret1:
647 crypto_free_shash(server->ntlmssp.hmacmd5);
648
649 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}