cifsencrypt.c 25 KB

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