cifsencrypt.c 21 KB

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