cifsencrypt.c 23 KB

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