cifsencrypt.c 22 KB

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