cifsencrypt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  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. */
  11. #include <linux/fs.h>
  12. #include <linux/slab.h>
  13. #include "cifspdu.h"
  14. #include "cifsglob.h"
  15. #include "cifs_debug.h"
  16. #include "cifs_unicode.h"
  17. #include "cifsproto.h"
  18. #include "ntlmssp.h"
  19. #include <linux/ctype.h>
  20. #include <linux/random.h>
  21. #include <linux/highmem.h>
  22. #include <linux/fips.h>
  23. #include <linux/iov_iter.h>
  24. #include "../common/arc4.h"
  25. #include <crypto/aead.h>
  26. static size_t cifs_shash_step(void *iter_base, size_t progress, size_t len,
  27. void *priv, void *priv2)
  28. {
  29. struct shash_desc *shash = priv;
  30. int ret, *pret = priv2;
  31. ret = crypto_shash_update(shash, iter_base, len);
  32. if (ret < 0) {
  33. *pret = ret;
  34. return len;
  35. }
  36. return 0;
  37. }
  38. /*
  39. * Pass the data from an iterator into a hash.
  40. */
  41. static int cifs_shash_iter(const struct iov_iter *iter, size_t maxsize,
  42. struct shash_desc *shash)
  43. {
  44. struct iov_iter tmp_iter = *iter;
  45. int err = -EIO;
  46. if (iterate_and_advance_kernel(&tmp_iter, maxsize, shash, &err,
  47. cifs_shash_step) != maxsize)
  48. return err;
  49. return 0;
  50. }
  51. int __cifs_calc_signature(struct smb_rqst *rqst,
  52. struct TCP_Server_Info *server, char *signature,
  53. struct shash_desc *shash)
  54. {
  55. int i;
  56. ssize_t rc;
  57. struct kvec *iov = rqst->rq_iov;
  58. int n_vec = rqst->rq_nvec;
  59. /* iov[0] is actual data and not the rfc1002 length for SMB2+ */
  60. if (!is_smb1(server)) {
  61. if (iov[0].iov_len <= 4)
  62. return -EIO;
  63. i = 0;
  64. } else {
  65. if (n_vec < 2 || iov[0].iov_len != 4)
  66. return -EIO;
  67. i = 1; /* skip rfc1002 length */
  68. }
  69. for (; i < n_vec; i++) {
  70. if (iov[i].iov_len == 0)
  71. continue;
  72. if (iov[i].iov_base == NULL) {
  73. cifs_dbg(VFS, "null iovec entry\n");
  74. return -EIO;
  75. }
  76. rc = crypto_shash_update(shash,
  77. iov[i].iov_base, iov[i].iov_len);
  78. if (rc) {
  79. cifs_dbg(VFS, "%s: Could not update with payload\n",
  80. __func__);
  81. return rc;
  82. }
  83. }
  84. rc = cifs_shash_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), shash);
  85. if (rc < 0)
  86. return rc;
  87. rc = crypto_shash_final(shash, signature);
  88. if (rc)
  89. cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
  90. return rc;
  91. }
  92. /*
  93. * Calculate and return the CIFS signature based on the mac key and SMB PDU.
  94. * The 16 byte signature must be allocated by the caller. Note we only use the
  95. * 1st eight bytes and that the smb header signature field on input contains
  96. * the sequence number before this function is called. Also, this function
  97. * should be called with the server->srv_mutex held.
  98. */
  99. static int cifs_calc_signature(struct smb_rqst *rqst,
  100. struct TCP_Server_Info *server, char *signature)
  101. {
  102. int rc;
  103. if (!rqst->rq_iov || !signature || !server)
  104. return -EINVAL;
  105. rc = cifs_alloc_hash("md5", &server->secmech.md5);
  106. if (rc)
  107. return -1;
  108. rc = crypto_shash_init(server->secmech.md5);
  109. if (rc) {
  110. cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
  111. return rc;
  112. }
  113. rc = crypto_shash_update(server->secmech.md5,
  114. server->session_key.response, server->session_key.len);
  115. if (rc) {
  116. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  117. return rc;
  118. }
  119. return __cifs_calc_signature(rqst, server, signature, server->secmech.md5);
  120. }
  121. /* must be called with server->srv_mutex held */
  122. int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
  123. __u32 *pexpected_response_sequence_number)
  124. {
  125. int rc = 0;
  126. char smb_signature[20];
  127. struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  128. if (rqst->rq_iov[0].iov_len != 4 ||
  129. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  130. return -EIO;
  131. if ((cifs_pdu == NULL) || (server == NULL))
  132. return -EINVAL;
  133. spin_lock(&server->srv_lock);
  134. if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
  135. server->tcpStatus == CifsNeedNegotiate) {
  136. spin_unlock(&server->srv_lock);
  137. return rc;
  138. }
  139. spin_unlock(&server->srv_lock);
  140. if (!server->session_estab) {
  141. memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
  142. return rc;
  143. }
  144. cifs_pdu->Signature.Sequence.SequenceNumber =
  145. cpu_to_le32(server->sequence_number);
  146. cifs_pdu->Signature.Sequence.Reserved = 0;
  147. *pexpected_response_sequence_number = ++server->sequence_number;
  148. ++server->sequence_number;
  149. rc = cifs_calc_signature(rqst, server, smb_signature);
  150. if (rc)
  151. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  152. else
  153. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  154. return rc;
  155. }
  156. int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  157. __u32 *pexpected_response_sequence)
  158. {
  159. struct smb_rqst rqst = { .rq_iov = iov,
  160. .rq_nvec = n_vec };
  161. return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
  162. }
  163. /* must be called with server->srv_mutex held */
  164. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  165. __u32 *pexpected_response_sequence_number)
  166. {
  167. struct kvec iov[2];
  168. iov[0].iov_base = cifs_pdu;
  169. iov[0].iov_len = 4;
  170. iov[1].iov_base = (char *)cifs_pdu + 4;
  171. iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
  172. return cifs_sign_smbv(iov, 2, server,
  173. pexpected_response_sequence_number);
  174. }
  175. int cifs_verify_signature(struct smb_rqst *rqst,
  176. struct TCP_Server_Info *server,
  177. __u32 expected_sequence_number)
  178. {
  179. unsigned int rc;
  180. char server_response_sig[8];
  181. char what_we_think_sig_should_be[20];
  182. struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  183. if (rqst->rq_iov[0].iov_len != 4 ||
  184. rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
  185. return -EIO;
  186. if (cifs_pdu == NULL || server == NULL)
  187. return -EINVAL;
  188. if (!server->session_estab)
  189. return 0;
  190. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  191. struct smb_com_lock_req *pSMB =
  192. (struct smb_com_lock_req *)cifs_pdu;
  193. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  194. return 0;
  195. }
  196. /* BB what if signatures are supposed to be on for session but
  197. server does not send one? BB */
  198. /* Do not need to verify session setups with signature "BSRSPYL " */
  199. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  200. cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
  201. cifs_pdu->Command);
  202. /* save off the original signature so we can modify the smb and check
  203. its signature against what the server sent */
  204. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  205. cifs_pdu->Signature.Sequence.SequenceNumber =
  206. cpu_to_le32(expected_sequence_number);
  207. cifs_pdu->Signature.Sequence.Reserved = 0;
  208. cifs_server_lock(server);
  209. rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
  210. cifs_server_unlock(server);
  211. if (rc)
  212. return rc;
  213. /* cifs_dump_mem("what we think it should be: ",
  214. what_we_think_sig_should_be, 16); */
  215. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  216. return -EACCES;
  217. else
  218. return 0;
  219. }
  220. /* Build a proper attribute value/target info pairs blob.
  221. * Fill in netbios and dns domain name and workstation name
  222. * and client time (total five av pairs and + one end of fields indicator.
  223. * Allocate domain name which gets freed when session struct is deallocated.
  224. */
  225. static int
  226. build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
  227. {
  228. unsigned int dlen;
  229. unsigned int size = 2 * sizeof(struct ntlmssp2_name);
  230. char *defdmname = "WORKGROUP";
  231. unsigned char *blobptr;
  232. struct ntlmssp2_name *attrptr;
  233. if (!ses->domainName) {
  234. ses->domainName = kstrdup(defdmname, GFP_KERNEL);
  235. if (!ses->domainName)
  236. return -ENOMEM;
  237. }
  238. dlen = strlen(ses->domainName);
  239. /*
  240. * The length of this blob is two times the size of a
  241. * structure (av pair) which holds name/size
  242. * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
  243. * unicode length of a netbios domain name
  244. */
  245. kfree_sensitive(ses->auth_key.response);
  246. ses->auth_key.len = size + 2 * dlen;
  247. ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
  248. if (!ses->auth_key.response) {
  249. ses->auth_key.len = 0;
  250. return -ENOMEM;
  251. }
  252. blobptr = ses->auth_key.response;
  253. attrptr = (struct ntlmssp2_name *) blobptr;
  254. /*
  255. * As defined in MS-NTLM 3.3.2, just this av pair field
  256. * is sufficient as part of the temp
  257. */
  258. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
  259. attrptr->length = cpu_to_le16(2 * dlen);
  260. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  261. cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  262. return 0;
  263. }
  264. /* Server has provided av pairs/target info in the type 2 challenge
  265. * packet and we have plucked it and stored within smb session.
  266. * We parse that blob here to find netbios domain name to be used
  267. * as part of ntlmv2 authentication (in Target String), if not already
  268. * specified on the command line.
  269. * If this function returns without any error but without fetching
  270. * domain name, authentication may fail against some server but
  271. * may not fail against other (those who are not very particular
  272. * about target string i.e. for some, just user name might suffice.
  273. */
  274. static int
  275. find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
  276. {
  277. unsigned int attrsize;
  278. unsigned int type;
  279. unsigned int onesize = sizeof(struct ntlmssp2_name);
  280. unsigned char *blobptr;
  281. unsigned char *blobend;
  282. struct ntlmssp2_name *attrptr;
  283. if (!ses->auth_key.len || !ses->auth_key.response)
  284. return 0;
  285. blobptr = ses->auth_key.response;
  286. blobend = blobptr + ses->auth_key.len;
  287. while (blobptr + onesize < blobend) {
  288. attrptr = (struct ntlmssp2_name *) blobptr;
  289. type = le16_to_cpu(attrptr->type);
  290. if (type == NTLMSSP_AV_EOL)
  291. break;
  292. blobptr += 2; /* advance attr type */
  293. attrsize = le16_to_cpu(attrptr->length);
  294. blobptr += 2; /* advance attr size */
  295. if (blobptr + attrsize > blobend)
  296. break;
  297. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  298. if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
  299. break;
  300. if (!ses->domainName) {
  301. ses->domainName =
  302. kmalloc(attrsize + 1, GFP_KERNEL);
  303. if (!ses->domainName)
  304. return -ENOMEM;
  305. cifs_from_utf16(ses->domainName,
  306. (__le16 *)blobptr, attrsize, attrsize,
  307. nls_cp, NO_MAP_UNI_RSVD);
  308. break;
  309. }
  310. }
  311. blobptr += attrsize; /* advance attr value */
  312. }
  313. return 0;
  314. }
  315. /* Server has provided av pairs/target info in the type 2 challenge
  316. * packet and we have plucked it and stored within smb session.
  317. * We parse that blob here to find the server given timestamp
  318. * as part of ntlmv2 authentication (or local current time as
  319. * default in case of failure)
  320. */
  321. static __le64
  322. find_timestamp(struct cifs_ses *ses)
  323. {
  324. unsigned int attrsize;
  325. unsigned int type;
  326. unsigned int onesize = sizeof(struct ntlmssp2_name);
  327. unsigned char *blobptr;
  328. unsigned char *blobend;
  329. struct ntlmssp2_name *attrptr;
  330. struct timespec64 ts;
  331. if (!ses->auth_key.len || !ses->auth_key.response)
  332. return 0;
  333. blobptr = ses->auth_key.response;
  334. blobend = blobptr + ses->auth_key.len;
  335. while (blobptr + onesize < blobend) {
  336. attrptr = (struct ntlmssp2_name *) blobptr;
  337. type = le16_to_cpu(attrptr->type);
  338. if (type == NTLMSSP_AV_EOL)
  339. break;
  340. blobptr += 2; /* advance attr type */
  341. attrsize = le16_to_cpu(attrptr->length);
  342. blobptr += 2; /* advance attr size */
  343. if (blobptr + attrsize > blobend)
  344. break;
  345. if (type == NTLMSSP_AV_TIMESTAMP) {
  346. if (attrsize == sizeof(u64))
  347. return *((__le64 *)blobptr);
  348. }
  349. blobptr += attrsize; /* advance attr value */
  350. }
  351. ktime_get_real_ts64(&ts);
  352. return cpu_to_le64(cifs_UnixTimeToNT(ts));
  353. }
  354. static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
  355. const struct nls_table *nls_cp, struct shash_desc *hmacmd5)
  356. {
  357. int rc = 0;
  358. int len;
  359. char nt_hash[CIFS_NTHASH_SIZE];
  360. __le16 *user;
  361. wchar_t *domain;
  362. wchar_t *server;
  363. /* calculate md4 hash of password */
  364. E_md4hash(ses->password, nt_hash, nls_cp);
  365. rc = crypto_shash_setkey(hmacmd5->tfm, nt_hash, CIFS_NTHASH_SIZE);
  366. if (rc) {
  367. cifs_dbg(VFS, "%s: Could not set NT hash as a key, rc=%d\n", __func__, rc);
  368. return rc;
  369. }
  370. rc = crypto_shash_init(hmacmd5);
  371. if (rc) {
  372. cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
  373. return rc;
  374. }
  375. /* convert ses->user_name to unicode */
  376. len = ses->user_name ? strlen(ses->user_name) : 0;
  377. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  378. if (user == NULL)
  379. return -ENOMEM;
  380. if (len) {
  381. len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
  382. UniStrupr(user);
  383. } else {
  384. *(u16 *)user = 0;
  385. }
  386. rc = crypto_shash_update(hmacmd5, (char *)user, 2 * len);
  387. kfree(user);
  388. if (rc) {
  389. cifs_dbg(VFS, "%s: Could not update with user, rc=%d\n", __func__, rc);
  390. return rc;
  391. }
  392. /* convert ses->domainName to unicode and uppercase */
  393. if (ses->domainName) {
  394. len = strlen(ses->domainName);
  395. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  396. if (domain == NULL)
  397. return -ENOMEM;
  398. len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
  399. nls_cp);
  400. rc = crypto_shash_update(hmacmd5, (char *)domain, 2 * len);
  401. kfree(domain);
  402. if (rc) {
  403. cifs_dbg(VFS, "%s: Could not update with domain, rc=%d\n", __func__, rc);
  404. return rc;
  405. }
  406. } else {
  407. /* We use ses->ip_addr if no domain name available */
  408. len = strlen(ses->ip_addr);
  409. server = kmalloc(2 + (len * 2), GFP_KERNEL);
  410. if (server == NULL)
  411. return -ENOMEM;
  412. len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp);
  413. rc = crypto_shash_update(hmacmd5, (char *)server, 2 * len);
  414. kfree(server);
  415. if (rc) {
  416. cifs_dbg(VFS, "%s: Could not update with server, rc=%d\n", __func__, rc);
  417. return rc;
  418. }
  419. }
  420. rc = crypto_shash_final(hmacmd5, ntlmv2_hash);
  421. if (rc)
  422. cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
  423. return rc;
  424. }
  425. static int
  426. CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash, struct shash_desc *hmacmd5)
  427. {
  428. int rc;
  429. struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
  430. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  431. unsigned int hash_len;
  432. /* The MD5 hash starts at challenge_key.key */
  433. hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
  434. offsetof(struct ntlmv2_resp, challenge.key[0]));
  435. rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  436. if (rc) {
  437. cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc);
  438. return rc;
  439. }
  440. rc = crypto_shash_init(hmacmd5);
  441. if (rc) {
  442. cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
  443. return rc;
  444. }
  445. if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
  446. memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  447. else
  448. memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  449. rc = crypto_shash_update(hmacmd5, ntlmv2->challenge.key, hash_len);
  450. if (rc) {
  451. cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc);
  452. return rc;
  453. }
  454. /* Note that the MD5 digest over writes anon.challenge_key.key */
  455. rc = crypto_shash_final(hmacmd5, ntlmv2->ntlmv2_hash);
  456. if (rc)
  457. cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
  458. return rc;
  459. }
  460. int
  461. setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
  462. {
  463. struct shash_desc *hmacmd5 = NULL;
  464. int rc;
  465. int baselen;
  466. unsigned int tilen;
  467. struct ntlmv2_resp *ntlmv2;
  468. char ntlmv2_hash[16];
  469. unsigned char *tiblob = NULL; /* target info blob */
  470. __le64 rsp_timestamp;
  471. if (nls_cp == NULL) {
  472. cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__);
  473. return -EINVAL;
  474. }
  475. if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
  476. if (!ses->domainName) {
  477. if (ses->domainAuto) {
  478. rc = find_domain_name(ses, nls_cp);
  479. if (rc) {
  480. cifs_dbg(VFS, "error %d finding domain name\n",
  481. rc);
  482. goto setup_ntlmv2_rsp_ret;
  483. }
  484. } else {
  485. ses->domainName = kstrdup("", GFP_KERNEL);
  486. }
  487. }
  488. } else {
  489. rc = build_avpair_blob(ses, nls_cp);
  490. if (rc) {
  491. cifs_dbg(VFS, "error %d building av pair blob\n", rc);
  492. goto setup_ntlmv2_rsp_ret;
  493. }
  494. }
  495. /* Must be within 5 minutes of the server (or in range +/-2h
  496. * in case of Mac OS X), so simply carry over server timestamp
  497. * (as Windows 7 does)
  498. */
  499. rsp_timestamp = find_timestamp(ses);
  500. baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
  501. tilen = ses->auth_key.len;
  502. tiblob = ses->auth_key.response;
  503. ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
  504. if (!ses->auth_key.response) {
  505. rc = -ENOMEM;
  506. ses->auth_key.len = 0;
  507. goto setup_ntlmv2_rsp_ret;
  508. }
  509. ses->auth_key.len += baselen;
  510. ntlmv2 = (struct ntlmv2_resp *)
  511. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  512. ntlmv2->blob_signature = cpu_to_le32(0x00000101);
  513. ntlmv2->reserved = 0;
  514. ntlmv2->time = rsp_timestamp;
  515. get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
  516. ntlmv2->reserved2 = 0;
  517. memcpy(ses->auth_key.response + baselen, tiblob, tilen);
  518. cifs_server_lock(ses->server);
  519. rc = cifs_alloc_hash("hmac(md5)", &hmacmd5);
  520. if (rc) {
  521. cifs_dbg(VFS, "Could not allocate HMAC-MD5, rc=%d\n", rc);
  522. goto unlock;
  523. }
  524. /* calculate ntlmv2_hash */
  525. rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp, hmacmd5);
  526. if (rc) {
  527. cifs_dbg(VFS, "Could not get NTLMv2 hash, rc=%d\n", rc);
  528. goto unlock;
  529. }
  530. /* calculate first part of the client response (CR1) */
  531. rc = CalcNTLMv2_response(ses, ntlmv2_hash, hmacmd5);
  532. if (rc) {
  533. cifs_dbg(VFS, "Could not calculate CR1, rc=%d\n", rc);
  534. goto unlock;
  535. }
  536. /* now calculate the session key for NTLMv2 */
  537. rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  538. if (rc) {
  539. cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc);
  540. goto unlock;
  541. }
  542. rc = crypto_shash_init(hmacmd5);
  543. if (rc) {
  544. cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
  545. goto unlock;
  546. }
  547. rc = crypto_shash_update(hmacmd5, ntlmv2->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  548. if (rc) {
  549. cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc);
  550. goto unlock;
  551. }
  552. rc = crypto_shash_final(hmacmd5, ses->auth_key.response);
  553. if (rc)
  554. cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
  555. unlock:
  556. cifs_server_unlock(ses->server);
  557. cifs_free_hash(&hmacmd5);
  558. setup_ntlmv2_rsp_ret:
  559. kfree_sensitive(tiblob);
  560. return rc;
  561. }
  562. int
  563. calc_seckey(struct cifs_ses *ses)
  564. {
  565. unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
  566. struct arc4_ctx *ctx_arc4;
  567. if (fips_enabled)
  568. return -ENODEV;
  569. get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
  570. ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL);
  571. if (!ctx_arc4) {
  572. cifs_dbg(VFS, "Could not allocate arc4 context\n");
  573. return -ENOMEM;
  574. }
  575. cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
  576. cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
  577. CIFS_CPHTXT_SIZE);
  578. /* make secondary_key/nonce as session key */
  579. memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
  580. /* and make len as that of session key only */
  581. ses->auth_key.len = CIFS_SESS_KEY_SIZE;
  582. memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
  583. kfree_sensitive(ctx_arc4);
  584. return 0;
  585. }
  586. void
  587. cifs_crypto_secmech_release(struct TCP_Server_Info *server)
  588. {
  589. cifs_free_hash(&server->secmech.aes_cmac);
  590. cifs_free_hash(&server->secmech.hmacsha256);
  591. cifs_free_hash(&server->secmech.md5);
  592. cifs_free_hash(&server->secmech.sha512);
  593. if (server->secmech.enc) {
  594. crypto_free_aead(server->secmech.enc);
  595. server->secmech.enc = NULL;
  596. }
  597. if (server->secmech.dec) {
  598. crypto_free_aead(server->secmech.dec);
  599. server->secmech.dec = NULL;
  600. }
  601. }