cifsencrypt.c 13 KB

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