cifsencrypt.c 21 KB

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