cifsencrypt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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->server->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->auth_key.len = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
  269. ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
  270. if (!ses->auth_key.response) {
  271. ses->auth_key.len = 0;
  272. cERROR(1, "Challenge target info allocation failure");
  273. return -ENOMEM;
  274. }
  275. blobptr = ses->auth_key.response;
  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, const struct nls_table *nls_cp)
  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->auth_key.len || !ses->auth_key.response)
  328. return 0;
  329. blobptr = ses->auth_key.response;
  330. blobend = blobptr + ses->auth_key.len;
  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. ses->domainName =
  346. kmalloc(attrsize + 1, GFP_KERNEL);
  347. if (!ses->domainName)
  348. return -ENOMEM;
  349. cifs_from_ucs2(ses->domainName,
  350. (__le16 *)blobptr, attrsize, attrsize,
  351. nls_cp, false);
  352. break;
  353. }
  354. }
  355. blobptr += attrsize; /* advance attr value */
  356. }
  357. return 0;
  358. }
  359. static int calc_ntlmv2_hash(struct cifsSesInfo *ses, char *ntlmv2_hash,
  360. const struct nls_table *nls_cp)
  361. {
  362. int rc = 0;
  363. int len;
  364. char nt_hash[CIFS_NTHASH_SIZE];
  365. wchar_t *user;
  366. wchar_t *domain;
  367. wchar_t *server;
  368. if (!ses->server->secmech.sdeschmacmd5) {
  369. cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
  370. return -1;
  371. }
  372. /* calculate md4 hash of password */
  373. E_md4hash(ses->password, nt_hash);
  374. crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
  375. CIFS_NTHASH_SIZE);
  376. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  377. if (rc) {
  378. cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
  379. return rc;
  380. }
  381. /* convert ses->userName to unicode and uppercase */
  382. len = strlen(ses->userName);
  383. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  384. if (user == NULL) {
  385. cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
  386. rc = -ENOMEM;
  387. goto calc_exit_2;
  388. }
  389. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  390. UniStrupr(user);
  391. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  392. (char *)user, 2 * len);
  393. /* convert ses->domainName to unicode and uppercase */
  394. if (ses->domainName) {
  395. len = strlen(ses->domainName);
  396. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  397. if (domain == NULL) {
  398. cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
  399. rc = -ENOMEM;
  400. goto calc_exit_1;
  401. }
  402. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  403. nls_cp);
  404. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  405. (char *)domain, 2 * len);
  406. kfree(domain);
  407. } else if (ses->serverName) {
  408. len = strlen(ses->serverName);
  409. server = kmalloc(2 + (len * 2), GFP_KERNEL);
  410. if (server == NULL) {
  411. cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
  412. rc = -ENOMEM;
  413. goto calc_exit_1;
  414. }
  415. len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
  416. nls_cp);
  417. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  418. (char *)server, 2 * len);
  419. kfree(server);
  420. }
  421. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  422. ntlmv2_hash);
  423. calc_exit_1:
  424. kfree(user);
  425. calc_exit_2:
  426. return rc;
  427. }
  428. static int
  429. CalcNTLMv2_response(const struct cifsSesInfo *ses, char *ntlmv2_hash)
  430. {
  431. int rc;
  432. unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
  433. if (!ses->server->secmech.sdeschmacmd5) {
  434. cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
  435. return -1;
  436. }
  437. crypto_shash_setkey(ses->server->secmech.hmacmd5,
  438. ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  439. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  440. if (rc) {
  441. cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
  442. return rc;
  443. }
  444. if (ses->server->secType == RawNTLMSSP)
  445. memcpy(ses->auth_key.response + offset,
  446. ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  447. else
  448. memcpy(ses->auth_key.response + offset,
  449. ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
  450. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  451. ses->auth_key.response + offset, ses->auth_key.len - offset);
  452. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  453. ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  454. return rc;
  455. }
  456. int
  457. setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
  458. {
  459. int rc;
  460. int baselen;
  461. unsigned int tilen;
  462. struct ntlmv2_resp *buf;
  463. char ntlmv2_hash[16];
  464. unsigned char *tiblob = NULL; /* target info blob */
  465. if (ses->server->secType == RawNTLMSSP) {
  466. if (!ses->domainName) {
  467. rc = find_domain_name(ses, nls_cp);
  468. if (rc) {
  469. cERROR(1, "error %d finding domain name", rc);
  470. goto setup_ntlmv2_rsp_ret;
  471. }
  472. }
  473. } else {
  474. rc = build_avpair_blob(ses, nls_cp);
  475. if (rc) {
  476. cERROR(1, "error %d building av pair blob", rc);
  477. goto setup_ntlmv2_rsp_ret;
  478. }
  479. }
  480. baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
  481. tilen = ses->auth_key.len;
  482. tiblob = ses->auth_key.response;
  483. ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
  484. if (!ses->auth_key.response) {
  485. rc = ENOMEM;
  486. ses->auth_key.len = 0;
  487. cERROR(1, "%s: Can't allocate auth blob", __func__);
  488. goto setup_ntlmv2_rsp_ret;
  489. }
  490. ses->auth_key.len += baselen;
  491. buf = (struct ntlmv2_resp *)
  492. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  493. buf->blob_signature = cpu_to_le32(0x00000101);
  494. buf->reserved = 0;
  495. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  496. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  497. buf->reserved2 = 0;
  498. memcpy(ses->auth_key.response + baselen, tiblob, tilen);
  499. /* calculate ntlmv2_hash */
  500. rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
  501. if (rc) {
  502. cERROR(1, "could not get v2 hash rc %d", rc);
  503. goto setup_ntlmv2_rsp_ret;
  504. }
  505. /* calculate first part of the client response (CR1) */
  506. rc = CalcNTLMv2_response(ses, ntlmv2_hash);
  507. if (rc) {
  508. cERROR(1, "Could not calculate CR1 rc: %d", rc);
  509. goto setup_ntlmv2_rsp_ret;
  510. }
  511. /* now calculate the session key for NTLMv2 */
  512. crypto_shash_setkey(ses->server->secmech.hmacmd5,
  513. ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  514. rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
  515. if (rc) {
  516. cERROR(1, "%s: Could not init hmacmd5\n", __func__);
  517. goto setup_ntlmv2_rsp_ret;
  518. }
  519. crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
  520. ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  521. CIFS_HMAC_MD5_HASH_SIZE);
  522. rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
  523. ses->auth_key.response);
  524. setup_ntlmv2_rsp_ret:
  525. kfree(tiblob);
  526. return rc;
  527. }
  528. int
  529. calc_seckey(struct cifsSesInfo *ses)
  530. {
  531. int rc;
  532. struct crypto_blkcipher *tfm_arc4;
  533. struct scatterlist sgin, sgout;
  534. struct blkcipher_desc desc;
  535. unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
  536. get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
  537. tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
  538. if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
  539. cERROR(1, "could not allocate crypto API arc4\n");
  540. return PTR_ERR(tfm_arc4);
  541. }
  542. desc.tfm = tfm_arc4;
  543. crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
  544. CIFS_SESS_KEY_SIZE);
  545. sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
  546. sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
  547. rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
  548. if (rc) {
  549. cERROR(1, "could not encrypt session key rc: %d\n", rc);
  550. crypto_free_blkcipher(tfm_arc4);
  551. return rc;
  552. }
  553. /* make secondary_key/nonce as session key */
  554. memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
  555. /* and make len as that of session key only */
  556. ses->auth_key.len = CIFS_SESS_KEY_SIZE;
  557. crypto_free_blkcipher(tfm_arc4);
  558. return 0;
  559. }
  560. void
  561. cifs_crypto_shash_release(struct TCP_Server_Info *server)
  562. {
  563. if (server->secmech.md5)
  564. crypto_free_shash(server->secmech.md5);
  565. if (server->secmech.hmacmd5)
  566. crypto_free_shash(server->secmech.hmacmd5);
  567. kfree(server->secmech.sdeschmacmd5);
  568. kfree(server->secmech.sdescmd5);
  569. }
  570. int
  571. cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
  572. {
  573. int rc;
  574. unsigned int size;
  575. server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
  576. if (!server->secmech.hmacmd5 ||
  577. IS_ERR(server->secmech.hmacmd5)) {
  578. cERROR(1, "could not allocate crypto hmacmd5\n");
  579. return PTR_ERR(server->secmech.hmacmd5);
  580. }
  581. server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
  582. if (!server->secmech.md5 || IS_ERR(server->secmech.md5)) {
  583. cERROR(1, "could not allocate crypto md5\n");
  584. rc = PTR_ERR(server->secmech.md5);
  585. goto crypto_allocate_md5_fail;
  586. }
  587. size = sizeof(struct shash_desc) +
  588. crypto_shash_descsize(server->secmech.hmacmd5);
  589. server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
  590. if (!server->secmech.sdeschmacmd5) {
  591. cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
  592. rc = -ENOMEM;
  593. goto crypto_allocate_hmacmd5_sdesc_fail;
  594. }
  595. server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
  596. server->secmech.sdeschmacmd5->shash.flags = 0x0;
  597. size = sizeof(struct shash_desc) +
  598. crypto_shash_descsize(server->secmech.md5);
  599. server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
  600. if (!server->secmech.sdescmd5) {
  601. cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
  602. rc = -ENOMEM;
  603. goto crypto_allocate_md5_sdesc_fail;
  604. }
  605. server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
  606. server->secmech.sdescmd5->shash.flags = 0x0;
  607. return 0;
  608. crypto_allocate_md5_sdesc_fail:
  609. kfree(server->secmech.sdeschmacmd5);
  610. crypto_allocate_hmacmd5_sdesc_fail:
  611. crypto_free_shash(server->secmech.md5);
  612. crypto_allocate_md5_fail:
  613. crypto_free_shash(server->secmech.hmacmd5);
  614. return rc;
  615. }