cifsencrypt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, const 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. cifs_MD5_init(&context);
  46. cifs_MD5_update(&context, (char *)&key->data, key->len);
  47. cifs_MD5_update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
  48. cifs_MD5_final(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. cifs_MD5_init(&context);
  83. cifs_MD5_update(&context, (char *)&key->data, key->len);
  84. for (i = 0; i < n_vec; i++) {
  85. if (iov[i].iov_len == 0)
  86. continue;
  87. if (iov[i].iov_base == NULL) {
  88. cERROR(1, ("null iovec entry"));
  89. return -EIO;
  90. }
  91. /* The first entry includes a length field (which does not get
  92. signed that occupies the first 4 bytes before the header */
  93. if (i == 0) {
  94. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  95. break; /* nothing to sign or corrupt header */
  96. cifs_MD5_update(&context, iov[0].iov_base+4,
  97. iov[0].iov_len-4);
  98. } else
  99. cifs_MD5_update(&context, iov[i].iov_base, iov[i].iov_len);
  100. }
  101. cifs_MD5_final(signature, &context);
  102. return 0;
  103. }
  104. int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  105. __u32 *pexpected_response_sequence_number)
  106. {
  107. int rc = 0;
  108. char smb_signature[20];
  109. struct smb_hdr *cifs_pdu = iov[0].iov_base;
  110. if ((cifs_pdu == NULL) || (server == NULL))
  111. return -EINVAL;
  112. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  113. return rc;
  114. spin_lock(&GlobalMid_Lock);
  115. cifs_pdu->Signature.Sequence.SequenceNumber =
  116. cpu_to_le32(server->sequence_number);
  117. cifs_pdu->Signature.Sequence.Reserved = 0;
  118. *pexpected_response_sequence_number = server->sequence_number++;
  119. server->sequence_number++;
  120. spin_unlock(&GlobalMid_Lock);
  121. rc = cifs_calc_signature2(iov, n_vec, &server->mac_signing_key,
  122. smb_signature);
  123. if (rc)
  124. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  125. else
  126. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  127. return rc;
  128. }
  129. int cifs_verify_signature(struct smb_hdr *cifs_pdu,
  130. const struct mac_key *mac_key,
  131. __u32 expected_sequence_number)
  132. {
  133. unsigned int rc;
  134. char server_response_sig[8];
  135. char what_we_think_sig_should_be[20];
  136. if ((cifs_pdu == NULL) || (mac_key == NULL))
  137. return -EINVAL;
  138. if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
  139. return 0;
  140. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  141. struct smb_com_lock_req *pSMB =
  142. (struct smb_com_lock_req *)cifs_pdu;
  143. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  144. return 0;
  145. }
  146. /* BB what if signatures are supposed to be on for session but
  147. server does not send one? BB */
  148. /* Do not need to verify session setups with signature "BSRSPYL " */
  149. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  150. cFYI(1, ("dummy signature received for smb command 0x%x",
  151. cifs_pdu->Command));
  152. /* save off the origiginal signature so we can modify the smb and check
  153. its signature against what the server sent */
  154. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  155. cifs_pdu->Signature.Sequence.SequenceNumber =
  156. cpu_to_le32(expected_sequence_number);
  157. cifs_pdu->Signature.Sequence.Reserved = 0;
  158. rc = cifs_calculate_signature(cifs_pdu, mac_key,
  159. what_we_think_sig_should_be);
  160. if (rc)
  161. return rc;
  162. /* cifs_dump_mem("what we think it should be: ",
  163. what_we_think_sig_should_be, 16); */
  164. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  165. return -EACCES;
  166. else
  167. return 0;
  168. }
  169. /* We fill in key by putting in 40 byte array which was allocated by caller */
  170. int cifs_calculate_mac_key(struct mac_key *key, const char *rn,
  171. const char *password)
  172. {
  173. char temp_key[16];
  174. if ((key == NULL) || (rn == NULL))
  175. return -EINVAL;
  176. E_md4hash(password, temp_key);
  177. mdfour(key->data.ntlm, temp_key, 16);
  178. memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
  179. key->len = 40;
  180. return 0;
  181. }
  182. int CalcNTLMv2_partial_mac_key(struct cifsSesInfo *ses,
  183. const struct nls_table *nls_info)
  184. {
  185. char temp_hash[16];
  186. struct HMACMD5Context ctx;
  187. char *ucase_buf;
  188. __le16 *unicode_buf;
  189. unsigned int i, user_name_len, dom_name_len;
  190. if (ses == NULL)
  191. return -EINVAL;
  192. E_md4hash(ses->password, temp_hash);
  193. hmac_md5_init_limK_to_64(temp_hash, 16, &ctx);
  194. user_name_len = strlen(ses->userName);
  195. if (user_name_len > MAX_USERNAME_SIZE)
  196. return -EINVAL;
  197. if (ses->domainName == NULL)
  198. return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
  199. dom_name_len = strlen(ses->domainName);
  200. if (dom_name_len > MAX_USERNAME_SIZE)
  201. return -EINVAL;
  202. ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
  203. if (ucase_buf == NULL)
  204. return -ENOMEM;
  205. unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
  206. if (unicode_buf == NULL) {
  207. kfree(ucase_buf);
  208. return -ENOMEM;
  209. }
  210. for (i = 0; i < user_name_len; i++)
  211. ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
  212. ucase_buf[i] = 0;
  213. user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf,
  214. MAX_USERNAME_SIZE*2, nls_info);
  215. unicode_buf[user_name_len] = 0;
  216. user_name_len++;
  217. for (i = 0; i < dom_name_len; i++)
  218. ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
  219. ucase_buf[i] = 0;
  220. dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf,
  221. MAX_USERNAME_SIZE*2, nls_info);
  222. unicode_buf[user_name_len + dom_name_len] = 0;
  223. hmac_md5_update((const unsigned char *) unicode_buf,
  224. (user_name_len+dom_name_len)*2, &ctx);
  225. hmac_md5_final(ses->server->ntlmv2_hash, &ctx);
  226. kfree(ucase_buf);
  227. kfree(unicode_buf);
  228. return 0;
  229. }
  230. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  231. void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  232. char *lnm_session_key)
  233. {
  234. int i;
  235. char password_with_pad[CIFS_ENCPWD_SIZE];
  236. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  237. if (password)
  238. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  239. if (!encrypt && extended_security & CIFSSEC_MAY_PLNTXT) {
  240. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  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. SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  258. /* clear password before we return/free memory */
  259. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  260. }
  261. #endif /* CIFS_WEAK_PW_HASH */
  262. static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
  263. const struct nls_table *nls_cp)
  264. {
  265. int rc = 0;
  266. int len;
  267. char nt_hash[16];
  268. struct HMACMD5Context *pctxt;
  269. wchar_t *user;
  270. wchar_t *domain;
  271. pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL);
  272. if (pctxt == NULL)
  273. return -ENOMEM;
  274. /* calculate md4 hash of password */
  275. E_md4hash(ses->password, nt_hash);
  276. /* convert Domainname to unicode and uppercase */
  277. hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
  278. /* convert ses->userName to unicode and uppercase */
  279. len = strlen(ses->userName);
  280. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  281. if (user == NULL)
  282. goto calc_exit_2;
  283. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  284. UniStrupr(user);
  285. hmac_md5_update((char *)user, 2*len, pctxt);
  286. /* convert ses->domainName to unicode and uppercase */
  287. if (ses->domainName) {
  288. len = strlen(ses->domainName);
  289. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  290. if (domain == NULL)
  291. goto calc_exit_1;
  292. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  293. nls_cp);
  294. /* the following line was removed since it didn't work well
  295. with lower cased domain name that passed as an option.
  296. Maybe converting the domain name earlier makes sense */
  297. /* UniStrupr(domain); */
  298. hmac_md5_update((char *)domain, 2*len, pctxt);
  299. kfree(domain);
  300. }
  301. calc_exit_1:
  302. kfree(user);
  303. calc_exit_2:
  304. /* BB FIXME what about bytes 24 through 40 of the signing key?
  305. compare with the NTLM example */
  306. hmac_md5_final(ses->server->ntlmv2_hash, pctxt);
  307. return rc;
  308. }
  309. void setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
  310. const struct nls_table *nls_cp)
  311. {
  312. int rc;
  313. struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
  314. struct HMACMD5Context context;
  315. buf->blob_signature = cpu_to_le32(0x00000101);
  316. buf->reserved = 0;
  317. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  318. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  319. buf->reserved2 = 0;
  320. buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
  321. buf->names[0].length = 0;
  322. buf->names[1].type = 0;
  323. buf->names[1].length = 0;
  324. /* calculate buf->ntlmv2_hash */
  325. rc = calc_ntlmv2_hash(ses, nls_cp);
  326. if (rc)
  327. cERROR(1, ("could not get v2 hash rc %d", rc));
  328. CalcNTLMv2_response(ses, resp_buf);
  329. /* now calculate the MAC key for NTLMv2 */
  330. hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
  331. hmac_md5_update(resp_buf, 16, &context);
  332. hmac_md5_final(ses->server->mac_signing_key.data.ntlmv2.key, &context);
  333. memcpy(&ses->server->mac_signing_key.data.ntlmv2.resp, resp_buf,
  334. sizeof(struct ntlmv2_resp));
  335. ses->server->mac_signing_key.len = 16 + sizeof(struct ntlmv2_resp);
  336. }
  337. void CalcNTLMv2_response(const struct cifsSesInfo *ses,
  338. char *v2_session_response)
  339. {
  340. struct HMACMD5Context context;
  341. /* rest of v2 struct already generated */
  342. memcpy(v2_session_response + 8, ses->server->cryptKey, 8);
  343. hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
  344. hmac_md5_update(v2_session_response+8,
  345. sizeof(struct ntlmv2_resp) - 8, &context);
  346. hmac_md5_final(v2_session_response, &context);
  347. /* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
  348. }