cifsencrypt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * fs/cifs/cifsencrypt.c
  3. *
  4. * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP
  5. * for more detailed information
  6. *
  7. * Copyright (C) International Business Machines Corp., 2005,2013
  8. * Author(s): Steve French (sfrench@us.ibm.com)
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifs_debug.h"
  29. #include "cifs_unicode.h"
  30. #include "cifsproto.h"
  31. #include "ntlmssp.h"
  32. #include <linux/ctype.h>
  33. #include <linux/random.h>
  34. #include <linux/highmem.h>
  35. static int
  36. cifs_crypto_shash_md5_allocate(struct TCP_Server_Info *server)
  37. {
  38. int rc;
  39. unsigned int size;
  40. if (server->secmech.sdescmd5 != NULL)
  41. return 0; /* already allocated */
  42. server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
  43. if (IS_ERR(server->secmech.md5)) {
  44. cifs_dbg(VFS, "could not allocate crypto md5\n");
  45. rc = PTR_ERR(server->secmech.md5);
  46. server->secmech.md5 = NULL;
  47. return rc;
  48. }
  49. size = sizeof(struct shash_desc) +
  50. crypto_shash_descsize(server->secmech.md5);
  51. server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
  52. if (!server->secmech.sdescmd5) {
  53. crypto_free_shash(server->secmech.md5);
  54. server->secmech.md5 = NULL;
  55. return -ENOMEM;
  56. }
  57. server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
  58. server->secmech.sdescmd5->shash.flags = 0x0;
  59. return 0;
  60. }
  61. /*
  62. * Calculate and return the CIFS signature based on the mac key and SMB PDU.
  63. * The 16 byte signature must be allocated by the caller. Note we only use the
  64. * 1st eight bytes and that the smb header signature field on input contains
  65. * the sequence number before this function is called. Also, this function
  66. * should be called with the server->srv_mutex held.
  67. */
  68. static int cifs_calc_signature(struct smb_rqst *rqst,
  69. struct TCP_Server_Info *server, char *signature)
  70. {
  71. int i;
  72. int rc;
  73. struct kvec *iov = rqst->rq_iov;
  74. int n_vec = rqst->rq_nvec;
  75. if (iov == NULL || signature == NULL || server == NULL)
  76. return -EINVAL;
  77. if (!server->secmech.sdescmd5) {
  78. rc = cifs_crypto_shash_md5_allocate(server);
  79. if (rc) {
  80. cifs_dbg(VFS, "%s: Can't alloc md5 crypto\n", __func__);
  81. return -1;
  82. }
  83. }
  84. rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
  85. if (rc) {
  86. cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
  87. return rc;
  88. }
  89. rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
  90. server->session_key.response, server->session_key.len);
  91. if (rc) {
  92. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  93. return rc;
  94. }
  95. for (i = 0; i < n_vec; i++) {
  96. if (iov[i].iov_len == 0)
  97. continue;
  98. if (iov[i].iov_base == NULL) {
  99. cifs_dbg(VFS, "null iovec entry\n");
  100. return -EIO;
  101. }
  102. /* The first entry includes a length field (which does not get
  103. signed that occupies the first 4 bytes before the header */
  104. if (i == 0) {
  105. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  106. break; /* nothing to sign or corrupt header */
  107. rc =
  108. crypto_shash_update(&server->secmech.sdescmd5->shash,
  109. iov[i].iov_base + 4, iov[i].iov_len - 4);
  110. } else {
  111. rc =
  112. crypto_shash_update(&server->secmech.sdescmd5->shash,
  113. iov[i].iov_base, iov[i].iov_len);
  114. }
  115. if (rc) {
  116. cifs_dbg(VFS, "%s: Could not update with payload\n",
  117. __func__);
  118. return rc;
  119. }
  120. }
  121. /* now hash over the rq_pages array */
  122. for (i = 0; i < rqst->rq_npages; i++) {
  123. struct kvec p_iov;
  124. cifs_rqst_page_to_kvec(rqst, i, &p_iov);
  125. crypto_shash_update(&server->secmech.sdescmd5->shash,
  126. p_iov.iov_base, p_iov.iov_len);
  127. kunmap(rqst->rq_pages[i]);
  128. }
  129. rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
  130. if (rc)
  131. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  132. return rc;
  133. }
  134. /* must be called with server->srv_mutex held */
  135. int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
  136. __u32 *pexpected_response_sequence_number)
  137. {
  138. int rc = 0;
  139. char smb_signature[20];
  140. struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  141. if ((cifs_pdu == NULL) || (server == NULL))
  142. return -EINVAL;
  143. if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
  144. server->tcpStatus == CifsNeedNegotiate)
  145. return rc;
  146. if (!server->session_estab) {
  147. memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
  148. return rc;
  149. }
  150. cifs_pdu->Signature.Sequence.SequenceNumber =
  151. cpu_to_le32(server->sequence_number);
  152. cifs_pdu->Signature.Sequence.Reserved = 0;
  153. *pexpected_response_sequence_number = ++server->sequence_number;
  154. ++server->sequence_number;
  155. rc = cifs_calc_signature(rqst, server, smb_signature);
  156. if (rc)
  157. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  158. else
  159. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  160. return rc;
  161. }
  162. int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  163. __u32 *pexpected_response_sequence)
  164. {
  165. struct smb_rqst rqst = { .rq_iov = iov,
  166. .rq_nvec = n_vec };
  167. return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
  168. }
  169. /* must be called with server->srv_mutex held */
  170. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  171. __u32 *pexpected_response_sequence_number)
  172. {
  173. struct kvec iov;
  174. iov.iov_base = cifs_pdu;
  175. iov.iov_len = be32_to_cpu(cifs_pdu->smb_buf_length) + 4;
  176. return cifs_sign_smbv(&iov, 1, server,
  177. pexpected_response_sequence_number);
  178. }
  179. int cifs_verify_signature(struct smb_rqst *rqst,
  180. struct TCP_Server_Info *server,
  181. __u32 expected_sequence_number)
  182. {
  183. unsigned int rc;
  184. char server_response_sig[8];
  185. char what_we_think_sig_should_be[20];
  186. struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  187. if (cifs_pdu == NULL || server == NULL)
  188. return -EINVAL;
  189. if (!server->session_estab)
  190. return 0;
  191. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  192. struct smb_com_lock_req *pSMB =
  193. (struct smb_com_lock_req *)cifs_pdu;
  194. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  195. return 0;
  196. }
  197. /* BB what if signatures are supposed to be on for session but
  198. server does not send one? BB */
  199. /* Do not need to verify session setups with signature "BSRSPYL " */
  200. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  201. cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
  202. cifs_pdu->Command);
  203. /* save off the origiginal signature so we can modify the smb and check
  204. its signature against what the server sent */
  205. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  206. cifs_pdu->Signature.Sequence.SequenceNumber =
  207. cpu_to_le32(expected_sequence_number);
  208. cifs_pdu->Signature.Sequence.Reserved = 0;
  209. mutex_lock(&server->srv_mutex);
  210. rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
  211. mutex_unlock(&server->srv_mutex);
  212. if (rc)
  213. return rc;
  214. /* cifs_dump_mem("what we think it should be: ",
  215. what_we_think_sig_should_be, 16); */
  216. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  217. return -EACCES;
  218. else
  219. return 0;
  220. }
  221. /* first calculate 24 bytes ntlm response and then 16 byte session key */
  222. int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
  223. {
  224. int rc = 0;
  225. unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
  226. char temp_key[CIFS_SESS_KEY_SIZE];
  227. if (!ses)
  228. return -EINVAL;
  229. ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
  230. if (!ses->auth_key.response)
  231. return -ENOMEM;
  232. ses->auth_key.len = temp_len;
  233. rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
  234. ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
  235. if (rc) {
  236. cifs_dbg(FYI, "%s Can't generate NTLM response, error: %d\n",
  237. __func__, rc);
  238. return rc;
  239. }
  240. rc = E_md4hash(ses->password, temp_key, nls_cp);
  241. if (rc) {
  242. cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
  243. __func__, rc);
  244. return rc;
  245. }
  246. rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
  247. if (rc)
  248. cifs_dbg(FYI, "%s Can't generate NTLM session key, error: %d\n",
  249. __func__, rc);
  250. return rc;
  251. }
  252. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  253. int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  254. char *lnm_session_key)
  255. {
  256. int i;
  257. int rc;
  258. char password_with_pad[CIFS_ENCPWD_SIZE] = {0};
  259. if (password)
  260. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  261. if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
  262. memcpy(lnm_session_key, password_with_pad,
  263. CIFS_ENCPWD_SIZE);
  264. return 0;
  265. }
  266. /* calculate old style session key */
  267. /* calling toupper is less broken than repeatedly
  268. calling nls_toupper would be since that will never
  269. work for UTF8, but neither handles multibyte code pages
  270. but the only alternative would be converting to UCS-16 (Unicode)
  271. (using a routine something like UniStrupr) then
  272. uppercasing and then converting back from Unicode - which
  273. would only worth doing it if we knew it were utf8. Basically
  274. utf8 and other multibyte codepages each need their own strupper
  275. function since a byte at a time will ont work. */
  276. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  277. password_with_pad[i] = toupper(password_with_pad[i]);
  278. rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  279. return rc;
  280. }
  281. #endif /* CIFS_WEAK_PW_HASH */
  282. /* Build a proper attribute value/target info pairs blob.
  283. * Fill in netbios and dns domain name and workstation name
  284. * and client time (total five av pairs and + one end of fields indicator.
  285. * Allocate domain name which gets freed when session struct is deallocated.
  286. */
  287. static int
  288. build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
  289. {
  290. unsigned int dlen;
  291. unsigned int size = 2 * sizeof(struct ntlmssp2_name);
  292. char *defdmname = "WORKGROUP";
  293. unsigned char *blobptr;
  294. struct ntlmssp2_name *attrptr;
  295. if (!ses->domainName) {
  296. ses->domainName = kstrdup(defdmname, GFP_KERNEL);
  297. if (!ses->domainName)
  298. return -ENOMEM;
  299. }
  300. dlen = strlen(ses->domainName);
  301. /*
  302. * The length of this blob is two times the size of a
  303. * structure (av pair) which holds name/size
  304. * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
  305. * unicode length of a netbios domain name
  306. */
  307. ses->auth_key.len = size + 2 * dlen;
  308. ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
  309. if (!ses->auth_key.response) {
  310. ses->auth_key.len = 0;
  311. return -ENOMEM;
  312. }
  313. blobptr = ses->auth_key.response;
  314. attrptr = (struct ntlmssp2_name *) blobptr;
  315. /*
  316. * As defined in MS-NTLM 3.3.2, just this av pair field
  317. * is sufficient as part of the temp
  318. */
  319. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
  320. attrptr->length = cpu_to_le16(2 * dlen);
  321. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  322. cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  323. return 0;
  324. }
  325. /* Server has provided av pairs/target info in the type 2 challenge
  326. * packet and we have plucked it and stored within smb session.
  327. * We parse that blob here to find netbios domain name to be used
  328. * as part of ntlmv2 authentication (in Target String), if not already
  329. * specified on the command line.
  330. * If this function returns without any error but without fetching
  331. * domain name, authentication may fail against some server but
  332. * may not fail against other (those who are not very particular
  333. * about target string i.e. for some, just user name might suffice.
  334. */
  335. static int
  336. find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
  337. {
  338. unsigned int attrsize;
  339. unsigned int type;
  340. unsigned int onesize = sizeof(struct ntlmssp2_name);
  341. unsigned char *blobptr;
  342. unsigned char *blobend;
  343. struct ntlmssp2_name *attrptr;
  344. if (!ses->auth_key.len || !ses->auth_key.response)
  345. return 0;
  346. blobptr = ses->auth_key.response;
  347. blobend = blobptr + ses->auth_key.len;
  348. while (blobptr + onesize < blobend) {
  349. attrptr = (struct ntlmssp2_name *) blobptr;
  350. type = le16_to_cpu(attrptr->type);
  351. if (type == NTLMSSP_AV_EOL)
  352. break;
  353. blobptr += 2; /* advance attr type */
  354. attrsize = le16_to_cpu(attrptr->length);
  355. blobptr += 2; /* advance attr size */
  356. if (blobptr + attrsize > blobend)
  357. break;
  358. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  359. if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
  360. break;
  361. if (!ses->domainName) {
  362. ses->domainName =
  363. kmalloc(attrsize + 1, GFP_KERNEL);
  364. if (!ses->domainName)
  365. return -ENOMEM;
  366. cifs_from_utf16(ses->domainName,
  367. (__le16 *)blobptr, attrsize, attrsize,
  368. nls_cp, NO_MAP_UNI_RSVD);
  369. break;
  370. }
  371. }
  372. blobptr += attrsize; /* advance attr value */
  373. }
  374. return 0;
  375. }
  376. /* Server has provided av pairs/target info in the type 2 challenge
  377. * packet and we have plucked it and stored within smb session.
  378. * We parse that blob here to find the server given timestamp
  379. * as part of ntlmv2 authentication (or local current time as
  380. * default in case of failure)
  381. */
  382. static __le64
  383. find_timestamp(struct cifs_ses *ses)
  384. {
  385. unsigned int attrsize;
  386. unsigned int type;
  387. unsigned int onesize = sizeof(struct ntlmssp2_name);
  388. unsigned char *blobptr;
  389. unsigned char *blobend;
  390. struct ntlmssp2_name *attrptr;
  391. if (!ses->auth_key.len || !ses->auth_key.response)
  392. return 0;
  393. blobptr = ses->auth_key.response;
  394. blobend = blobptr + ses->auth_key.len;
  395. while (blobptr + onesize < blobend) {
  396. attrptr = (struct ntlmssp2_name *) blobptr;
  397. type = le16_to_cpu(attrptr->type);
  398. if (type == NTLMSSP_AV_EOL)
  399. break;
  400. blobptr += 2; /* advance attr type */
  401. attrsize = le16_to_cpu(attrptr->length);
  402. blobptr += 2; /* advance attr size */
  403. if (blobptr + attrsize > blobend)
  404. break;
  405. if (type == NTLMSSP_AV_TIMESTAMP) {
  406. if (attrsize == sizeof(u64))
  407. return *((__le64 *)blobptr);
  408. }
  409. blobptr += attrsize; /* advance attr value */
  410. }
  411. return cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  412. }
  413. static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
  414. const struct nls_table *nls_cp)
  415. {
  416. int rc = 0;
  417. int len;
  418. char nt_hash[CIFS_NTHASH_SIZE];
  419. __le16 *user;
  420. wchar_t *domain;
  421. wchar_t *server;
  422. if (!ses->server->secmech.sdeschmacmd5) {
  423. cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
  424. return -1;
  425. }
  426. /* calculate md4 hash of password */
  427. E_md4hash(ses->password, nt_hash, nls_cp);
  428. rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
  429. CIFS_NTHASH_SIZE);
  430. if (rc) {
  431. cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
  432. return rc;
  433. }
  434. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  435. if (rc) {
  436. cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
  437. return rc;
  438. }
  439. /* convert ses->user_name to unicode */
  440. len = ses->user_name ? strlen(ses->user_name) : 0;
  441. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  442. if (user == NULL) {
  443. rc = -ENOMEM;
  444. return rc;
  445. }
  446. if (len) {
  447. len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
  448. UniStrupr(user);
  449. } else {
  450. memset(user, '\0', 2);
  451. }
  452. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  453. (char *)user, 2 * len);
  454. kfree(user);
  455. if (rc) {
  456. cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
  457. return rc;
  458. }
  459. /* convert ses->domainName to unicode and uppercase */
  460. if (ses->domainName) {
  461. len = strlen(ses->domainName);
  462. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  463. if (domain == NULL) {
  464. rc = -ENOMEM;
  465. return rc;
  466. }
  467. len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
  468. nls_cp);
  469. rc =
  470. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  471. (char *)domain, 2 * len);
  472. kfree(domain);
  473. if (rc) {
  474. cifs_dbg(VFS, "%s: Could not update with domain\n",
  475. __func__);
  476. return rc;
  477. }
  478. } else {
  479. /* We use ses->serverName if no domain name available */
  480. len = strlen(ses->serverName);
  481. server = kmalloc(2 + (len * 2), GFP_KERNEL);
  482. if (server == NULL) {
  483. rc = -ENOMEM;
  484. return rc;
  485. }
  486. len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
  487. nls_cp);
  488. rc =
  489. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  490. (char *)server, 2 * len);
  491. kfree(server);
  492. if (rc) {
  493. cifs_dbg(VFS, "%s: Could not update with server\n",
  494. __func__);
  495. return rc;
  496. }
  497. }
  498. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  499. ntlmv2_hash);
  500. if (rc)
  501. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  502. return rc;
  503. }
  504. static int
  505. CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
  506. {
  507. int rc;
  508. struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
  509. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  510. unsigned int hash_len;
  511. /* The MD5 hash starts at challenge_key.key */
  512. hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
  513. offsetof(struct ntlmv2_resp, challenge.key[0]));
  514. if (!ses->server->secmech.sdeschmacmd5) {
  515. cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
  516. return -1;
  517. }
  518. rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
  519. ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  520. if (rc) {
  521. cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
  522. __func__);
  523. return rc;
  524. }
  525. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  526. if (rc) {
  527. cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
  528. return rc;
  529. }
  530. if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
  531. memcpy(ntlmv2->challenge.key,
  532. ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  533. else
  534. memcpy(ntlmv2->challenge.key,
  535. ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  536. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  537. ntlmv2->challenge.key, hash_len);
  538. if (rc) {
  539. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  540. return rc;
  541. }
  542. /* Note that the MD5 digest over writes anon.challenge_key.key */
  543. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  544. ntlmv2->ntlmv2_hash);
  545. if (rc)
  546. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  547. return rc;
  548. }
  549. static int crypto_hmacmd5_alloc(struct TCP_Server_Info *server)
  550. {
  551. int rc;
  552. unsigned int size;
  553. /* check if already allocated */
  554. if (server->secmech.sdeschmacmd5)
  555. return 0;
  556. server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
  557. if (IS_ERR(server->secmech.hmacmd5)) {
  558. cifs_dbg(VFS, "could not allocate crypto hmacmd5\n");
  559. rc = PTR_ERR(server->secmech.hmacmd5);
  560. server->secmech.hmacmd5 = NULL;
  561. return rc;
  562. }
  563. size = sizeof(struct shash_desc) +
  564. crypto_shash_descsize(server->secmech.hmacmd5);
  565. server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
  566. if (!server->secmech.sdeschmacmd5) {
  567. crypto_free_shash(server->secmech.hmacmd5);
  568. server->secmech.hmacmd5 = NULL;
  569. return -ENOMEM;
  570. }
  571. server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
  572. server->secmech.sdeschmacmd5->shash.flags = 0x0;
  573. return 0;
  574. }
  575. int
  576. setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
  577. {
  578. int rc;
  579. int baselen;
  580. unsigned int tilen;
  581. struct ntlmv2_resp *ntlmv2;
  582. char ntlmv2_hash[16];
  583. unsigned char *tiblob = NULL; /* target info blob */
  584. __le64 rsp_timestamp;
  585. if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
  586. if (!ses->domainName) {
  587. rc = find_domain_name(ses, nls_cp);
  588. if (rc) {
  589. cifs_dbg(VFS, "error %d finding domain name\n",
  590. rc);
  591. goto setup_ntlmv2_rsp_ret;
  592. }
  593. }
  594. } else {
  595. rc = build_avpair_blob(ses, nls_cp);
  596. if (rc) {
  597. cifs_dbg(VFS, "error %d building av pair blob\n", rc);
  598. goto setup_ntlmv2_rsp_ret;
  599. }
  600. }
  601. /* Must be within 5 minutes of the server (or in range +/-2h
  602. * in case of Mac OS X), so simply carry over server timestamp
  603. * (as Windows 7 does)
  604. */
  605. rsp_timestamp = find_timestamp(ses);
  606. baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
  607. tilen = ses->auth_key.len;
  608. tiblob = ses->auth_key.response;
  609. ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
  610. if (!ses->auth_key.response) {
  611. rc = -ENOMEM;
  612. ses->auth_key.len = 0;
  613. goto setup_ntlmv2_rsp_ret;
  614. }
  615. ses->auth_key.len += baselen;
  616. ntlmv2 = (struct ntlmv2_resp *)
  617. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  618. ntlmv2->blob_signature = cpu_to_le32(0x00000101);
  619. ntlmv2->reserved = 0;
  620. ntlmv2->time = rsp_timestamp;
  621. get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
  622. ntlmv2->reserved2 = 0;
  623. memcpy(ses->auth_key.response + baselen, tiblob, tilen);
  624. mutex_lock(&ses->server->srv_mutex);
  625. rc = crypto_hmacmd5_alloc(ses->server);
  626. if (rc) {
  627. cifs_dbg(VFS, "could not crypto alloc hmacmd5 rc %d\n", rc);
  628. goto unlock;
  629. }
  630. /* calculate ntlmv2_hash */
  631. rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
  632. if (rc) {
  633. cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
  634. goto unlock;
  635. }
  636. /* calculate first part of the client response (CR1) */
  637. rc = CalcNTLMv2_response(ses, ntlmv2_hash);
  638. if (rc) {
  639. cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
  640. goto unlock;
  641. }
  642. /* now calculate the session key for NTLMv2 */
  643. rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
  644. ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  645. if (rc) {
  646. cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
  647. __func__);
  648. goto unlock;
  649. }
  650. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  651. if (rc) {
  652. cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
  653. goto unlock;
  654. }
  655. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  656. ntlmv2->ntlmv2_hash,
  657. CIFS_HMAC_MD5_HASH_SIZE);
  658. if (rc) {
  659. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  660. goto unlock;
  661. }
  662. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  663. ses->auth_key.response);
  664. if (rc)
  665. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  666. unlock:
  667. mutex_unlock(&ses->server->srv_mutex);
  668. setup_ntlmv2_rsp_ret:
  669. kfree(tiblob);
  670. return rc;
  671. }
  672. int
  673. calc_seckey(struct cifs_ses *ses)
  674. {
  675. int rc;
  676. struct crypto_blkcipher *tfm_arc4;
  677. struct scatterlist sgin, sgout;
  678. struct blkcipher_desc desc;
  679. unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
  680. get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
  681. tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  682. if (IS_ERR(tfm_arc4)) {
  683. rc = PTR_ERR(tfm_arc4);
  684. cifs_dbg(VFS, "could not allocate crypto API arc4\n");
  685. return rc;
  686. }
  687. desc.tfm = tfm_arc4;
  688. rc = crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
  689. CIFS_SESS_KEY_SIZE);
  690. if (rc) {
  691. cifs_dbg(VFS, "%s: Could not set response as a key\n",
  692. __func__);
  693. return rc;
  694. }
  695. sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
  696. sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  697. rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
  698. if (rc) {
  699. cifs_dbg(VFS, "could not encrypt session key rc: %d\n", rc);
  700. crypto_free_blkcipher(tfm_arc4);
  701. return rc;
  702. }
  703. /* make secondary_key/nonce as session key */
  704. memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
  705. /* and make len as that of session key only */
  706. ses->auth_key.len = CIFS_SESS_KEY_SIZE;
  707. crypto_free_blkcipher(tfm_arc4);
  708. return rc;
  709. }
  710. void
  711. cifs_crypto_shash_release(struct TCP_Server_Info *server)
  712. {
  713. if (server->secmech.cmacaes) {
  714. crypto_free_shash(server->secmech.cmacaes);
  715. server->secmech.cmacaes = NULL;
  716. }
  717. if (server->secmech.hmacsha256) {
  718. crypto_free_shash(server->secmech.hmacsha256);
  719. server->secmech.hmacsha256 = NULL;
  720. }
  721. if (server->secmech.md5) {
  722. crypto_free_shash(server->secmech.md5);
  723. server->secmech.md5 = NULL;
  724. }
  725. if (server->secmech.hmacmd5) {
  726. crypto_free_shash(server->secmech.hmacmd5);
  727. server->secmech.hmacmd5 = NULL;
  728. }
  729. kfree(server->secmech.sdesccmacaes);
  730. server->secmech.sdesccmacaes = NULL;
  731. kfree(server->secmech.sdeschmacsha256);
  732. server->secmech.sdeschmacsha256 = NULL;
  733. kfree(server->secmech.sdeschmacmd5);
  734. server->secmech.sdeschmacmd5 = NULL;
  735. kfree(server->secmech.sdescmd5);
  736. server->secmech.sdescmd5 = NULL;
  737. }