cifsencrypt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  232. memcpy(lnm_session_key, password_with_pad,
  233. CIFS_ENCPWD_SIZE);
  234. return 0;
  235. }
  236. /* calculate old style session key */
  237. /* calling toupper is less broken than repeatedly
  238. calling nls_toupper would be since that will never
  239. work for UTF8, but neither handles multibyte code pages
  240. but the only alternative would be converting to UCS-16 (Unicode)
  241. (using a routine something like UniStrupr) then
  242. uppercasing and then converting back from Unicode - which
  243. would only worth doing it if we knew it were utf8. Basically
  244. utf8 and other multibyte codepages each need their own strupper
  245. function since a byte at a time will ont work. */
  246. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  247. password_with_pad[i] = toupper(password_with_pad[i]);
  248. rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  249. return rc;
  250. }
  251. #endif /* CIFS_WEAK_PW_HASH */
  252. /* Build a proper attribute value/target info pairs blob.
  253. * Fill in netbios and dns domain name and workstation name
  254. * and client time (total five av pairs and + one end of fields indicator.
  255. * Allocate domain name which gets freed when session struct is deallocated.
  256. */
  257. static int
  258. build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
  259. {
  260. unsigned int dlen;
  261. unsigned int size = 2 * sizeof(struct ntlmssp2_name);
  262. char *defdmname = "WORKGROUP";
  263. unsigned char *blobptr;
  264. struct ntlmssp2_name *attrptr;
  265. if (!ses->domainName) {
  266. ses->domainName = kstrdup(defdmname, GFP_KERNEL);
  267. if (!ses->domainName)
  268. return -ENOMEM;
  269. }
  270. dlen = strlen(ses->domainName);
  271. /*
  272. * The length of this blob is two times the size of a
  273. * structure (av pair) which holds name/size
  274. * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
  275. * unicode length of a netbios domain name
  276. */
  277. ses->auth_key.len = size + 2 * dlen;
  278. ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
  279. if (!ses->auth_key.response) {
  280. ses->auth_key.len = 0;
  281. return -ENOMEM;
  282. }
  283. blobptr = ses->auth_key.response;
  284. attrptr = (struct ntlmssp2_name *) blobptr;
  285. /*
  286. * As defined in MS-NTLM 3.3.2, just this av pair field
  287. * is sufficient as part of the temp
  288. */
  289. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
  290. attrptr->length = cpu_to_le16(2 * dlen);
  291. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  292. cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  293. return 0;
  294. }
  295. /* Server has provided av pairs/target info in the type 2 challenge
  296. * packet and we have plucked it and stored within smb session.
  297. * We parse that blob here to find netbios domain name to be used
  298. * as part of ntlmv2 authentication (in Target String), if not already
  299. * specified on the command line.
  300. * If this function returns without any error but without fetching
  301. * domain name, authentication may fail against some server but
  302. * may not fail against other (those who are not very particular
  303. * about target string i.e. for some, just user name might suffice.
  304. */
  305. static int
  306. find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
  307. {
  308. unsigned int attrsize;
  309. unsigned int type;
  310. unsigned int onesize = sizeof(struct ntlmssp2_name);
  311. unsigned char *blobptr;
  312. unsigned char *blobend;
  313. struct ntlmssp2_name *attrptr;
  314. if (!ses->auth_key.len || !ses->auth_key.response)
  315. return 0;
  316. blobptr = ses->auth_key.response;
  317. blobend = blobptr + ses->auth_key.len;
  318. while (blobptr + onesize < blobend) {
  319. attrptr = (struct ntlmssp2_name *) blobptr;
  320. type = le16_to_cpu(attrptr->type);
  321. if (type == NTLMSSP_AV_EOL)
  322. break;
  323. blobptr += 2; /* advance attr type */
  324. attrsize = le16_to_cpu(attrptr->length);
  325. blobptr += 2; /* advance attr size */
  326. if (blobptr + attrsize > blobend)
  327. break;
  328. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  329. if (!attrsize)
  330. break;
  331. if (!ses->domainName) {
  332. ses->domainName =
  333. kmalloc(attrsize + 1, GFP_KERNEL);
  334. if (!ses->domainName)
  335. return -ENOMEM;
  336. cifs_from_utf16(ses->domainName,
  337. (__le16 *)blobptr, attrsize, attrsize,
  338. nls_cp, false);
  339. break;
  340. }
  341. }
  342. blobptr += attrsize; /* advance attr value */
  343. }
  344. return 0;
  345. }
  346. static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
  347. const struct nls_table *nls_cp)
  348. {
  349. int rc = 0;
  350. int len;
  351. char nt_hash[CIFS_NTHASH_SIZE];
  352. wchar_t *user;
  353. wchar_t *domain;
  354. wchar_t *server;
  355. if (!ses->server->secmech.sdeschmacmd5) {
  356. cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
  357. return -1;
  358. }
  359. /* calculate md4 hash of password */
  360. E_md4hash(ses->password, nt_hash, nls_cp);
  361. rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
  362. CIFS_NTHASH_SIZE);
  363. if (rc) {
  364. cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
  365. return rc;
  366. }
  367. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  368. if (rc) {
  369. cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
  370. return rc;
  371. }
  372. /* convert ses->user_name to unicode and uppercase */
  373. len = ses->user_name ? strlen(ses->user_name) : 0;
  374. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  375. if (user == NULL) {
  376. rc = -ENOMEM;
  377. return rc;
  378. }
  379. if (len) {
  380. len = cifs_strtoUTF16((__le16 *)user, ses->user_name, len, nls_cp);
  381. UniStrupr(user);
  382. } else {
  383. memset(user, '\0', 2);
  384. }
  385. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  386. (char *)user, 2 * len);
  387. kfree(user);
  388. if (rc) {
  389. cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
  390. return rc;
  391. }
  392. /* convert ses->domainName to unicode and uppercase */
  393. if (ses->domainName) {
  394. len = strlen(ses->domainName);
  395. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  396. if (domain == NULL) {
  397. rc = -ENOMEM;
  398. return rc;
  399. }
  400. len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
  401. nls_cp);
  402. rc =
  403. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  404. (char *)domain, 2 * len);
  405. kfree(domain);
  406. if (rc) {
  407. cifs_dbg(VFS, "%s: Could not update with domain\n",
  408. __func__);
  409. return rc;
  410. }
  411. } else if (ses->serverName) {
  412. len = strlen(ses->serverName);
  413. server = kmalloc(2 + (len * 2), GFP_KERNEL);
  414. if (server == NULL) {
  415. rc = -ENOMEM;
  416. return rc;
  417. }
  418. len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
  419. nls_cp);
  420. rc =
  421. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  422. (char *)server, 2 * len);
  423. kfree(server);
  424. if (rc) {
  425. cifs_dbg(VFS, "%s: Could not update with server\n",
  426. __func__);
  427. return rc;
  428. }
  429. }
  430. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  431. ntlmv2_hash);
  432. if (rc)
  433. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  434. return rc;
  435. }
  436. static int
  437. CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
  438. {
  439. int rc;
  440. unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
  441. if (!ses->server->secmech.sdeschmacmd5) {
  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,
  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.sdeschmacmd5->shash);
  453. if (rc) {
  454. cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
  455. return rc;
  456. }
  457. if (ses->server->secType == RawNTLMSSP)
  458. memcpy(ses->auth_key.response + offset,
  459. ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  460. else
  461. memcpy(ses->auth_key.response + offset,
  462. ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  463. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  464. ses->auth_key.response + offset, ses->auth_key.len - offset);
  465. if (rc) {
  466. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  467. return rc;
  468. }
  469. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  470. ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  471. if (rc)
  472. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  473. return rc;
  474. }
  475. int
  476. setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
  477. {
  478. int rc;
  479. int baselen;
  480. unsigned int tilen;
  481. struct ntlmv2_resp *buf;
  482. char ntlmv2_hash[16];
  483. unsigned char *tiblob = NULL; /* target info blob */
  484. if (ses->server->secType == RawNTLMSSP) {
  485. if (!ses->domainName) {
  486. rc = find_domain_name(ses, nls_cp);
  487. if (rc) {
  488. cifs_dbg(VFS, "error %d finding domain name\n",
  489. rc);
  490. goto setup_ntlmv2_rsp_ret;
  491. }
  492. }
  493. } else {
  494. rc = build_avpair_blob(ses, nls_cp);
  495. if (rc) {
  496. cifs_dbg(VFS, "error %d building av pair blob\n", rc);
  497. goto setup_ntlmv2_rsp_ret;
  498. }
  499. }
  500. baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
  501. tilen = ses->auth_key.len;
  502. tiblob = ses->auth_key.response;
  503. ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
  504. if (!ses->auth_key.response) {
  505. rc = ENOMEM;
  506. ses->auth_key.len = 0;
  507. goto setup_ntlmv2_rsp_ret;
  508. }
  509. ses->auth_key.len += baselen;
  510. buf = (struct ntlmv2_resp *)
  511. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  512. buf->blob_signature = cpu_to_le32(0x00000101);
  513. buf->reserved = 0;
  514. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  515. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  516. buf->reserved2 = 0;
  517. memcpy(ses->auth_key.response + baselen, tiblob, tilen);
  518. /* calculate ntlmv2_hash */
  519. rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
  520. if (rc) {
  521. cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
  522. goto setup_ntlmv2_rsp_ret;
  523. }
  524. /* calculate first part of the client response (CR1) */
  525. rc = CalcNTLMv2_response(ses, ntlmv2_hash);
  526. if (rc) {
  527. cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
  528. goto setup_ntlmv2_rsp_ret;
  529. }
  530. /* now calculate the session key for NTLMv2 */
  531. rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
  532. ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  533. if (rc) {
  534. cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
  535. __func__);
  536. goto setup_ntlmv2_rsp_ret;
  537. }
  538. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  539. if (rc) {
  540. cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
  541. goto setup_ntlmv2_rsp_ret;
  542. }
  543. rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  544. ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  545. CIFS_HMAC_MD5_HASH_SIZE);
  546. if (rc) {
  547. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  548. goto setup_ntlmv2_rsp_ret;
  549. }
  550. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  551. ses->auth_key.response);
  552. if (rc)
  553. cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
  554. setup_ntlmv2_rsp_ret:
  555. kfree(tiblob);
  556. return rc;
  557. }
  558. int
  559. calc_seckey(struct cifs_ses *ses)
  560. {
  561. int rc;
  562. struct crypto_blkcipher *tfm_arc4;
  563. struct scatterlist sgin, sgout;
  564. struct blkcipher_desc desc;
  565. unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
  566. get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
  567. tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  568. if (IS_ERR(tfm_arc4)) {
  569. rc = PTR_ERR(tfm_arc4);
  570. cifs_dbg(VFS, "could not allocate crypto API arc4\n");
  571. return rc;
  572. }
  573. desc.tfm = tfm_arc4;
  574. rc = crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
  575. CIFS_SESS_KEY_SIZE);
  576. if (rc) {
  577. cifs_dbg(VFS, "%s: Could not set response as a key\n",
  578. __func__);
  579. return rc;
  580. }
  581. sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
  582. sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  583. rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
  584. if (rc) {
  585. cifs_dbg(VFS, "could not encrypt session key rc: %d\n", rc);
  586. crypto_free_blkcipher(tfm_arc4);
  587. return rc;
  588. }
  589. /* make secondary_key/nonce as session key */
  590. memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
  591. /* and make len as that of session key only */
  592. ses->auth_key.len = CIFS_SESS_KEY_SIZE;
  593. crypto_free_blkcipher(tfm_arc4);
  594. return rc;
  595. }
  596. void
  597. cifs_crypto_shash_release(struct TCP_Server_Info *server)
  598. {
  599. if (server->secmech.hmacsha256)
  600. crypto_free_shash(server->secmech.hmacsha256);
  601. if (server->secmech.md5)
  602. crypto_free_shash(server->secmech.md5);
  603. if (server->secmech.hmacmd5)
  604. crypto_free_shash(server->secmech.hmacmd5);
  605. kfree(server->secmech.sdeschmacsha256);
  606. kfree(server->secmech.sdeschmacmd5);
  607. kfree(server->secmech.sdescmd5);
  608. }
  609. int
  610. cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
  611. {
  612. int rc;
  613. unsigned int size;
  614. server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
  615. if (IS_ERR(server->secmech.hmacmd5)) {
  616. cifs_dbg(VFS, "could not allocate crypto hmacmd5\n");
  617. return PTR_ERR(server->secmech.hmacmd5);
  618. }
  619. server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
  620. if (IS_ERR(server->secmech.md5)) {
  621. cifs_dbg(VFS, "could not allocate crypto md5\n");
  622. rc = PTR_ERR(server->secmech.md5);
  623. goto crypto_allocate_md5_fail;
  624. }
  625. server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0);
  626. if (IS_ERR(server->secmech.hmacsha256)) {
  627. cifs_dbg(VFS, "could not allocate crypto hmacsha256\n");
  628. rc = PTR_ERR(server->secmech.hmacsha256);
  629. goto crypto_allocate_hmacsha256_fail;
  630. }
  631. size = sizeof(struct shash_desc) +
  632. crypto_shash_descsize(server->secmech.hmacmd5);
  633. server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
  634. if (!server->secmech.sdeschmacmd5) {
  635. rc = -ENOMEM;
  636. goto crypto_allocate_hmacmd5_sdesc_fail;
  637. }
  638. server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
  639. server->secmech.sdeschmacmd5->shash.flags = 0x0;
  640. size = sizeof(struct shash_desc) +
  641. crypto_shash_descsize(server->secmech.md5);
  642. server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
  643. if (!server->secmech.sdescmd5) {
  644. rc = -ENOMEM;
  645. goto crypto_allocate_md5_sdesc_fail;
  646. }
  647. server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
  648. server->secmech.sdescmd5->shash.flags = 0x0;
  649. size = sizeof(struct shash_desc) +
  650. crypto_shash_descsize(server->secmech.hmacsha256);
  651. server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL);
  652. if (!server->secmech.sdeschmacsha256) {
  653. rc = -ENOMEM;
  654. goto crypto_allocate_hmacsha256_sdesc_fail;
  655. }
  656. server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256;
  657. server->secmech.sdeschmacsha256->shash.flags = 0x0;
  658. return 0;
  659. crypto_allocate_hmacsha256_sdesc_fail:
  660. kfree(server->secmech.sdescmd5);
  661. crypto_allocate_md5_sdesc_fail:
  662. kfree(server->secmech.sdeschmacmd5);
  663. crypto_allocate_hmacmd5_sdesc_fail:
  664. crypto_free_shash(server->secmech.hmacsha256);
  665. crypto_allocate_hmacsha256_fail:
  666. crypto_free_shash(server->secmech.md5);
  667. crypto_allocate_md5_fail:
  668. crypto_free_shash(server->secmech.hmacmd5);
  669. return rc;
  670. }