cifsencrypt.c 22 KB

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