cifsencrypt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "ntlmssp.h"
  30. #include <linux/ctype.h>
  31. #include <linux/random.h>
  32. /* Calculate and return the CIFS signature based on the mac key and SMB PDU */
  33. /* the 16 byte signature must be allocated by the caller */
  34. /* Note we only use the 1st eight bytes */
  35. /* Note that the smb header signature field on input contains the
  36. sequence number before this function is called */
  37. extern void mdfour(unsigned char *out, unsigned char *in, int n);
  38. extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
  39. extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
  40. unsigned char *p24);
  41. static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
  42. const struct session_key *key, char *signature)
  43. {
  44. struct MD5Context context;
  45. if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL))
  46. return -EINVAL;
  47. cifs_MD5_init(&context);
  48. cifs_MD5_update(&context, (char *)&key->data, key->len);
  49. cifs_MD5_update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
  50. cifs_MD5_final(signature, &context);
  51. return 0;
  52. }
  53. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  54. __u32 *pexpected_response_sequence_number)
  55. {
  56. int rc = 0;
  57. char smb_signature[20];
  58. if ((cifs_pdu == NULL) || (server == NULL))
  59. return -EINVAL;
  60. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  61. return rc;
  62. spin_lock(&GlobalMid_Lock);
  63. cifs_pdu->Signature.Sequence.SequenceNumber =
  64. cpu_to_le32(server->sequence_number);
  65. cifs_pdu->Signature.Sequence.Reserved = 0;
  66. *pexpected_response_sequence_number = server->sequence_number++;
  67. server->sequence_number++;
  68. spin_unlock(&GlobalMid_Lock);
  69. rc = cifs_calculate_signature(cifs_pdu, &server->session_key,
  70. smb_signature);
  71. if (rc)
  72. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  73. else
  74. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  75. return rc;
  76. }
  77. static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
  78. const struct session_key *key, char *signature)
  79. {
  80. struct MD5Context context;
  81. int i;
  82. if ((iov == NULL) || (signature == NULL) || (key == NULL))
  83. return -EINVAL;
  84. cifs_MD5_init(&context);
  85. cifs_MD5_update(&context, (char *)&key->data, key->len);
  86. for (i = 0; i < n_vec; i++) {
  87. if (iov[i].iov_len == 0)
  88. continue;
  89. if (iov[i].iov_base == NULL) {
  90. cERROR(1, "null iovec entry");
  91. return -EIO;
  92. }
  93. /* The first entry includes a length field (which does not get
  94. signed that occupies the first 4 bytes before the header */
  95. if (i == 0) {
  96. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  97. break; /* nothing to sign or corrupt header */
  98. cifs_MD5_update(&context, iov[0].iov_base+4,
  99. iov[0].iov_len-4);
  100. } else
  101. cifs_MD5_update(&context, iov[i].iov_base, iov[i].iov_len);
  102. }
  103. cifs_MD5_final(signature, &context);
  104. return 0;
  105. }
  106. int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  107. __u32 *pexpected_response_sequence_number)
  108. {
  109. int rc = 0;
  110. char smb_signature[20];
  111. struct smb_hdr *cifs_pdu = iov[0].iov_base;
  112. if ((cifs_pdu == NULL) || (server == NULL))
  113. return -EINVAL;
  114. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  115. return rc;
  116. spin_lock(&GlobalMid_Lock);
  117. cifs_pdu->Signature.Sequence.SequenceNumber =
  118. cpu_to_le32(server->sequence_number);
  119. cifs_pdu->Signature.Sequence.Reserved = 0;
  120. *pexpected_response_sequence_number = server->sequence_number++;
  121. server->sequence_number++;
  122. spin_unlock(&GlobalMid_Lock);
  123. rc = cifs_calc_signature2(iov, n_vec, &server->session_key,
  124. smb_signature);
  125. if (rc)
  126. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  127. else
  128. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  129. return rc;
  130. }
  131. int cifs_verify_signature(struct smb_hdr *cifs_pdu,
  132. const struct session_key *session_key,
  133. __u32 expected_sequence_number)
  134. {
  135. unsigned int rc;
  136. char server_response_sig[8];
  137. char what_we_think_sig_should_be[20];
  138. if (cifs_pdu == NULL || session_key == NULL)
  139. return -EINVAL;
  140. if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
  141. return 0;
  142. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  143. struct smb_com_lock_req *pSMB =
  144. (struct smb_com_lock_req *)cifs_pdu;
  145. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  146. return 0;
  147. }
  148. /* BB what if signatures are supposed to be on for session but
  149. server does not send one? BB */
  150. /* Do not need to verify session setups with signature "BSRSPYL " */
  151. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  152. cFYI(1, "dummy signature received for smb command 0x%x",
  153. cifs_pdu->Command);
  154. /* save off the origiginal signature so we can modify the smb and check
  155. its signature against what the server sent */
  156. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  157. cifs_pdu->Signature.Sequence.SequenceNumber =
  158. cpu_to_le32(expected_sequence_number);
  159. cifs_pdu->Signature.Sequence.Reserved = 0;
  160. rc = cifs_calculate_signature(cifs_pdu, session_key,
  161. what_we_think_sig_should_be);
  162. if (rc)
  163. return rc;
  164. /* cifs_dump_mem("what we think it should be: ",
  165. what_we_think_sig_should_be, 16); */
  166. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  167. return -EACCES;
  168. else
  169. return 0;
  170. }
  171. /* We fill in key by putting in 40 byte array which was allocated by caller */
  172. int cifs_calculate_session_key(struct session_key *key, const char *rn,
  173. const char *password)
  174. {
  175. char temp_key[16];
  176. if ((key == NULL) || (rn == NULL))
  177. return -EINVAL;
  178. E_md4hash(password, temp_key);
  179. mdfour(key->data.ntlm, temp_key, 16);
  180. memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
  181. key->len = 40;
  182. return 0;
  183. }
  184. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  185. void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  186. char *lnm_session_key)
  187. {
  188. int i;
  189. char password_with_pad[CIFS_ENCPWD_SIZE];
  190. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  191. if (password)
  192. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  193. if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
  194. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  195. memcpy(lnm_session_key, password_with_pad,
  196. CIFS_ENCPWD_SIZE);
  197. return;
  198. }
  199. /* calculate old style session key */
  200. /* calling toupper is less broken than repeatedly
  201. calling nls_toupper would be since that will never
  202. work for UTF8, but neither handles multibyte code pages
  203. but the only alternative would be converting to UCS-16 (Unicode)
  204. (using a routine something like UniStrupr) then
  205. uppercasing and then converting back from Unicode - which
  206. would only worth doing it if we knew it were utf8. Basically
  207. utf8 and other multibyte codepages each need their own strupper
  208. function since a byte at a time will ont work. */
  209. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  210. password_with_pad[i] = toupper(password_with_pad[i]);
  211. SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  212. /* clear password before we return/free memory */
  213. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  214. }
  215. #endif /* CIFS_WEAK_PW_HASH */
  216. /* This is just a filler for ntlmv2 type of security mechanisms.
  217. * Older servers are not very particular about the contents of av pairs
  218. * in the blob and for sec mechs like ntlmv2, there is no negotiation
  219. * as in ntlmssp, so unless domain and server netbios and dns names
  220. * are specified, there is no way to obtain name. In case of ntlmssp,
  221. * server provides that info in type 2 challenge packet
  222. */
  223. static int
  224. build_avpair_blob(struct cifsSesInfo *ses)
  225. {
  226. struct ntlmssp2_name *attrptr;
  227. ses->tilen = 2 * sizeof(struct ntlmssp2_name);
  228. ses->tiblob = kzalloc(ses->tilen, GFP_KERNEL);
  229. if (!ses->tiblob) {
  230. ses->tilen = 0;
  231. cERROR(1, "Challenge target info allocation failure");
  232. return -ENOMEM;
  233. }
  234. attrptr = (struct ntlmssp2_name *) ses->tiblob;
  235. attrptr->type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
  236. return 0;
  237. }
  238. /* Server has provided av pairs/target info in the type 2 challenge
  239. * packet and we have plucked it and stored within smb session.
  240. * We parse that blob here to find netbios domain name to be used
  241. * as part of ntlmv2 authentication (in Target String), if not already
  242. * specified on the command line.
  243. * If this function returns without any error but without fetching
  244. * domain name, authentication may fail against some server but
  245. * may not fail against other (those who are not very particular
  246. * about target string i.e. for some, just user name might suffice.
  247. */
  248. static int
  249. find_domain_name(struct cifsSesInfo *ses)
  250. {
  251. unsigned int attrsize;
  252. unsigned int type;
  253. unsigned int onesize = sizeof(struct ntlmssp2_name);
  254. unsigned char *blobptr;
  255. unsigned char *blobend;
  256. struct ntlmssp2_name *attrptr;
  257. if (!ses->tilen || !ses->tiblob)
  258. return 0;
  259. blobptr = ses->tiblob;
  260. blobend = ses->tiblob + ses->tilen;
  261. while (blobptr + onesize < blobend) {
  262. attrptr = (struct ntlmssp2_name *) blobptr;
  263. type = le16_to_cpu(attrptr->type);
  264. if (type == NTLMSSP_AV_EOL)
  265. break;
  266. blobptr += 2; /* advance attr type */
  267. attrsize = le16_to_cpu(attrptr->length);
  268. blobptr += 2; /* advance attr size */
  269. if (blobptr + attrsize > blobend)
  270. break;
  271. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  272. if (!attrsize)
  273. break;
  274. if (!ses->domainName) {
  275. struct nls_table *default_nls;
  276. ses->domainName =
  277. kmalloc(attrsize + 1, GFP_KERNEL);
  278. if (!ses->domainName)
  279. return -ENOMEM;
  280. default_nls = load_nls_default();
  281. cifs_from_ucs2(ses->domainName,
  282. (__le16 *)blobptr, attrsize, attrsize,
  283. default_nls, false);
  284. unload_nls(default_nls);
  285. break;
  286. }
  287. }
  288. blobptr += attrsize; /* advance attr value */
  289. }
  290. return 0;
  291. }
  292. static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
  293. const struct nls_table *nls_cp)
  294. {
  295. int rc = 0;
  296. int len;
  297. char nt_hash[16];
  298. struct HMACMD5Context *pctxt;
  299. wchar_t *user;
  300. wchar_t *domain;
  301. pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL);
  302. if (pctxt == NULL)
  303. return -ENOMEM;
  304. /* calculate md4 hash of password */
  305. E_md4hash(ses->password, nt_hash);
  306. /* convert Domainname to unicode and uppercase */
  307. hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
  308. /* convert ses->userName to unicode and uppercase */
  309. len = strlen(ses->userName);
  310. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  311. if (user == NULL)
  312. goto calc_exit_2;
  313. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  314. UniStrupr(user);
  315. hmac_md5_update((char *)user, 2*len, pctxt);
  316. /* convert ses->domainName to unicode and uppercase */
  317. if (ses->domainName) {
  318. len = strlen(ses->domainName);
  319. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  320. if (domain == NULL)
  321. goto calc_exit_1;
  322. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  323. nls_cp);
  324. /* the following line was removed since it didn't work well
  325. with lower cased domain name that passed as an option.
  326. Maybe converting the domain name earlier makes sense */
  327. /* UniStrupr(domain); */
  328. hmac_md5_update((char *)domain, 2*len, pctxt);
  329. kfree(domain);
  330. }
  331. calc_exit_1:
  332. kfree(user);
  333. calc_exit_2:
  334. /* BB FIXME what about bytes 24 through 40 of the signing key?
  335. compare with the NTLM example */
  336. hmac_md5_final(ses->server->ntlmv2_hash, pctxt);
  337. kfree(pctxt);
  338. return rc;
  339. }
  340. int
  341. setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
  342. const struct nls_table *nls_cp)
  343. {
  344. int rc;
  345. struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
  346. struct HMACMD5Context context;
  347. buf->blob_signature = cpu_to_le32(0x00000101);
  348. buf->reserved = 0;
  349. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  350. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  351. buf->reserved2 = 0;
  352. if (ses->server->secType == RawNTLMSSP) {
  353. if (!ses->domainName) {
  354. rc = find_domain_name(ses);
  355. if (rc) {
  356. cERROR(1, "error %d finding domain name", rc);
  357. goto setup_ntlmv2_rsp_ret;
  358. }
  359. }
  360. } else {
  361. rc = build_avpair_blob(ses);
  362. if (rc) {
  363. cERROR(1, "error %d building av pair blob", rc);
  364. return rc;
  365. }
  366. }
  367. /* calculate buf->ntlmv2_hash */
  368. rc = calc_ntlmv2_hash(ses, nls_cp);
  369. if (rc) {
  370. cERROR(1, "could not get v2 hash rc %d", rc);
  371. goto setup_ntlmv2_rsp_ret;
  372. }
  373. CalcNTLMv2_response(ses, resp_buf);
  374. /* now calculate the MAC key for NTLMv2 */
  375. hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
  376. hmac_md5_update(resp_buf, 16, &context);
  377. hmac_md5_final(ses->server->session_key.data.ntlmv2.key, &context);
  378. memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
  379. sizeof(struct ntlmv2_resp));
  380. ses->server->session_key.len = 16 + sizeof(struct ntlmv2_resp);
  381. return 0;
  382. setup_ntlmv2_rsp_ret:
  383. kfree(ses->tiblob);
  384. ses->tiblob = NULL;
  385. ses->tilen = 0;
  386. return rc;
  387. }
  388. void CalcNTLMv2_response(const struct cifsSesInfo *ses,
  389. char *v2_session_response)
  390. {
  391. struct HMACMD5Context context;
  392. /* rest of v2 struct already generated */
  393. memcpy(v2_session_response + 8, ses->server->cryptKey, 8);
  394. hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context);
  395. hmac_md5_update(v2_session_response+8,
  396. sizeof(struct ntlmv2_resp) - 8, &context);
  397. if (ses->tilen)
  398. hmac_md5_update(ses->tiblob, ses->tilen, &context);
  399. hmac_md5_final(v2_session_response, &context);
  400. /* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
  401. }