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> |
| 23 | #include "cifspdu.h" |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 24 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "cifs_debug.h" |
| 26 | #include "md5.h" |
| 27 | #include "cifs_unicode.h" |
| 28 | #include "cifsproto.h" |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 29 | #include <linux/ctype.h> |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 30 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 32 | /* 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] | 33 | /* the 16 byte signature must be allocated by the caller */ |
| 34 | /* Note we only use the 1st eight bytes */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 35 | /* Note that the smb header signature field on input contains the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | sequence number before this function is called */ |
| 37 | |
| 38 | extern void mdfour(unsigned char *out, unsigned char *in, int n); |
| 39 | extern void E_md4hash(const unsigned char *passwd, unsigned char *p16); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 40 | extern void SMBencrypt(unsigned char *passwd, unsigned char *c8, |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 41 | unsigned char *p24); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 43 | static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, |
| 44 | const char *key, char *signature) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct MD5Context context; |
| 47 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 48 | if ((cifs_pdu == NULL) || (signature == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return -EINVAL; |
| 50 | |
| 51 | MD5Init(&context); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 52 | MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16); |
| 53 | MD5Update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length); |
| 54 | MD5Final(signature, &context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 58 | int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, |
| 59 | __u32 *pexpected_response_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | int rc = 0; |
| 62 | char smb_signature[20]; |
| 63 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 64 | if ((cifs_pdu == NULL) || (server == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return -EINVAL; |
| 66 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 67 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return rc; |
| 69 | |
| 70 | spin_lock(&GlobalMid_Lock); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 71 | cifs_pdu->Signature.Sequence.SequenceNumber = |
| 72 | cpu_to_le32(server->sequence_number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | cifs_pdu->Signature.Sequence.Reserved = 0; |
| 74 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 75 | *pexpected_response_sequence_number = server->sequence_number++; |
| 76 | server->sequence_number++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | spin_unlock(&GlobalMid_Lock); |
| 78 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 79 | rc = cifs_calculate_signature(cifs_pdu, server->mac_signing_key, |
| 80 | smb_signature); |
| 81 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
| 83 | else |
| 84 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); |
| 85 | |
| 86 | return rc; |
| 87 | } |
| 88 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 89 | static int cifs_calc_signature2(const struct kvec *iov, int n_vec, |
| 90 | const char *key, char *signature) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 91 | { |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 92 | struct MD5Context context; |
| 93 | int i; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 94 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 95 | if ((iov == NULL) || (signature == NULL)) |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 96 | return -EINVAL; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 97 | |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 98 | MD5Init(&context); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 99 | MD5Update(&context, key, CIFS_SESS_KEY_SIZE+16); |
| 100 | for (i=0;i<n_vec;i++) { |
| 101 | if (iov[i].iov_base == NULL) { |
| 102 | cERROR(1 ,("null iovec entry")); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 103 | return -EIO; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 104 | } else if (iov[i].iov_len == 0) |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 105 | break; /* bail out if we are sent nothing to sign */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 106 | /* The first entry includes a length field (which does not get |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 107 | signed that occupies the first 4 bytes before the header */ |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 108 | if (i == 0) { |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 109 | if (iov[0].iov_len <= 8 ) /* cmd field at offset 9 */ |
| 110 | break; /* nothing to sign or corrupt header */ |
| 111 | MD5Update(&context,iov[0].iov_base+4, iov[0].iov_len-4); |
| 112 | } else |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 113 | MD5Update(&context, iov[i].iov_base, iov[i].iov_len); |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 114 | } |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 115 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 116 | MD5Final(signature, &context); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 117 | |
Steve French | e9917a0 | 2006-03-31 21:22:00 +0000 | [diff] [blame] | 118 | return 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 122 | int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 123 | __u32 * pexpected_response_sequence_number) |
| 124 | { |
| 125 | int rc = 0; |
| 126 | char smb_signature[20]; |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 127 | struct smb_hdr *cifs_pdu = iov[0].iov_base; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 128 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 129 | if ((cifs_pdu == NULL) || (server == NULL)) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 132 | if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0) |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 133 | return rc; |
| 134 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 135 | spin_lock(&GlobalMid_Lock); |
| 136 | cifs_pdu->Signature.Sequence.SequenceNumber = |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 137 | cpu_to_le32(server->sequence_number); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 138 | cifs_pdu->Signature.Sequence.Reserved = 0; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 139 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 140 | *pexpected_response_sequence_number = server->sequence_number++; |
| 141 | server->sequence_number++; |
| 142 | spin_unlock(&GlobalMid_Lock); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 143 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 144 | rc = cifs_calc_signature2(iov, n_vec, server->mac_signing_key, |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 145 | smb_signature); |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 146 | if (rc) |
| 147 | memset(cifs_pdu->Signature.SecuritySignature, 0, 8); |
| 148 | else |
| 149 | memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 150 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 151 | return rc; |
Steve French | 84afc29 | 2005-12-02 13:32:45 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 154 | int cifs_verify_signature(struct smb_hdr *cifs_pdu, const char *mac_key, |
| 155 | __u32 expected_sequence_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | unsigned int rc; |
| 158 | char server_response_sig[8]; |
| 159 | char what_we_think_sig_should_be[20]; |
| 160 | |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 161 | if ((cifs_pdu == NULL) || (mac_key == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | |
| 164 | if (cifs_pdu->Command == SMB_COM_NEGOTIATE) |
| 165 | return 0; |
| 166 | |
| 167 | if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) { |
Steve French | ffdd6e4 | 2007-06-24 21:15:44 +0000 | [diff] [blame^] | 168 | struct smb_com_lock_req * pSMB = |
| 169 | (struct smb_com_lock_req *)cifs_pdu; |
| 170 | if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* BB what if signatures are supposed to be on for session but server does not |
| 175 | send one? BB */ |
| 176 | |
| 177 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
| 178 | if(memcmp(cifs_pdu->Signature.SecuritySignature,"BSRSPYL ",8)==0) |
| 179 | cFYI(1,("dummy signature received for smb command 0x%x",cifs_pdu->Command)); |
| 180 | |
| 181 | /* save off the origiginal signature so we can modify the smb and check |
| 182 | its signature against what the server sent */ |
| 183 | memcpy(server_response_sig,cifs_pdu->Signature.SecuritySignature,8); |
| 184 | |
| 185 | cifs_pdu->Signature.Sequence.SequenceNumber = cpu_to_le32(expected_sequence_number); |
| 186 | cifs_pdu->Signature.Sequence.Reserved = 0; |
| 187 | |
| 188 | rc = cifs_calculate_signature(cifs_pdu, mac_key, |
| 189 | what_we_think_sig_should_be); |
| 190 | |
| 191 | if(rc) |
| 192 | return rc; |
| 193 | |
| 194 | |
| 195 | /* cifs_dump_mem("what we think it should be: ",what_we_think_sig_should_be,16); */ |
| 196 | |
| 197 | if(memcmp(server_response_sig, what_we_think_sig_should_be, 8)) |
| 198 | return -EACCES; |
| 199 | else |
| 200 | return 0; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | /* We fill in key by putting in 40 byte array which was allocated by caller */ |
| 205 | int cifs_calculate_mac_key(char * key, const char * rn, const char * password) |
| 206 | { |
| 207 | char temp_key[16]; |
| 208 | if ((key == NULL) || (rn == NULL)) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | E_md4hash(password, temp_key); |
| 212 | mdfour(key,temp_key,16); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 213 | memcpy(key+16,rn, CIFS_SESS_KEY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 217 | int CalcNTLMv2_partial_mac_key(struct cifsSesInfo * ses, |
| 218 | const struct nls_table * nls_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | char temp_hash[16]; |
| 221 | struct HMACMD5Context ctx; |
| 222 | char * ucase_buf; |
Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 223 | __le16 * unicode_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | unsigned int i,user_name_len,dom_name_len; |
| 225 | |
| 226 | if(ses == NULL) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | E_md4hash(ses->password, temp_hash); |
| 230 | |
| 231 | hmac_md5_init_limK_to_64(temp_hash, 16, &ctx); |
| 232 | user_name_len = strlen(ses->userName); |
| 233 | if(user_name_len > MAX_USERNAME_SIZE) |
| 234 | return -EINVAL; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 235 | if(ses->domainName == NULL) |
| 236 | return -EINVAL; /* BB should we use CIFS_LINUX_DOM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | dom_name_len = strlen(ses->domainName); |
| 238 | if(dom_name_len > MAX_USERNAME_SIZE) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL); |
| 242 | if(ucase_buf == NULL) |
| 243 | return -ENOMEM; |
| 244 | unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL); |
| 245 | if(unicode_buf == NULL) { |
| 246 | kfree(ucase_buf); |
| 247 | return -ENOMEM; |
| 248 | } |
| 249 | |
| 250 | for(i=0;i<user_name_len;i++) |
| 251 | ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]]; |
| 252 | ucase_buf[i] = 0; |
| 253 | user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); |
| 254 | unicode_buf[user_name_len] = 0; |
| 255 | user_name_len++; |
| 256 | |
| 257 | for(i=0;i<dom_name_len;i++) |
| 258 | ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]]; |
| 259 | ucase_buf[i] = 0; |
| 260 | dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf, MAX_USERNAME_SIZE*2, nls_info); |
| 261 | |
| 262 | unicode_buf[user_name_len + dom_name_len] = 0; |
| 263 | hmac_md5_update((const unsigned char *) unicode_buf, |
| 264 | (user_name_len+dom_name_len)*2,&ctx); |
| 265 | |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 266 | hmac_md5_final(ses->server->mac_signing_key,&ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | kfree(ucase_buf); |
| 268 | kfree(unicode_buf); |
| 269 | return 0; |
| 270 | } |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 271 | |
| 272 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
| 273 | void calc_lanman_hash(struct cifsSesInfo * ses, char * lnm_session_key) |
| 274 | { |
| 275 | int i; |
| 276 | char password_with_pad[CIFS_ENCPWD_SIZE]; |
| 277 | |
Steve French | bdc4bf6e | 2006-06-02 22:57:13 +0000 | [diff] [blame] | 278 | if(ses->server == NULL) |
| 279 | return; |
| 280 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 281 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
Steve French | 66abda5 | 2006-08-11 16:52:09 +0000 | [diff] [blame] | 282 | if(ses->password) |
| 283 | strncpy(password_with_pad, ses->password, CIFS_ENCPWD_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 284 | |
Steve French | bdc4bf6e | 2006-06-02 22:57:13 +0000 | [diff] [blame] | 285 | if((ses->server->secMode & SECMODE_PW_ENCRYPT) == 0) |
| 286 | if(extended_security & CIFSSEC_MAY_PLNTXT) { |
| 287 | memcpy(lnm_session_key, password_with_pad, CIFS_ENCPWD_SIZE); |
| 288 | return; |
| 289 | } |
| 290 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 291 | /* calculate old style session key */ |
| 292 | /* calling toupper is less broken than repeatedly |
| 293 | calling nls_toupper would be since that will never |
| 294 | work for UTF8, but neither handles multibyte code pages |
| 295 | but the only alternative would be converting to UCS-16 (Unicode) |
| 296 | (using a routine something like UniStrupr) then |
| 297 | uppercasing and then converting back from Unicode - which |
| 298 | would only worth doing it if we knew it were utf8. Basically |
| 299 | utf8 and other multibyte codepages each need their own strupper |
| 300 | function since a byte at a time will ont work. */ |
| 301 | |
| 302 | for(i = 0; i < CIFS_ENCPWD_SIZE; i++) { |
| 303 | password_with_pad[i] = toupper(password_with_pad[i]); |
| 304 | } |
| 305 | |
| 306 | SMBencrypt(password_with_pad, ses->server->cryptKey, lnm_session_key); |
| 307 | /* clear password before we return/free memory */ |
| 308 | memset(password_with_pad, 0, CIFS_ENCPWD_SIZE); |
| 309 | } |
| 310 | #endif /* CIFS_WEAK_PW_HASH */ |
| 311 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 312 | static int calc_ntlmv2_hash(struct cifsSesInfo *ses, |
| 313 | const struct nls_table * nls_cp) |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 314 | { |
| 315 | int rc = 0; |
| 316 | int len; |
| 317 | char nt_hash[16]; |
| 318 | struct HMACMD5Context * pctxt; |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 319 | wchar_t * user; |
| 320 | wchar_t * domain; |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 321 | |
| 322 | pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL); |
| 323 | |
| 324 | if(pctxt == NULL) |
| 325 | return -ENOMEM; |
| 326 | |
| 327 | /* calculate md4 hash of password */ |
| 328 | E_md4hash(ses->password, nt_hash); |
| 329 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 330 | /* convert Domainname to unicode and uppercase */ |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 331 | hmac_md5_init_limK_to_64(nt_hash, 16, pctxt); |
| 332 | |
| 333 | /* convert ses->userName to unicode and uppercase */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 334 | len = strlen(ses->userName); |
| 335 | user = kmalloc(2 + (len * 2), GFP_KERNEL); |
| 336 | if(user == NULL) |
| 337 | goto calc_exit_2; |
| 338 | len = cifs_strtoUCS(user, ses->userName, len, nls_cp); |
| 339 | UniStrupr(user); |
| 340 | hmac_md5_update((char *)user, 2*len, pctxt); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 341 | |
| 342 | /* convert ses->domainName to unicode and uppercase */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 343 | if(ses->domainName) { |
| 344 | len = strlen(ses->domainName); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 345 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 346 | domain = kmalloc(2 + (len * 2), GFP_KERNEL); |
| 347 | if(domain == NULL) |
| 348 | goto calc_exit_1; |
| 349 | len = cifs_strtoUCS(domain, ses->domainName, len, nls_cp); |
| 350 | UniStrupr(domain); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 351 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 352 | hmac_md5_update((char *)domain, 2*len, pctxt); |
| 353 | |
| 354 | kfree(domain); |
| 355 | } |
| 356 | calc_exit_1: |
| 357 | kfree(user); |
| 358 | calc_exit_2: |
| 359 | /* BB FIXME what about bytes 24 through 40 of the signing key? |
| 360 | compare with the NTLM example */ |
| 361 | hmac_md5_final(ses->server->mac_signing_key, pctxt); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 362 | |
| 363 | return rc; |
| 364 | } |
| 365 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 366 | void setup_ntlmv2_rsp(struct cifsSesInfo * ses, char * resp_buf, |
| 367 | const struct nls_table * nls_cp) |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 368 | { |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 369 | int rc; |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 370 | struct ntlmv2_resp * buf = (struct ntlmv2_resp *)resp_buf; |
| 371 | |
| 372 | buf->blob_signature = cpu_to_le32(0x00000101); |
| 373 | buf->reserved = 0; |
| 374 | buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 375 | get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 376 | buf->reserved2 = 0; |
Steve French | 33ec32f | 2006-12-08 04:14:28 +0000 | [diff] [blame] | 377 | buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE); |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 378 | buf->names[0].length = 0; |
Steve French | 33ec32f | 2006-12-08 04:14:28 +0000 | [diff] [blame] | 379 | buf->names[1].type = 0; |
| 380 | buf->names[1].length = 0; |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 381 | |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 382 | /* calculate buf->ntlmv2_hash */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 383 | rc = calc_ntlmv2_hash(ses, nls_cp); |
Steve French | a8ee034 | 2006-06-05 23:34:19 +0000 | [diff] [blame] | 384 | if(rc) |
| 385 | cERROR(1,("could not get v2 hash rc %d",rc)); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 386 | CalcNTLMv2_response(ses, resp_buf); |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 389 | void CalcNTLMv2_response(const struct cifsSesInfo * ses, char * v2_session_response) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | struct HMACMD5Context context; |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 392 | /* rest of v2 struct already generated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | memcpy(v2_session_response + 8, ses->server->cryptKey,8); |
Steve French | ad009ac | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 394 | hmac_md5_init_limK_to_64(ses->server->mac_signing_key, 16, &context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 396 | hmac_md5_update(v2_session_response+8, |
| 397 | sizeof(struct ntlmv2_resp) - 8, &context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | hmac_md5_final(v2_session_response,&context); |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 400 | /* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |