cifsencrypt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * fs/cifs/cifsencrypt.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2005,2006
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include "cifspdu.h"
  24. #include "cifsglob.h"
  25. #include "cifs_debug.h"
  26. #include "md5.h"
  27. #include "cifs_unicode.h"
  28. #include "cifsproto.h"
  29. #include "ntlmssp.h"
  30. #include <linux/ctype.h>
  31. #include <linux/random.h>
  32. /* Calculate and return the CIFS signature based on the mac key and SMB PDU */
  33. /* the 16 byte signature must be allocated by the caller */
  34. /* Note we only use the 1st eight bytes */
  35. /* Note that the smb header signature field on input contains the
  36. sequence number before this function is called */
  37. extern void mdfour(unsigned char *out, unsigned char *in, int n);
  38. extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
  39. extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
  40. unsigned char *p24);
  41. static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
  42. struct TCP_Server_Info *server, char *signature)
  43. {
  44. struct MD5Context context;
  45. if (cifs_pdu == NULL || signature == NULL || server == NULL)
  46. return -EINVAL;
  47. cifs_MD5_init(&context);
  48. cifs_MD5_update(&context, server->session_key.response,
  49. server->session_key.len);
  50. cifs_MD5_update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
  51. cifs_MD5_final(signature, &context);
  52. return 0;
  53. }
  54. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  55. __u32 *pexpected_response_sequence_number)
  56. {
  57. int rc = 0;
  58. char smb_signature[20];
  59. if ((cifs_pdu == NULL) || (server == NULL))
  60. return -EINVAL;
  61. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  62. return rc;
  63. spin_lock(&GlobalMid_Lock);
  64. cifs_pdu->Signature.Sequence.SequenceNumber =
  65. cpu_to_le32(server->sequence_number);
  66. cifs_pdu->Signature.Sequence.Reserved = 0;
  67. *pexpected_response_sequence_number = server->sequence_number++;
  68. server->sequence_number++;
  69. spin_unlock(&GlobalMid_Lock);
  70. rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
  71. if (rc)
  72. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  73. else
  74. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  75. return rc;
  76. }
  77. static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
  78. struct TCP_Server_Info *server, char *signature)
  79. {
  80. struct MD5Context context;
  81. int i;
  82. if (iov == NULL || signature == NULL || server == NULL)
  83. return -EINVAL;
  84. cifs_MD5_init(&context);
  85. cifs_MD5_update(&context, server->session_key.response,
  86. server->session_key.len);
  87. for (i = 0; i < n_vec; i++) {
  88. if (iov[i].iov_len == 0)
  89. continue;
  90. if (iov[i].iov_base == NULL) {
  91. cERROR(1, "null iovec entry");
  92. return -EIO;
  93. }
  94. /* The first entry includes a length field (which does not get
  95. signed that occupies the first 4 bytes before the header */
  96. if (i == 0) {
  97. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  98. break; /* nothing to sign or corrupt header */
  99. cifs_MD5_update(&context, iov[0].iov_base+4,
  100. iov[0].iov_len-4);
  101. } else
  102. cifs_MD5_update(&context, iov[i].iov_base, iov[i].iov_len);
  103. }
  104. cifs_MD5_final(signature, &context);
  105. return 0;
  106. }
  107. int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  108. __u32 *pexpected_response_sequence_number)
  109. {
  110. int rc = 0;
  111. char smb_signature[20];
  112. struct smb_hdr *cifs_pdu = iov[0].iov_base;
  113. if ((cifs_pdu == NULL) || (server == NULL))
  114. return -EINVAL;
  115. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  116. return rc;
  117. spin_lock(&GlobalMid_Lock);
  118. cifs_pdu->Signature.Sequence.SequenceNumber =
  119. cpu_to_le32(server->sequence_number);
  120. cifs_pdu->Signature.Sequence.Reserved = 0;
  121. *pexpected_response_sequence_number = server->sequence_number++;
  122. server->sequence_number++;
  123. spin_unlock(&GlobalMid_Lock);
  124. rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
  125. if (rc)
  126. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  127. else
  128. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  129. return rc;
  130. }
  131. int cifs_verify_signature(struct smb_hdr *cifs_pdu,
  132. struct TCP_Server_Info *server,
  133. __u32 expected_sequence_number)
  134. {
  135. unsigned int rc;
  136. char server_response_sig[8];
  137. char what_we_think_sig_should_be[20];
  138. if (cifs_pdu == NULL || server == NULL)
  139. return -EINVAL;
  140. if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
  141. return 0;
  142. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  143. struct smb_com_lock_req *pSMB =
  144. (struct smb_com_lock_req *)cifs_pdu;
  145. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  146. return 0;
  147. }
  148. /* BB what if signatures are supposed to be on for session but
  149. server does not send one? BB */
  150. /* Do not need to verify session setups with signature "BSRSPYL " */
  151. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  152. cFYI(1, "dummy signature received for smb command 0x%x",
  153. cifs_pdu->Command);
  154. /* save off the origiginal signature so we can modify the smb and check
  155. its signature against what the server sent */
  156. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  157. cifs_pdu->Signature.Sequence.SequenceNumber =
  158. cpu_to_le32(expected_sequence_number);
  159. cifs_pdu->Signature.Sequence.Reserved = 0;
  160. rc = cifs_calculate_signature(cifs_pdu, server,
  161. what_we_think_sig_should_be);
  162. if (rc)
  163. return rc;
  164. /* cifs_dump_mem("what we think it should be: ",
  165. what_we_think_sig_should_be, 16); */
  166. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  167. return -EACCES;
  168. else
  169. return 0;
  170. }
  171. /* first calculate 24 bytes ntlm response and then 16 byte session key */
  172. int setup_ntlm_response(struct cifsSesInfo *ses)
  173. {
  174. unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
  175. char temp_key[CIFS_SESS_KEY_SIZE];
  176. if (!ses)
  177. return -EINVAL;
  178. ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
  179. if (!ses->auth_key.response) {
  180. cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len);
  181. return -ENOMEM;
  182. }
  183. ses->auth_key.len = temp_len;
  184. SMBNTencrypt(ses->password, ses->cryptKey,
  185. ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  186. E_md4hash(ses->password, temp_key);
  187. mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
  188. return 0;
  189. }
  190. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  191. void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  192. char *lnm_session_key)
  193. {
  194. int i;
  195. char password_with_pad[CIFS_ENCPWD_SIZE];
  196. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  197. if (password)
  198. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  199. if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
  200. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  201. memcpy(lnm_session_key, password_with_pad,
  202. CIFS_ENCPWD_SIZE);
  203. return;
  204. }
  205. /* calculate old style session key */
  206. /* calling toupper is less broken than repeatedly
  207. calling nls_toupper would be since that will never
  208. work for UTF8, but neither handles multibyte code pages
  209. but the only alternative would be converting to UCS-16 (Unicode)
  210. (using a routine something like UniStrupr) then
  211. uppercasing and then converting back from Unicode - which
  212. would only worth doing it if we knew it were utf8. Basically
  213. utf8 and other multibyte codepages each need their own strupper
  214. function since a byte at a time will ont work. */
  215. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  216. password_with_pad[i] = toupper(password_with_pad[i]);
  217. SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  218. /* clear password before we return/free memory */
  219. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  220. }
  221. #endif /* CIFS_WEAK_PW_HASH */
  222. /* Build a proper attribute value/target info pairs blob.
  223. * Fill in netbios and dns domain name and workstation name
  224. * and client time (total five av pairs and + one end of fields indicator.
  225. * Allocate domain name which gets freed when session struct is deallocated.
  226. */
  227. static int
  228. build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
  229. {
  230. unsigned int dlen;
  231. unsigned int wlen;
  232. unsigned int size = 6 * sizeof(struct ntlmssp2_name);
  233. __le64 curtime;
  234. char *defdmname = "WORKGROUP";
  235. unsigned char *blobptr;
  236. struct ntlmssp2_name *attrptr;
  237. if (!ses->domainName) {
  238. ses->domainName = kstrdup(defdmname, GFP_KERNEL);
  239. if (!ses->domainName)
  240. return -ENOMEM;
  241. }
  242. dlen = strlen(ses->domainName);
  243. wlen = strlen(ses->server->hostname);
  244. /* The length of this blob is a size which is
  245. * six times the size of a structure which holds name/size +
  246. * two times the unicode length of a domain name +
  247. * two times the unicode length of a server name +
  248. * size of a timestamp (which is 8 bytes).
  249. */
  250. ses->tilen = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
  251. ses->tiblob = kzalloc(ses->tilen, GFP_KERNEL);
  252. if (!ses->tiblob) {
  253. ses->tilen = 0;
  254. cERROR(1, "Challenge target info allocation failure");
  255. return -ENOMEM;
  256. }
  257. blobptr = ses->tiblob;
  258. attrptr = (struct ntlmssp2_name *) blobptr;
  259. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
  260. attrptr->length = cpu_to_le16(2 * dlen);
  261. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  262. cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  263. blobptr += 2 * dlen;
  264. attrptr = (struct ntlmssp2_name *) blobptr;
  265. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_COMPUTER_NAME);
  266. attrptr->length = cpu_to_le16(2 * wlen);
  267. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  268. cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
  269. blobptr += 2 * wlen;
  270. attrptr = (struct ntlmssp2_name *) blobptr;
  271. attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_DOMAIN_NAME);
  272. attrptr->length = cpu_to_le16(2 * dlen);
  273. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  274. cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  275. blobptr += 2 * dlen;
  276. attrptr = (struct ntlmssp2_name *) blobptr;
  277. attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_COMPUTER_NAME);
  278. attrptr->length = cpu_to_le16(2 * wlen);
  279. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  280. cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
  281. blobptr += 2 * wlen;
  282. attrptr = (struct ntlmssp2_name *) blobptr;
  283. attrptr->type = cpu_to_le16(NTLMSSP_AV_TIMESTAMP);
  284. attrptr->length = cpu_to_le16(sizeof(__le64));
  285. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  286. curtime = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  287. memcpy(blobptr, &curtime, sizeof(__le64));
  288. return 0;
  289. }
  290. /* Server has provided av pairs/target info in the type 2 challenge
  291. * packet and we have plucked it and stored within smb session.
  292. * We parse that blob here to find netbios domain name to be used
  293. * as part of ntlmv2 authentication (in Target String), if not already
  294. * specified on the command line.
  295. * If this function returns without any error but without fetching
  296. * domain name, authentication may fail against some server but
  297. * may not fail against other (those who are not very particular
  298. * about target string i.e. for some, just user name might suffice.
  299. */
  300. static int
  301. find_domain_name(struct cifsSesInfo *ses)
  302. {
  303. unsigned int attrsize;
  304. unsigned int type;
  305. unsigned int onesize = sizeof(struct ntlmssp2_name);
  306. unsigned char *blobptr;
  307. unsigned char *blobend;
  308. struct ntlmssp2_name *attrptr;
  309. if (!ses->tilen || !ses->tiblob)
  310. return 0;
  311. blobptr = ses->tiblob;
  312. blobend = ses->tiblob + ses->tilen;
  313. while (blobptr + onesize < blobend) {
  314. attrptr = (struct ntlmssp2_name *) blobptr;
  315. type = le16_to_cpu(attrptr->type);
  316. if (type == NTLMSSP_AV_EOL)
  317. break;
  318. blobptr += 2; /* advance attr type */
  319. attrsize = le16_to_cpu(attrptr->length);
  320. blobptr += 2; /* advance attr size */
  321. if (blobptr + attrsize > blobend)
  322. break;
  323. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  324. if (!attrsize)
  325. break;
  326. if (!ses->domainName) {
  327. struct nls_table *default_nls;
  328. ses->domainName =
  329. kmalloc(attrsize + 1, GFP_KERNEL);
  330. if (!ses->domainName)
  331. return -ENOMEM;
  332. default_nls = load_nls_default();
  333. cifs_from_ucs2(ses->domainName,
  334. (__le16 *)blobptr, attrsize, attrsize,
  335. default_nls, false);
  336. unload_nls(default_nls);
  337. break;
  338. }
  339. }
  340. blobptr += attrsize; /* advance attr value */
  341. }
  342. return 0;
  343. }
  344. static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
  345. const struct nls_table *nls_cp)
  346. {
  347. int rc = 0;
  348. int len;
  349. char nt_hash[16];
  350. struct HMACMD5Context *pctxt;
  351. wchar_t *user;
  352. wchar_t *domain;
  353. pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL);
  354. if (pctxt == NULL)
  355. return -ENOMEM;
  356. /* calculate md4 hash of password */
  357. E_md4hash(ses->password, nt_hash);
  358. /* convert Domainname to unicode and uppercase */
  359. hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
  360. /* convert ses->userName to unicode and uppercase */
  361. len = strlen(ses->userName);
  362. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  363. if (user == NULL)
  364. goto calc_exit_2;
  365. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  366. UniStrupr(user);
  367. hmac_md5_update((char *)user, 2*len, pctxt);
  368. /* convert ses->domainName to unicode and uppercase */
  369. if (ses->domainName) {
  370. len = strlen(ses->domainName);
  371. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  372. if (domain == NULL)
  373. goto calc_exit_1;
  374. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  375. nls_cp);
  376. /* the following line was removed since it didn't work well
  377. with lower cased domain name that passed as an option.
  378. Maybe converting the domain name earlier makes sense */
  379. /* UniStrupr(domain); */
  380. hmac_md5_update((char *)domain, 2*len, pctxt);
  381. kfree(domain);
  382. }
  383. calc_exit_1:
  384. kfree(user);
  385. calc_exit_2:
  386. /* BB FIXME what about bytes 24 through 40 of the signing key?
  387. compare with the NTLM example */
  388. hmac_md5_final(ses->ntlmv2_hash, pctxt);
  389. kfree(pctxt);
  390. return rc;
  391. }
  392. int
  393. setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
  394. {
  395. int rc;
  396. int baselen;
  397. struct ntlmv2_resp *buf;
  398. struct HMACMD5Context context;
  399. if (ses->server->secType == RawNTLMSSP) {
  400. if (!ses->domainName) {
  401. rc = find_domain_name(ses);
  402. if (rc) {
  403. cERROR(1, "error %d finding domain name", rc);
  404. goto setup_ntlmv2_rsp_ret;
  405. }
  406. }
  407. } else {
  408. rc = build_avpair_blob(ses, nls_cp);
  409. if (rc) {
  410. cERROR(1, "error %d building av pair blob", rc);
  411. return rc;
  412. }
  413. }
  414. baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
  415. ses->auth_key.len = baselen + ses->tilen;
  416. ses->auth_key.response = kmalloc(ses->auth_key.len, GFP_KERNEL);
  417. if (!ses->auth_key.response) {
  418. rc = ENOMEM;
  419. cERROR(1, "%s: Can't allocate auth blob", __func__);
  420. goto setup_ntlmv2_rsp_ret;
  421. }
  422. buf = (struct ntlmv2_resp *)
  423. (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
  424. buf->blob_signature = cpu_to_le32(0x00000101);
  425. buf->reserved = 0;
  426. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  427. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  428. buf->reserved2 = 0;
  429. memcpy(ses->auth_key.response + baselen, ses->tiblob, ses->tilen);
  430. /* calculate buf->ntlmv2_hash */
  431. rc = calc_ntlmv2_hash(ses, nls_cp);
  432. if (rc) {
  433. cERROR(1, "could not get v2 hash rc %d", rc);
  434. goto setup_ntlmv2_rsp_ret;
  435. }
  436. CalcNTLMv2_response(ses);
  437. /* now calculate the session key for NTLMv2 */
  438. hmac_md5_init_limK_to_64(ses->ntlmv2_hash, 16, &context);
  439. hmac_md5_update(ses->auth_key.response + CIFS_SESS_KEY_SIZE,
  440. 16, &context);
  441. hmac_md5_final(ses->auth_key.response, &context);
  442. return 0;
  443. setup_ntlmv2_rsp_ret:
  444. kfree(ses->tiblob);
  445. ses->tiblob = NULL;
  446. ses->tilen = 0;
  447. return rc;
  448. }
  449. void CalcNTLMv2_response(const struct cifsSesInfo *ses)
  450. {
  451. unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
  452. struct HMACMD5Context context;
  453. /* rest of v2 struct already generated */
  454. memcpy(ses->auth_key.response + offset, ses->cryptKey, 8);
  455. hmac_md5_init_limK_to_64(ses->ntlmv2_hash, 16, &context);
  456. hmac_md5_update(ses->auth_key.response + offset,
  457. ses->auth_key.len - offset, &context);
  458. hmac_md5_final(ses->auth_key.response + CIFS_SESS_KEY_SIZE, &context);
  459. }