cifsencrypt.c 19 KB

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