cifsencrypt.c 22 KB

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