cifsencrypt.c 22 KB

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