cifsencrypt.c 12 KB

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