cifsencrypt.c 11 KB

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