cifsencrypt.c 24 KB

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