cifsencrypt.c 21 KB

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