cifsencrypt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. const struct session_key *key, char *signature)
  43. {
  44. struct MD5Context context;
  45. if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL))
  46. return -EINVAL;
  47. cifs_MD5_init(&context);
  48. cifs_MD5_update(&context, (char *)&key->data, key->len);
  49. cifs_MD5_update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
  50. cifs_MD5_final(signature, &context);
  51. return 0;
  52. }
  53. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  54. __u32 *pexpected_response_sequence_number)
  55. {
  56. int rc = 0;
  57. char smb_signature[20];
  58. if ((cifs_pdu == NULL) || (server == NULL))
  59. return -EINVAL;
  60. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  61. return rc;
  62. spin_lock(&GlobalMid_Lock);
  63. cifs_pdu->Signature.Sequence.SequenceNumber =
  64. cpu_to_le32(server->sequence_number);
  65. cifs_pdu->Signature.Sequence.Reserved = 0;
  66. *pexpected_response_sequence_number = server->sequence_number++;
  67. server->sequence_number++;
  68. spin_unlock(&GlobalMid_Lock);
  69. rc = cifs_calculate_signature(cifs_pdu, &server->session_key,
  70. 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. const struct session_key *key, char *signature)
  79. {
  80. struct MD5Context context;
  81. int i;
  82. if ((iov == NULL) || (signature == NULL) || (key == NULL))
  83. return -EINVAL;
  84. cifs_MD5_init(&context);
  85. cifs_MD5_update(&context, (char *)&key->data, key->len);
  86. for (i = 0; i < n_vec; i++) {
  87. if (iov[i].iov_len == 0)
  88. continue;
  89. if (iov[i].iov_base == NULL) {
  90. cERROR(1, "null iovec entry");
  91. return -EIO;
  92. }
  93. /* The first entry includes a length field (which does not get
  94. signed that occupies the first 4 bytes before the header */
  95. if (i == 0) {
  96. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  97. break; /* nothing to sign or corrupt header */
  98. cifs_MD5_update(&context, iov[0].iov_base+4,
  99. iov[0].iov_len-4);
  100. } else
  101. cifs_MD5_update(&context, iov[i].iov_base, iov[i].iov_len);
  102. }
  103. cifs_MD5_final(signature, &context);
  104. return 0;
  105. }
  106. int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  107. __u32 *pexpected_response_sequence_number)
  108. {
  109. int rc = 0;
  110. char smb_signature[20];
  111. struct smb_hdr *cifs_pdu = iov[0].iov_base;
  112. if ((cifs_pdu == NULL) || (server == NULL))
  113. return -EINVAL;
  114. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  115. return rc;
  116. spin_lock(&GlobalMid_Lock);
  117. cifs_pdu->Signature.Sequence.SequenceNumber =
  118. cpu_to_le32(server->sequence_number);
  119. cifs_pdu->Signature.Sequence.Reserved = 0;
  120. *pexpected_response_sequence_number = server->sequence_number++;
  121. server->sequence_number++;
  122. spin_unlock(&GlobalMid_Lock);
  123. rc = cifs_calc_signature2(iov, n_vec, &server->session_key,
  124. 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. const struct session_key *session_key,
  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 || session_key == 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, session_key,
  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. /* We fill in key by putting in 40 byte array which was allocated by caller */
  172. int cifs_calculate_session_key(struct session_key *key, const char *rn,
  173. const char *password)
  174. {
  175. char temp_key[16];
  176. if ((key == NULL) || (rn == NULL))
  177. return -EINVAL;
  178. E_md4hash(password, temp_key);
  179. mdfour(key->data.ntlm, temp_key, 16);
  180. memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
  181. key->len = 40;
  182. return 0;
  183. }
  184. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  185. void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  186. char *lnm_session_key)
  187. {
  188. int i;
  189. char password_with_pad[CIFS_ENCPWD_SIZE];
  190. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  191. if (password)
  192. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  193. if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
  194. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  195. memcpy(lnm_session_key, password_with_pad,
  196. CIFS_ENCPWD_SIZE);
  197. return;
  198. }
  199. /* calculate old style session key */
  200. /* calling toupper is less broken than repeatedly
  201. calling nls_toupper would be since that will never
  202. work for UTF8, but neither handles multibyte code pages
  203. but the only alternative would be converting to UCS-16 (Unicode)
  204. (using a routine something like UniStrupr) then
  205. uppercasing and then converting back from Unicode - which
  206. would only worth doing it if we knew it were utf8. Basically
  207. utf8 and other multibyte codepages each need their own strupper
  208. function since a byte at a time will ont work. */
  209. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  210. password_with_pad[i] = toupper(password_with_pad[i]);
  211. SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  212. /* clear password before we return/free memory */
  213. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  214. }
  215. #endif /* CIFS_WEAK_PW_HASH */
  216. /* Build a proper attribute value/target info pairs blob.
  217. * Fill in netbios and dns domain name and workstation name
  218. * and client time (total five av pairs and + one end of fields indicator.
  219. * Allocate domain name which gets freed when session struct is deallocated.
  220. */
  221. static int
  222. build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
  223. {
  224. unsigned int dlen;
  225. unsigned int wlen;
  226. unsigned int size = 6 * sizeof(struct ntlmssp2_name);
  227. __le64 curtime;
  228. char *defdmname = "WORKGROUP";
  229. unsigned char *blobptr;
  230. struct ntlmssp2_name *attrptr;
  231. if (!ses->domainName) {
  232. ses->domainName = kstrdup(defdmname, GFP_KERNEL);
  233. if (!ses->domainName)
  234. return -ENOMEM;
  235. }
  236. dlen = strlen(ses->domainName);
  237. wlen = strlen(ses->server->hostname);
  238. /* The length of this blob is a size which is
  239. * six times the size of a structure which holds name/size +
  240. * two times the unicode length of a domain name +
  241. * two times the unicode length of a server name +
  242. * size of a timestamp (which is 8 bytes).
  243. */
  244. ses->tilen = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
  245. ses->tiblob = kzalloc(ses->tilen, GFP_KERNEL);
  246. if (!ses->tiblob) {
  247. ses->tilen = 0;
  248. cERROR(1, "Challenge target info allocation failure");
  249. return -ENOMEM;
  250. }
  251. blobptr = ses->tiblob;
  252. attrptr = (struct ntlmssp2_name *) blobptr;
  253. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
  254. attrptr->length = cpu_to_le16(2 * dlen);
  255. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  256. cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  257. blobptr += 2 * dlen;
  258. attrptr = (struct ntlmssp2_name *) blobptr;
  259. attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_COMPUTER_NAME);
  260. attrptr->length = cpu_to_le16(2 * wlen);
  261. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  262. cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
  263. blobptr += 2 * wlen;
  264. attrptr = (struct ntlmssp2_name *) blobptr;
  265. attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_DOMAIN_NAME);
  266. attrptr->length = cpu_to_le16(2 * dlen);
  267. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  268. cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
  269. blobptr += 2 * dlen;
  270. attrptr = (struct ntlmssp2_name *) blobptr;
  271. attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_COMPUTER_NAME);
  272. attrptr->length = cpu_to_le16(2 * wlen);
  273. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  274. cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
  275. blobptr += 2 * wlen;
  276. attrptr = (struct ntlmssp2_name *) blobptr;
  277. attrptr->type = cpu_to_le16(NTLMSSP_AV_TIMESTAMP);
  278. attrptr->length = cpu_to_le16(sizeof(__le64));
  279. blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
  280. curtime = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  281. memcpy(blobptr, &curtime, sizeof(__le64));
  282. return 0;
  283. }
  284. /* Server has provided av pairs/target info in the type 2 challenge
  285. * packet and we have plucked it and stored within smb session.
  286. * We parse that blob here to find netbios domain name to be used
  287. * as part of ntlmv2 authentication (in Target String), if not already
  288. * specified on the command line.
  289. * If this function returns without any error but without fetching
  290. * domain name, authentication may fail against some server but
  291. * may not fail against other (those who are not very particular
  292. * about target string i.e. for some, just user name might suffice.
  293. */
  294. static int
  295. find_domain_name(struct cifsSesInfo *ses)
  296. {
  297. unsigned int attrsize;
  298. unsigned int type;
  299. unsigned int onesize = sizeof(struct ntlmssp2_name);
  300. unsigned char *blobptr;
  301. unsigned char *blobend;
  302. struct ntlmssp2_name *attrptr;
  303. if (!ses->tilen || !ses->tiblob)
  304. return 0;
  305. blobptr = ses->tiblob;
  306. blobend = ses->tiblob + ses->tilen;
  307. while (blobptr + onesize < blobend) {
  308. attrptr = (struct ntlmssp2_name *) blobptr;
  309. type = le16_to_cpu(attrptr->type);
  310. if (type == NTLMSSP_AV_EOL)
  311. break;
  312. blobptr += 2; /* advance attr type */
  313. attrsize = le16_to_cpu(attrptr->length);
  314. blobptr += 2; /* advance attr size */
  315. if (blobptr + attrsize > blobend)
  316. break;
  317. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  318. if (!attrsize)
  319. break;
  320. if (!ses->domainName) {
  321. struct nls_table *default_nls;
  322. ses->domainName =
  323. kmalloc(attrsize + 1, GFP_KERNEL);
  324. if (!ses->domainName)
  325. return -ENOMEM;
  326. default_nls = load_nls_default();
  327. cifs_from_ucs2(ses->domainName,
  328. (__le16 *)blobptr, attrsize, attrsize,
  329. default_nls, false);
  330. unload_nls(default_nls);
  331. break;
  332. }
  333. }
  334. blobptr += attrsize; /* advance attr value */
  335. }
  336. return 0;
  337. }
  338. static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
  339. const struct nls_table *nls_cp)
  340. {
  341. int rc = 0;
  342. int len;
  343. char nt_hash[16];
  344. struct HMACMD5Context *pctxt;
  345. wchar_t *user;
  346. wchar_t *domain;
  347. pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL);
  348. if (pctxt == NULL)
  349. return -ENOMEM;
  350. /* calculate md4 hash of password */
  351. E_md4hash(ses->password, nt_hash);
  352. /* convert Domainname to unicode and uppercase */
  353. hmac_md5_init_limK_to_64(nt_hash, 16, pctxt);
  354. /* convert ses->userName to unicode and uppercase */
  355. len = strlen(ses->userName);
  356. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  357. if (user == NULL)
  358. goto calc_exit_2;
  359. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  360. UniStrupr(user);
  361. hmac_md5_update((char *)user, 2*len, pctxt);
  362. /* convert ses->domainName to unicode and uppercase */
  363. if (ses->domainName) {
  364. len = strlen(ses->domainName);
  365. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  366. if (domain == NULL)
  367. goto calc_exit_1;
  368. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  369. nls_cp);
  370. /* the following line was removed since it didn't work well
  371. with lower cased domain name that passed as an option.
  372. Maybe converting the domain name earlier makes sense */
  373. /* UniStrupr(domain); */
  374. hmac_md5_update((char *)domain, 2*len, pctxt);
  375. kfree(domain);
  376. }
  377. calc_exit_1:
  378. kfree(user);
  379. calc_exit_2:
  380. /* BB FIXME what about bytes 24 through 40 of the signing key?
  381. compare with the NTLM example */
  382. hmac_md5_final(ses->ntlmv2_hash, pctxt);
  383. kfree(pctxt);
  384. return rc;
  385. }
  386. int
  387. setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
  388. const struct nls_table *nls_cp)
  389. {
  390. int rc;
  391. struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
  392. struct HMACMD5Context context;
  393. buf->blob_signature = cpu_to_le32(0x00000101);
  394. buf->reserved = 0;
  395. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  396. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  397. buf->reserved2 = 0;
  398. if (ses->server->secType == RawNTLMSSP) {
  399. if (!ses->domainName) {
  400. rc = find_domain_name(ses);
  401. if (rc) {
  402. cERROR(1, "error %d finding domain name", rc);
  403. goto setup_ntlmv2_rsp_ret;
  404. }
  405. }
  406. } else {
  407. rc = build_avpair_blob(ses, nls_cp);
  408. if (rc) {
  409. cERROR(1, "error %d building av pair blob", rc);
  410. return rc;
  411. }
  412. }
  413. /* calculate buf->ntlmv2_hash */
  414. rc = calc_ntlmv2_hash(ses, nls_cp);
  415. if (rc) {
  416. cERROR(1, "could not get v2 hash rc %d", rc);
  417. goto setup_ntlmv2_rsp_ret;
  418. }
  419. CalcNTLMv2_response(ses, resp_buf);
  420. /* now calculate the session key for NTLMv2 */
  421. hmac_md5_init_limK_to_64(ses->ntlmv2_hash, 16, &context);
  422. hmac_md5_update(resp_buf, 16, &context);
  423. hmac_md5_final(ses->auth_key.data.ntlmv2.key, &context);
  424. memcpy(&ses->auth_key.data.ntlmv2.resp, resp_buf,
  425. sizeof(struct ntlmv2_resp));
  426. ses->auth_key.len = 16 + sizeof(struct ntlmv2_resp);
  427. return 0;
  428. setup_ntlmv2_rsp_ret:
  429. kfree(ses->tiblob);
  430. ses->tiblob = NULL;
  431. ses->tilen = 0;
  432. return rc;
  433. }
  434. void CalcNTLMv2_response(const struct cifsSesInfo *ses,
  435. char *v2_session_response)
  436. {
  437. struct HMACMD5Context context;
  438. /* rest of v2 struct already generated */
  439. memcpy(v2_session_response + 8, ses->cryptKey, 8);
  440. hmac_md5_init_limK_to_64(ses->ntlmv2_hash, 16, &context);
  441. hmac_md5_update(v2_session_response+8,
  442. sizeof(struct ntlmv2_resp) - 8, &context);
  443. if (ses->tilen)
  444. hmac_md5_update(ses->tiblob, ses->tilen, &context);
  445. hmac_md5_final(v2_session_response, &context);
  446. /* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */
  447. }