cifsencrypt.c 24 KB

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