Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifsencrypt.c |
| 3 | * |
Steve French | 12b3b8f | 2006-02-09 21:12:47 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2005,2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "cifspdu.h" |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 25 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "cifs_debug.h" |
| 27 | #include "md5.h" |
| 28 | #include "cifs_unicode.h" |
| 29 | #include "cifsproto.h" |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 30 | #include "ntlmssp.h" |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 32 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 34 | /* Calculate and return the CIFS signature based on the mac key and SMB PDU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* the 16 byte signature must be allocated by the caller */ |
| 36 | /* Note we only use the 1st eight bytes */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 37 | /* Note that the smb header signature field on input contains the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | sequence number before this function is called */ |
| 39 | |
| 40 | extern void mdfour(unsigned char *out, unsigned char *in, int n); |
| 41 | extern void E_md4hash(const unsigned char *passwd, unsigned char *p16); |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 42 | extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8, |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 43 | unsigned char *p24); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 44 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 45 | static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 46 | struct TCP_Server_Info *server, char *signature) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 48 | int rc = 0; |
| 49 | struct { |
| 50 | struct shash_desc shash; |
| 51 | char ctx[crypto_shash_descsize(server->ntlmssp.md5)]; |
| 52 | } sdesc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 54 | if (cifs_pdu == NULL || server == NULL || signature == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return -EINVAL; |
| 56 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 57 | sdesc.shash.tfm = server->ntlmssp.md5; |
| 58 | sdesc.shash.flags = 0x0; |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 59 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 60 | rc = crypto_shash_init(&sdesc.shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 61 | if (rc) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 62 | cERROR(1, "could not initialize master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 63 | return rc; |
| 64 | } |
| 65 | |
| 66 | if (server->secType == RawNTLMSSP) |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 67 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 68 | server->session_key.data.ntlmv2.key, |
| 69 | CIFS_NTLMV2_SESSKEY_SIZE); |
| 70 | else |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 71 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 72 | (char *)&server->session_key.data, |
| 73 | server->session_key.len); |
| 74 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 75 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 76 | cifs_pdu->Protocol, cifs_pdu->smb_buf_length); |
| 77 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 78 | rc = crypto_shash_final(&sdesc.shash, signature); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 79 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 80 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 83 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 84 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
| 85 | __u32 *pexpected_response_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | int rc = 0; |
| 88 | char smb_signature[20]; |
| 89 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 90 | if ((cifs_pdu == NULL) || (server == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 93 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return rc; |
| 95 | |
| 96 | spin_lock(&GlobalMid_Lock); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 97 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 98 | cpu_to_le32(server->sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 100 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 101 | *pexpected_response_sequence_number = server->sequence_number++; |
| 102 | server->sequence_number++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | spin_unlock(&GlobalMid_Lock); |
| 104 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 105 | rc = cifs_calculate_signature(cifs_pdu, server, smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 106 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | 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 French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 114 | static int cifs_calc_signature2(const struct kvec *iov, int n_vec, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 115 | struct TCP_Server_Info *server, char *signature) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 116 | { |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 117 | int i; |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 118 | int rc = 0; |
| 119 | struct { |
| 120 | struct shash_desc shash; |
| 121 | char ctx[crypto_shash_descsize(server->ntlmssp.md5)]; |
| 122 | } sdesc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 123 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 124 | if (iov == NULL || server == NULL || signature == NULL) |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 125 | return -EINVAL; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 126 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 127 | sdesc.shash.tfm = server->ntlmssp.md5; |
| 128 | sdesc.shash.flags = 0x0; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 129 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 130 | rc = crypto_shash_init(&sdesc.shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 131 | if (rc) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 132 | cERROR(1, "could not initialize master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | if (server->secType == RawNTLMSSP) |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 137 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 138 | server->session_key.data.ntlmv2.key, |
| 139 | CIFS_NTLMV2_SESSKEY_SIZE); |
| 140 | else |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 141 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 142 | (char *)&server->session_key.data, |
| 143 | server->session_key.len); |
| 144 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 145 | for (i = 0; i < n_vec; i++) { |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 146 | if (iov[i].iov_len == 0) |
| 147 | continue; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 148 | if (iov[i].iov_base == NULL) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 149 | cERROR(1, "null iovec entry"); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 150 | return -EIO; |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 151 | } |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 152 | /* The first entry includes a length field (which does not get |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 153 | signed that occupies the first 4 bytes before the header */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 154 | if (i == 0) { |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 155 | if (iov[0].iov_len <= 8) /* cmd field at offset 9 */ |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 156 | break; /* nothing to sign or corrupt header */ |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 157 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 158 | iov[i].iov_base + 4, iov[i].iov_len - 4); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 159 | } else |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 160 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 161 | iov[i].iov_base, iov[i].iov_len); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 162 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 163 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 164 | rc = crypto_shash_final(&sdesc.shash, signature); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 165 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 166 | return 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 169 | int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 170 | __u32 *pexpected_response_sequence_number) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 171 | { |
| 172 | int rc = 0; |
| 173 | char smb_signature[20]; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 174 | struct smb_hdr *cifs_pdu = iov[0].iov_base; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 175 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 176 | if ((cifs_pdu == NULL) || (server == NULL)) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 179 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 180 | return rc; |
| 181 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 182 | spin_lock(&GlobalMid_Lock); |
| 183 | cifs_pdu->Signature.Sequence.SequenceNumber = |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 184 | cpu_to_le32(server->sequence_number); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 185 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 186 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 187 | *pexpected_response_sequence_number = server->sequence_number++; |
| 188 | server->sequence_number++; |
| 189 | spin_unlock(&GlobalMid_Lock); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 190 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 191 | rc = cifs_calc_signature2(iov, n_vec, server, smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 192 | if (rc) |
| 193 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
| 194 | else |
| 195 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 196 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 197 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 200 | int cifs_verify_signature(struct smb_hdr *cifs_pdu, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 201 | struct TCP_Server_Info *server, |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 202 | __u32 expected_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 204 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | char server_response_sig[8]; |
| 206 | char what_we_think_sig_should_be[20]; |
| 207 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 208 | if (cifs_pdu == NULL || server == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 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 French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 215 | struct smb_com_lock_req *pSMB = |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 216 | (struct smb_com_lock_req *)cifs_pdu; |
| 217 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 221 | /* BB what if signatures are supposed to be on for session but |
| 222 | server does not send one? BB */ |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 225 | if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 226 | cFYI(1, "dummy signature received for smb command 0x%x", |
| 227 | cifs_pdu->Command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | /* save off the origiginal signature so we can modify the smb and check |
| 230 | its signature against what the server sent */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 231 | memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 233 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 234 | cpu_to_le32(expected_sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | cifs_pdu->Signature.Sequence.Reserved = 0; |
| 236 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 237 | rc = cifs_calculate_signature(cifs_pdu, server, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | what_we_think_sig_should_be); |
| 239 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 240 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return rc; |
| 242 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 243 | /* cifs_dump_mem("what we think it should be: ", |
| 244 | what_we_think_sig_should_be, 16); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 246 | if (memcmp(server_response_sig, what_we_think_sig_should_be, 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | 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 French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 254 | int cifs_calculate_session_key(struct session_key *key, const char *rn, |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 255 | const char *password) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
| 257 | char temp_key[16]; |
| 258 | if ((key == NULL) || (rn == NULL)) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | E_md4hash(password, temp_key); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 262 | mdfour(key->data.ntlm, temp_key, 16); |
| 263 | memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE); |
| 264 | key->len = 40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 268 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 269 | void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt, |
| 270 | char *lnm_session_key) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 271 | { |
| 272 | int i; |
| 273 | char password_with_pad[CIFS_ENCPWD_SIZE]; |
| 274 | |
| 275 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 276 | if (password) |
| 277 | strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 278 | |
Jeff Layton | 04912d6 | 2010-04-24 07:57:45 -0400 | [diff] [blame] | 279 | if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) { |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 280 | 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 French | bdc4bf6e | 2006-06-02 22:57:13 +0000 | [diff] [blame] | 285 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 286 | /* 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 Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 297 | for (i = 0; i < CIFS_ENCPWD_SIZE; i++) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 298 | password_with_pad[i] = toupper(password_with_pad[i]); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 299 | |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 300 | SMBencrypt(password_with_pad, cryptkey, lnm_session_key); |
| 301 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 302 | /* 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 French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 307 | static int calc_ntlmv2_hash(struct cifsSesInfo *ses, |
| 308 | const struct nls_table *nls_cp) |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 309 | { |
| 310 | int rc = 0; |
| 311 | int len; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 312 | char nt_hash[CIFS_NTHASH_SIZE]; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 313 | wchar_t *user; |
| 314 | wchar_t *domain; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 315 | wchar_t *server; |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 316 | struct { |
| 317 | struct shash_desc shash; |
| 318 | char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)]; |
| 319 | } sdesc; |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 320 | |
| 321 | /* calculate md4 hash of password */ |
| 322 | E_md4hash(ses->password, nt_hash); |
| 323 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 324 | sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5; |
| 325 | sdesc.shash.flags = 0x0; |
| 326 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 327 | crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash, |
| 328 | CIFS_NTHASH_SIZE); |
| 329 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 330 | rc = crypto_shash_init(&sdesc.shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 331 | if (rc) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 332 | cERROR(1, "could not initialize master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 333 | return rc; |
| 334 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 335 | |
| 336 | /* convert ses->userName to unicode and uppercase */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 337 | len = strlen(ses->userName); |
| 338 | user = kmalloc(2 + (len * 2), GFP_KERNEL); |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 339 | if (user == NULL) |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 340 | goto calc_exit_2; |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 341 | len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 342 | UniStrupr(user); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 343 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 344 | crypto_shash_update(&sdesc.shash, (char *)user, 2 * len); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 345 | |
| 346 | /* convert ses->domainName to unicode and uppercase */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 347 | if (ses->domainName) { |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 348 | len = strlen(ses->domainName); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 349 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 350 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 351 | if (domain == NULL) |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 352 | goto calc_exit_1; |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 353 | len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len, |
| 354 | nls_cp); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 355 | /* 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 French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 359 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 360 | crypto_shash_update(&sdesc.shash, (char *)domain, 2 * len); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 361 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 362 | kfree(domain); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 363 | } else if (ses->serverName) { |
| 364 | len = strlen(ses->serverName); |
| 365 | |
| 366 | server = kmalloc(2 + (len * 2), GFP_KERNEL); |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 367 | if (server == NULL) |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 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 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 376 | crypto_shash_update(&sdesc.shash, (char *)server, 2 * len); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 377 | |
| 378 | kfree(server); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 379 | } |
| 380 | calc_exit_1: |
| 381 | kfree(user); |
| 382 | calc_exit_2: |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 383 | /* BB FIXME what about bytes 24 through 40 of the signing key? |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 384 | compare with the NTLM example */ |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 385 | rc = crypto_shash_final(&sdesc.shash, ses->server->ntlmv2_hash); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 386 | |
| 387 | return rc; |
| 388 | } |
| 389 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 390 | static int |
| 391 | find_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 | |
| 440 | static int |
| 441 | CalcNTLMv2_response(const struct TCP_Server_Info *server, |
| 442 | char *v2_session_response) |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 443 | { |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 444 | int rc; |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 445 | struct { |
| 446 | struct shash_desc shash; |
| 447 | char ctx[crypto_shash_descsize(server->ntlmssp.hmacmd5)]; |
| 448 | } sdesc; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 449 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 450 | sdesc.shash.tfm = server->ntlmssp.hmacmd5; |
| 451 | sdesc.shash.flags = 0x0; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 452 | |
| 453 | crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash, |
| 454 | CIFS_HMAC_MD5_HASH_SIZE); |
| 455 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 456 | rc = crypto_shash_init(&sdesc.shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 457 | if (rc) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 458 | cERROR(1, "could not initialize master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 459 | return rc; |
| 460 | } |
| 461 | |
| 462 | memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE, |
| 463 | server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE); |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 464 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 465 | v2_session_response + CIFS_SERVER_CHALLENGE_SIZE, |
| 466 | sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE); |
| 467 | |
| 468 | if (server->tilen) |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 469 | crypto_shash_update(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 470 | server->tiblob, server->tilen); |
| 471 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 472 | rc = crypto_shash_final(&sdesc.shash, v2_session_response); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 473 | |
| 474 | return rc; |
| 475 | } |
| 476 | |
| 477 | int |
| 478 | setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf, |
| 479 | const struct nls_table *nls_cp) |
| 480 | { |
| 481 | int rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 482 | struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf; |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 483 | struct { |
| 484 | struct shash_desc shash; |
| 485 | char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)]; |
| 486 | } sdesc; |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 487 | |
| 488 | buf->blob_signature = cpu_to_le32(0x00000101); |
| 489 | buf->reserved = 0; |
| 490 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 491 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 492 | buf->reserved2 = 0; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 493 | |
| 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 French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 501 | |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 502 | /* calculate buf->ntlmv2_hash */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 503 | rc = calc_ntlmv2_hash(ses, nls_cp); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 504 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 505 | cERROR(1, "could not get v2 hash rc %d", rc); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 506 | 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 French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 513 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 514 | crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, |
| 515 | ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 516 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 517 | sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5; |
| 518 | sdesc.shash.flags = 0x0; |
| 519 | |
| 520 | rc = crypto_shash_init(&sdesc.shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 521 | if (rc) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 522 | cERROR(1, "could not initialize master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 523 | return rc; |
| 524 | } |
| 525 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 526 | crypto_shash_update(&sdesc.shash, resp_buf, CIFS_HMAC_MD5_HASH_SIZE); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 527 | |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 528 | rc = crypto_shash_final(&sdesc.shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 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 French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 538 | int |
| 539 | calc_seckey(struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 541 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 547 | get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 549 | 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 | |
Shirish Pargaonkar | 3ec6bbc | 2010-08-23 11:04:07 -0500 | [diff] [blame] | 556 | desc.tfm = tfm_arc4; |
| 557 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 558 | crypto_blkcipher_setkey(tfm_arc4, |
| 559 | server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE); |
| 560 | sg_init_one(&sgin, sec_key, CIFS_CPHTXT_SIZE); |
| 561 | sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE); |
| 562 | rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE); |
| 563 | |
| 564 | if (!rc) |
| 565 | memcpy(server->session_key.data.ntlmv2.key, |
| 566 | sec_key, CIFS_NTLMV2_SESSKEY_SIZE); |
| 567 | |
| 568 | crypto_free_blkcipher(tfm_arc4); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | cifs_crypto_shash_release(struct TCP_Server_Info *server) |
| 575 | { |
| 576 | if (server->ntlmssp.md5) |
| 577 | crypto_free_shash(server->ntlmssp.md5); |
| 578 | |
| 579 | if (server->ntlmssp.hmacmd5) |
| 580 | crypto_free_shash(server->ntlmssp.hmacmd5); |
| 581 | } |
| 582 | |
| 583 | int |
| 584 | cifs_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 585 | { |
| 586 | server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0); |
| 587 | if (!server->ntlmssp.hmacmd5 || |
| 588 | IS_ERR(server->ntlmssp.hmacmd5)) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 589 | cERROR(1, "could not allocate master crypto API hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0); |
| 594 | if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) { |
Steve French | 56234e2 | 2010-09-08 20:57:05 +0000 | [diff] [blame^] | 595 | crypto_free_shash(server->ntlmssp.hmacmd5); |
| 596 | cERROR(1, "could not allocate master crypto API md5\n"); |
| 597 | return 1; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |