cifsencrypt.c 23 KB

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