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 | { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 48 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 50 | if (cifs_pdu == NULL || server == NULL || signature == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return -EINVAL; |
| 52 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 53 | if (!server->ntlmssp.sdescmd5) { |
| 54 | cERROR(1, |
| 55 | "cifs_calculate_signature: can't generate signature\n"); |
| 56 | return -1; |
| 57 | } |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 58 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 59 | rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 60 | if (rc) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 61 | cERROR(1, "cifs_calculate_signature: oould not init md5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 62 | return rc; |
| 63 | } |
| 64 | |
| 65 | if (server->secType == RawNTLMSSP) |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 66 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 67 | server->session_key.data.ntlmv2.key, |
| 68 | CIFS_NTLMV2_SESSKEY_SIZE); |
| 69 | else |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 70 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 71 | (char *)&server->session_key.data, |
| 72 | server->session_key.len); |
| 73 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 74 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 75 | cifs_pdu->Protocol, cifs_pdu->smb_buf_length); |
| 76 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 77 | rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 78 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 79 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 82 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 83 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
| 84 | __u32 *pexpected_response_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | int rc = 0; |
| 87 | char smb_signature[20]; |
| 88 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 89 | if ((cifs_pdu == NULL) || (server == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 92 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return rc; |
| 94 | |
| 95 | spin_lock(&GlobalMid_Lock); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 96 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 97 | cpu_to_le32(server->sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 99 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 100 | *pexpected_response_sequence_number = server->sequence_number++; |
| 101 | server->sequence_number++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | spin_unlock(&GlobalMid_Lock); |
| 103 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 104 | rc = cifs_calculate_signature(cifs_pdu, server, smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 105 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | 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 French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 113 | static int cifs_calc_signature2(const struct kvec *iov, int n_vec, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 114 | struct TCP_Server_Info *server, char *signature) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 115 | { |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 116 | int i; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 117 | int rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 118 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 119 | if (iov == NULL || server == NULL || signature == NULL) |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 120 | return -EINVAL; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 121 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 122 | if (!server->ntlmssp.sdescmd5) { |
| 123 | cERROR(1, "cifs_calc_signature2: can't generate signature\n"); |
| 124 | return -1; |
| 125 | } |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 126 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 127 | rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 128 | if (rc) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 129 | cERROR(1, "cifs_calc_signature2: oould not init md5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 130 | return rc; |
| 131 | } |
| 132 | |
| 133 | if (server->secType == RawNTLMSSP) |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 134 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 135 | server->session_key.data.ntlmv2.key, |
| 136 | CIFS_NTLMV2_SESSKEY_SIZE); |
| 137 | else |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 138 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 139 | (char *)&server->session_key.data, |
| 140 | server->session_key.len); |
| 141 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 142 | for (i = 0; i < n_vec; i++) { |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 143 | if (iov[i].iov_len == 0) |
| 144 | continue; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 145 | if (iov[i].iov_base == NULL) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 146 | cERROR(1, "cifs_calc_signature2: null iovec entry"); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 147 | return -EIO; |
Jeff Layton | 745542e | 2007-11-03 04:34:04 +0000 | [diff] [blame] | 148 | } |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 149 | /* The first entry includes a length field (which does not get |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 150 | signed that occupies the first 4 bytes before the header */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 151 | if (i == 0) { |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 152 | if (iov[0].iov_len <= 8) /* cmd field at offset 9 */ |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 153 | break; /* nothing to sign or corrupt header */ |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 154 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 155 | iov[i].iov_base + 4, iov[i].iov_len - 4); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 156 | } else |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 157 | crypto_shash_update(&server->ntlmssp.sdescmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 158 | iov[i].iov_base, iov[i].iov_len); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 159 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 160 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 161 | rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 162 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 163 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 166 | 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] | 167 | __u32 *pexpected_response_sequence_number) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 168 | { |
| 169 | int rc = 0; |
| 170 | char smb_signature[20]; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 171 | struct smb_hdr *cifs_pdu = iov[0].iov_base; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 172 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 173 | if ((cifs_pdu == NULL) || (server == NULL)) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 174 | return -EINVAL; |
| 175 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 176 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 177 | return rc; |
| 178 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 179 | spin_lock(&GlobalMid_Lock); |
| 180 | cifs_pdu->Signature.Sequence.SequenceNumber = |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 181 | cpu_to_le32(server->sequence_number); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 182 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 183 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 184 | *pexpected_response_sequence_number = server->sequence_number++; |
| 185 | server->sequence_number++; |
| 186 | spin_unlock(&GlobalMid_Lock); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 187 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 188 | rc = cifs_calc_signature2(iov, n_vec, server, smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 189 | if (rc) |
| 190 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
| 191 | else |
| 192 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 193 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 194 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 197 | int cifs_verify_signature(struct smb_hdr *cifs_pdu, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 198 | struct TCP_Server_Info *server, |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 199 | __u32 expected_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 201 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | char server_response_sig[8]; |
| 203 | char what_we_think_sig_should_be[20]; |
| 204 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 205 | if (cifs_pdu == NULL || server == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 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 French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 212 | struct smb_com_lock_req *pSMB = |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame] | 213 | (struct smb_com_lock_req *)cifs_pdu; |
| 214 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 218 | /* BB what if signatures are supposed to be on for session but |
| 219 | server does not send one? BB */ |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 222 | if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 223 | cFYI(1, "dummy signature received for smb command 0x%x", |
| 224 | cifs_pdu->Command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | /* save off the origiginal signature so we can modify the smb and check |
| 227 | its signature against what the server sent */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 228 | memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 230 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 231 | cpu_to_le32(expected_sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | cifs_pdu->Signature.Sequence.Reserved = 0; |
| 233 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 234 | rc = cifs_calculate_signature(cifs_pdu, server, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | what_we_think_sig_should_be); |
| 236 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 237 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return rc; |
| 239 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 240 | /* cifs_dump_mem("what we think it should be: ", |
| 241 | what_we_think_sig_should_be, 16); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 243 | if (memcmp(server_response_sig, what_we_think_sig_should_be, 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | 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 French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 251 | int cifs_calculate_session_key(struct session_key *key, const char *rn, |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 252 | const char *password) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | char temp_key[16]; |
| 255 | if ((key == NULL) || (rn == NULL)) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | E_md4hash(password, temp_key); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 259 | mdfour(key->data.ntlm, temp_key, 16); |
| 260 | memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE); |
| 261 | key->len = 40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 265 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 266 | void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt, |
| 267 | char *lnm_session_key) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 268 | { |
| 269 | int i; |
| 270 | char password_with_pad[CIFS_ENCPWD_SIZE]; |
| 271 | |
| 272 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 273 | if (password) |
| 274 | strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 275 | |
Jeff Layton | 04912d6 | 2010-04-24 07:57:45 -0400 | [diff] [blame] | 276 | if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) { |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 277 | 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 French | bdc4bf6e | 2006-06-02 22:57:13 +0000 | [diff] [blame] | 282 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 283 | /* 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 Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 294 | for (i = 0; i < CIFS_ENCPWD_SIZE; i++) |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 295 | password_with_pad[i] = toupper(password_with_pad[i]); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 296 | |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 297 | SMBencrypt(password_with_pad, cryptkey, lnm_session_key); |
| 298 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 299 | /* 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 French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 304 | static int calc_ntlmv2_hash(struct cifsSesInfo *ses, |
| 305 | const struct nls_table *nls_cp) |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 306 | { |
| 307 | int rc = 0; |
| 308 | int len; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 309 | char nt_hash[CIFS_NTHASH_SIZE]; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 310 | wchar_t *user; |
| 311 | wchar_t *domain; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 312 | wchar_t *server; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 313 | |
| 314 | if (!ses->server->ntlmssp.sdeschmacmd5) { |
| 315 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n"); |
| 316 | return -1; |
| 317 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 318 | |
| 319 | /* calculate md4 hash of password */ |
| 320 | E_md4hash(ses->password, nt_hash); |
| 321 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 322 | crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash, |
| 323 | CIFS_NTHASH_SIZE); |
| 324 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 325 | rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 326 | if (rc) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 327 | cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 328 | return rc; |
| 329 | } |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 330 | |
| 331 | /* convert ses->userName to unicode and uppercase */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 332 | len = strlen(ses->userName); |
| 333 | user = kmalloc(2 + (len * 2), GFP_KERNEL); |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 334 | if (user == NULL) { |
| 335 | cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n"); |
| 336 | rc = -ENOMEM; |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 337 | goto calc_exit_2; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 338 | } |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 339 | len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 340 | UniStrupr(user); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 341 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 342 | crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash, |
| 343 | (char *)user, 2 * len); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 344 | |
| 345 | /* convert ses->domainName to unicode and uppercase */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 346 | if (ses->domainName) { |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 347 | len = strlen(ses->domainName); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 348 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 349 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 350 | if (domain == NULL) { |
| 351 | cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure"); |
| 352 | rc = -ENOMEM; |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 353 | goto calc_exit_1; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 354 | } |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 355 | len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len, |
| 356 | nls_cp); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 357 | /* 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 French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 361 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 362 | crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash, |
| 363 | (char *)domain, 2 * len); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 364 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 365 | kfree(domain); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 366 | } else if (ses->serverName) { |
| 367 | len = strlen(ses->serverName); |
| 368 | |
| 369 | server = kmalloc(2 + (len * 2), GFP_KERNEL); |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 370 | if (server == NULL) { |
| 371 | cERROR(1, "calc_ntlmv2_hash: server mem alloc failure"); |
| 372 | rc = -ENOMEM; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 373 | goto calc_exit_1; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 374 | } |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 375 | 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.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 382 | crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash, |
| 383 | (char *)server, 2 * len); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 384 | |
| 385 | kfree(server); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 386 | } |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 387 | |
| 388 | rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash, |
| 389 | ses->server->ntlmv2_hash); |
| 390 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 391 | calc_exit_1: |
| 392 | kfree(user); |
| 393 | calc_exit_2: |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 394 | /* BB FIXME what about bytes 24 through 40 of the signing key? |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 395 | compare with the NTLM example */ |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 396 | |
| 397 | return rc; |
| 398 | } |
| 399 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 400 | static int |
| 401 | find_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 | |
| 450 | static int |
| 451 | CalcNTLMv2_response(const struct TCP_Server_Info *server, |
| 452 | char *v2_session_response) |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 453 | { |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 454 | int rc; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 455 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 456 | if (!server->ntlmssp.sdeschmacmd5) { |
| 457 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n"); |
| 458 | return -1; |
| 459 | } |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 460 | |
| 461 | crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash, |
| 462 | CIFS_HMAC_MD5_HASH_SIZE); |
| 463 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 464 | rc = crypto_shash_init(&server->ntlmssp.sdeschmacmd5->shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 465 | if (rc) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 466 | cERROR(1, "CalcNTLMv2_response: could not init hmacmd5"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE, |
| 471 | server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE); |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 472 | crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 473 | v2_session_response + CIFS_SERVER_CHALLENGE_SIZE, |
| 474 | sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE); |
| 475 | |
| 476 | if (server->tilen) |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 477 | crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 478 | server->tiblob, server->tilen); |
| 479 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 480 | rc = crypto_shash_final(&server->ntlmssp.sdeschmacmd5->shash, |
| 481 | v2_session_response); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 482 | |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | int |
| 487 | setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf, |
| 488 | const struct nls_table *nls_cp) |
| 489 | { |
| 490 | int rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 491 | struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf; |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 492 | |
| 493 | buf->blob_signature = cpu_to_le32(0x00000101); |
| 494 | buf->reserved = 0; |
| 495 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 496 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 497 | buf->reserved2 = 0; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 498 | |
| 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 French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 506 | |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 507 | /* calculate buf->ntlmv2_hash */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 508 | rc = calc_ntlmv2_hash(ses, nls_cp); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 509 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 510 | cERROR(1, "could not get v2 hash rc %d", rc); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 511 | 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 French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 518 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 519 | if (!ses->server->ntlmssp.sdeschmacmd5) { |
| 520 | cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n"); |
| 521 | return -1; |
| 522 | } |
| 523 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 524 | crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, |
| 525 | ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 526 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 527 | rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 528 | if (rc) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 529 | cERROR(1, "setup_ntlmv2_rsp: could not init hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 530 | return rc; |
| 531 | } |
| 532 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 533 | crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash, |
| 534 | resp_buf, CIFS_HMAC_MD5_HASH_SIZE); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 535 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 536 | rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash, |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 537 | 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 French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 546 | int |
| 547 | calc_seckey(struct TCP_Server_Info *server) |
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 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 555 | get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 557 | 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 Pargaonkar | 3ec6bbc | 2010-08-23 11:04:07 -0500 | [diff] [blame] | 564 | desc.tfm = tfm_arc4; |
| 565 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 566 | 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 | |
| 581 | void |
| 582 | cifs_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.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 589 | |
| 590 | kfree(server->ntlmssp.sdeschmacmd5); |
| 591 | |
| 592 | kfree(server->ntlmssp.sdescmd5); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | int |
| 596 | cifs_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 597 | { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 598 | int rc; |
| 599 | unsigned int size; |
| 600 | |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 601 | server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0); |
| 602 | if (!server->ntlmssp.hmacmd5 || |
| 603 | IS_ERR(server->ntlmssp.hmacmd5)) { |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 604 | cERROR(1, "could not allocate crypto hmacmd5\n"); |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 605 | 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.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 610 | cERROR(1, "could not allocate crypto md5\n"); |
| 611 | rc = 1; |
| 612 | goto cifs_crypto_shash_allocate_ret1; |
Steve French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 613 | } |
| 614 | |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 615 | 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 French | 9fbc590 | 2010-08-20 20:42:26 +0000 | [diff] [blame] | 638 | return 0; |
shirishpargaonkar@gmail.com | 2d20ca8 | 2010-08-24 11:53:48 -0500 | [diff] [blame] | 639 | |
| 640 | cifs_crypto_shash_allocate_ret3: |
| 641 | kfree(server->ntlmssp.sdeschmacmd5); |
| 642 | |
| 643 | cifs_crypto_shash_allocate_ret2: |
| 644 | crypto_free_shash(server->ntlmssp.md5); |
| 645 | |
| 646 | cifs_crypto_shash_allocate_ret1: |
| 647 | crypto_free_shash(server->ntlmssp.hmacmd5); |
| 648 | |
| 649 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |