cifsencrypt.c 22 KB

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