cifsencrypt.c 22 KB

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