1 // SPDX-License-Identifier: LGPL-2.1
4 * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP
5 * for more detailed information
7 * Copyright (C) International Business Machines Corp., 2005,2013
8 * Author(s): Steve French (sfrench@us.ibm.com)
13 #include <linux/slab.h>
16 #include "cifs_debug.h"
17 #include "cifs_unicode.h"
18 #include "cifsproto.h"
20 #include <linux/ctype.h>
21 #include <linux/random.h>
22 #include <linux/highmem.h>
23 #include <linux/fips.h>
24 #include "../common/arc4.h"
25 #include <crypto/aead.h>
28 * Hash data from a BVEC-type iterator.
30 static int cifs_shash_bvec(const struct iov_iter *iter, ssize_t maxsize,
31 struct shash_desc *shash)
33 const struct bio_vec *bv = iter->bvec;
34 unsigned long start = iter->iov_offset;
39 for (i = 0; i < iter->nr_segs; i++) {
48 len = min_t(size_t, maxsize, len - start);
49 off = bv[i].bv_offset + start;
51 p = kmap_local_page(bv[i].bv_page);
52 ret = crypto_shash_update(shash, p + off, len);
67 * Hash data from a KVEC-type iterator.
69 static int cifs_shash_kvec(const struct iov_iter *iter, ssize_t maxsize,
70 struct shash_desc *shash)
72 const struct kvec *kv = iter->kvec;
73 unsigned long start = iter->iov_offset;
77 for (i = 0; i < iter->nr_segs; i++) {
86 len = min_t(size_t, maxsize, len - start);
87 ret = crypto_shash_update(shash, kv[i].iov_base + start, len);
101 * Hash data from an XARRAY-type iterator.
103 static ssize_t cifs_shash_xarray(const struct iov_iter *iter, ssize_t maxsize,
104 struct shash_desc *shash)
106 struct folio *folios[16], *folio;
107 unsigned int nr, i, j, npages;
108 loff_t start = iter->xarray_start + iter->iov_offset;
109 pgoff_t last, index = start / PAGE_SIZE;
111 size_t len, offset, foffset;
117 last = (start + maxsize - 1) / PAGE_SIZE;
119 nr = xa_extract(iter->xarray, (void **)folios, index, last,
120 ARRAY_SIZE(folios), XA_PRESENT);
124 for (i = 0; i < nr; i++) {
126 npages = folio_nr_pages(folio);
127 foffset = start - folio_pos(folio);
128 offset = foffset % PAGE_SIZE;
129 for (j = foffset / PAGE_SIZE; j < npages; j++) {
130 len = min_t(size_t, maxsize, PAGE_SIZE - offset);
131 p = kmap_local_page(folio_page(folio, j));
132 ret = crypto_shash_update(shash, p, len);
144 } while (nr == ARRAY_SIZE(folios));
149 * Pass the data from an iterator into a hash.
151 static int cifs_shash_iter(const struct iov_iter *iter, size_t maxsize,
152 struct shash_desc *shash)
157 switch (iov_iter_type(iter)) {
159 return cifs_shash_bvec(iter, maxsize, shash);
161 return cifs_shash_kvec(iter, maxsize, shash);
163 return cifs_shash_xarray(iter, maxsize, shash);
165 pr_err("cifs_shash_iter(%u) unsupported\n", iov_iter_type(iter));
171 int __cifs_calc_signature(struct smb_rqst *rqst,
172 struct TCP_Server_Info *server, char *signature,
173 struct shash_desc *shash)
177 struct kvec *iov = rqst->rq_iov;
178 int n_vec = rqst->rq_nvec;
180 /* iov[0] is actual data and not the rfc1002 length for SMB2+ */
181 if (!is_smb1(server)) {
182 if (iov[0].iov_len <= 4)
186 if (n_vec < 2 || iov[0].iov_len != 4)
188 i = 1; /* skip rfc1002 length */
191 for (; i < n_vec; i++) {
192 if (iov[i].iov_len == 0)
194 if (iov[i].iov_base == NULL) {
195 cifs_dbg(VFS, "null iovec entry\n");
199 rc = crypto_shash_update(shash,
200 iov[i].iov_base, iov[i].iov_len);
202 cifs_dbg(VFS, "%s: Could not update with payload\n",
208 rc = cifs_shash_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), shash);
212 rc = crypto_shash_final(shash, signature);
214 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
220 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
221 * The 16 byte signature must be allocated by the caller. Note we only use the
222 * 1st eight bytes and that the smb header signature field on input contains
223 * the sequence number before this function is called. Also, this function
224 * should be called with the server->srv_mutex held.
226 static int cifs_calc_signature(struct smb_rqst *rqst,
227 struct TCP_Server_Info *server, char *signature)
231 if (!rqst->rq_iov || !signature || !server)
234 rc = cifs_alloc_hash("md5", &server->secmech.md5);
238 rc = crypto_shash_init(server->secmech.md5);
240 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
244 rc = crypto_shash_update(server->secmech.md5,
245 server->session_key.response, server->session_key.len);
247 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
251 return __cifs_calc_signature(rqst, server, signature, server->secmech.md5);
254 /* must be called with server->srv_mutex held */
255 int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
256 __u32 *pexpected_response_sequence_number)
259 char smb_signature[20];
260 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
262 if (rqst->rq_iov[0].iov_len != 4 ||
263 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
266 if ((cifs_pdu == NULL) || (server == NULL))
269 spin_lock(&server->srv_lock);
270 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
271 server->tcpStatus == CifsNeedNegotiate) {
272 spin_unlock(&server->srv_lock);
275 spin_unlock(&server->srv_lock);
277 if (!server->session_estab) {
278 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
282 cifs_pdu->Signature.Sequence.SequenceNumber =
283 cpu_to_le32(server->sequence_number);
284 cifs_pdu->Signature.Sequence.Reserved = 0;
286 *pexpected_response_sequence_number = ++server->sequence_number;
287 ++server->sequence_number;
289 rc = cifs_calc_signature(rqst, server, smb_signature);
291 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
293 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
298 int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
299 __u32 *pexpected_response_sequence)
301 struct smb_rqst rqst = { .rq_iov = iov,
304 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
307 /* must be called with server->srv_mutex held */
308 int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
309 __u32 *pexpected_response_sequence_number)
313 iov[0].iov_base = cifs_pdu;
315 iov[1].iov_base = (char *)cifs_pdu + 4;
316 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
318 return cifs_sign_smbv(iov, 2, server,
319 pexpected_response_sequence_number);
322 int cifs_verify_signature(struct smb_rqst *rqst,
323 struct TCP_Server_Info *server,
324 __u32 expected_sequence_number)
327 char server_response_sig[8];
328 char what_we_think_sig_should_be[20];
329 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
331 if (rqst->rq_iov[0].iov_len != 4 ||
332 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
335 if (cifs_pdu == NULL || server == NULL)
338 if (!server->session_estab)
341 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
342 struct smb_com_lock_req *pSMB =
343 (struct smb_com_lock_req *)cifs_pdu;
344 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
348 /* BB what if signatures are supposed to be on for session but
349 server does not send one? BB */
351 /* Do not need to verify session setups with signature "BSRSPYL " */
352 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
353 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
356 /* save off the origiginal signature so we can modify the smb and check
357 its signature against what the server sent */
358 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
360 cifs_pdu->Signature.Sequence.SequenceNumber =
361 cpu_to_le32(expected_sequence_number);
362 cifs_pdu->Signature.Sequence.Reserved = 0;
364 cifs_server_lock(server);
365 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
366 cifs_server_unlock(server);
371 /* cifs_dump_mem("what we think it should be: ",
372 what_we_think_sig_should_be, 16); */
374 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
381 /* Build a proper attribute value/target info pairs blob.
382 * Fill in netbios and dns domain name and workstation name
383 * and client time (total five av pairs and + one end of fields indicator.
384 * Allocate domain name which gets freed when session struct is deallocated.
387 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
390 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
391 char *defdmname = "WORKGROUP";
392 unsigned char *blobptr;
393 struct ntlmssp2_name *attrptr;
395 if (!ses->domainName) {
396 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
397 if (!ses->domainName)
401 dlen = strlen(ses->domainName);
404 * The length of this blob is two times the size of a
405 * structure (av pair) which holds name/size
406 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
407 * unicode length of a netbios domain name
409 kfree_sensitive(ses->auth_key.response);
410 ses->auth_key.len = size + 2 * dlen;
411 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
412 if (!ses->auth_key.response) {
413 ses->auth_key.len = 0;
417 blobptr = ses->auth_key.response;
418 attrptr = (struct ntlmssp2_name *) blobptr;
421 * As defined in MS-NTLM 3.3.2, just this av pair field
422 * is sufficient as part of the temp
424 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
425 attrptr->length = cpu_to_le16(2 * dlen);
426 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
427 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
432 /* Server has provided av pairs/target info in the type 2 challenge
433 * packet and we have plucked it and stored within smb session.
434 * We parse that blob here to find netbios domain name to be used
435 * as part of ntlmv2 authentication (in Target String), if not already
436 * specified on the command line.
437 * If this function returns without any error but without fetching
438 * domain name, authentication may fail against some server but
439 * may not fail against other (those who are not very particular
440 * about target string i.e. for some, just user name might suffice.
443 find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
445 unsigned int attrsize;
447 unsigned int onesize = sizeof(struct ntlmssp2_name);
448 unsigned char *blobptr;
449 unsigned char *blobend;
450 struct ntlmssp2_name *attrptr;
452 if (!ses->auth_key.len || !ses->auth_key.response)
455 blobptr = ses->auth_key.response;
456 blobend = blobptr + ses->auth_key.len;
458 while (blobptr + onesize < blobend) {
459 attrptr = (struct ntlmssp2_name *) blobptr;
460 type = le16_to_cpu(attrptr->type);
461 if (type == NTLMSSP_AV_EOL)
463 blobptr += 2; /* advance attr type */
464 attrsize = le16_to_cpu(attrptr->length);
465 blobptr += 2; /* advance attr size */
466 if (blobptr + attrsize > blobend)
468 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
469 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
471 if (!ses->domainName) {
473 kmalloc(attrsize + 1, GFP_KERNEL);
474 if (!ses->domainName)
476 cifs_from_utf16(ses->domainName,
477 (__le16 *)blobptr, attrsize, attrsize,
478 nls_cp, NO_MAP_UNI_RSVD);
482 blobptr += attrsize; /* advance attr value */
488 /* Server has provided av pairs/target info in the type 2 challenge
489 * packet and we have plucked it and stored within smb session.
490 * We parse that blob here to find the server given timestamp
491 * as part of ntlmv2 authentication (or local current time as
492 * default in case of failure)
495 find_timestamp(struct cifs_ses *ses)
497 unsigned int attrsize;
499 unsigned int onesize = sizeof(struct ntlmssp2_name);
500 unsigned char *blobptr;
501 unsigned char *blobend;
502 struct ntlmssp2_name *attrptr;
503 struct timespec64 ts;
505 if (!ses->auth_key.len || !ses->auth_key.response)
508 blobptr = ses->auth_key.response;
509 blobend = blobptr + ses->auth_key.len;
511 while (blobptr + onesize < blobend) {
512 attrptr = (struct ntlmssp2_name *) blobptr;
513 type = le16_to_cpu(attrptr->type);
514 if (type == NTLMSSP_AV_EOL)
516 blobptr += 2; /* advance attr type */
517 attrsize = le16_to_cpu(attrptr->length);
518 blobptr += 2; /* advance attr size */
519 if (blobptr + attrsize > blobend)
521 if (type == NTLMSSP_AV_TIMESTAMP) {
522 if (attrsize == sizeof(u64))
523 return *((__le64 *)blobptr);
525 blobptr += attrsize; /* advance attr value */
528 ktime_get_real_ts64(&ts);
529 return cpu_to_le64(cifs_UnixTimeToNT(ts));
532 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
533 const struct nls_table *nls_cp)
537 char nt_hash[CIFS_NTHASH_SIZE];
542 if (!ses->server->secmech.hmacmd5) {
543 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
547 /* calculate md4 hash of password */
548 E_md4hash(ses->password, nt_hash, nls_cp);
550 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, nt_hash,
553 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
557 rc = crypto_shash_init(ses->server->secmech.hmacmd5);
559 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
563 /* convert ses->user_name to unicode */
564 len = ses->user_name ? strlen(ses->user_name) : 0;
565 user = kmalloc(2 + (len * 2), GFP_KERNEL);
572 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
575 memset(user, '\0', 2);
578 rc = crypto_shash_update(ses->server->secmech.hmacmd5,
579 (char *)user, 2 * len);
582 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
586 /* convert ses->domainName to unicode and uppercase */
587 if (ses->domainName) {
588 len = strlen(ses->domainName);
590 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
591 if (domain == NULL) {
595 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
598 crypto_shash_update(ses->server->secmech.hmacmd5,
599 (char *)domain, 2 * len);
602 cifs_dbg(VFS, "%s: Could not update with domain\n",
607 /* We use ses->ip_addr if no domain name available */
608 len = strlen(ses->ip_addr);
610 server = kmalloc(2 + (len * 2), GFP_KERNEL);
611 if (server == NULL) {
615 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len,
618 crypto_shash_update(ses->server->secmech.hmacmd5,
619 (char *)server, 2 * len);
622 cifs_dbg(VFS, "%s: Could not update with server\n",
628 rc = crypto_shash_final(ses->server->secmech.hmacmd5,
631 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
637 CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
640 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
641 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
642 unsigned int hash_len;
644 /* The MD5 hash starts at challenge_key.key */
645 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
646 offsetof(struct ntlmv2_resp, challenge.key[0]));
648 if (!ses->server->secmech.hmacmd5) {
649 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
653 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
654 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
656 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
661 rc = crypto_shash_init(ses->server->secmech.hmacmd5);
663 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
667 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
668 memcpy(ntlmv2->challenge.key,
669 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
671 memcpy(ntlmv2->challenge.key,
672 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
673 rc = crypto_shash_update(ses->server->secmech.hmacmd5,
674 ntlmv2->challenge.key, hash_len);
676 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
680 /* Note that the MD5 digest over writes anon.challenge_key.key */
681 rc = crypto_shash_final(ses->server->secmech.hmacmd5,
682 ntlmv2->ntlmv2_hash);
684 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
690 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
695 struct ntlmv2_resp *ntlmv2;
696 char ntlmv2_hash[16];
697 unsigned char *tiblob = NULL; /* target info blob */
698 __le64 rsp_timestamp;
700 if (nls_cp == NULL) {
701 cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__);
705 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
706 if (!ses->domainName) {
707 if (ses->domainAuto) {
708 rc = find_domain_name(ses, nls_cp);
710 cifs_dbg(VFS, "error %d finding domain name\n",
712 goto setup_ntlmv2_rsp_ret;
715 ses->domainName = kstrdup("", GFP_KERNEL);
719 rc = build_avpair_blob(ses, nls_cp);
721 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
722 goto setup_ntlmv2_rsp_ret;
726 /* Must be within 5 minutes of the server (or in range +/-2h
727 * in case of Mac OS X), so simply carry over server timestamp
728 * (as Windows 7 does)
730 rsp_timestamp = find_timestamp(ses);
732 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
733 tilen = ses->auth_key.len;
734 tiblob = ses->auth_key.response;
736 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
737 if (!ses->auth_key.response) {
739 ses->auth_key.len = 0;
740 goto setup_ntlmv2_rsp_ret;
742 ses->auth_key.len += baselen;
744 ntlmv2 = (struct ntlmv2_resp *)
745 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
746 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
747 ntlmv2->reserved = 0;
748 ntlmv2->time = rsp_timestamp;
750 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
751 ntlmv2->reserved2 = 0;
753 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
755 cifs_server_lock(ses->server);
757 rc = cifs_alloc_hash("hmac(md5)", &ses->server->secmech.hmacmd5);
762 /* calculate ntlmv2_hash */
763 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
765 cifs_dbg(VFS, "Could not get v2 hash rc %d\n", rc);
769 /* calculate first part of the client response (CR1) */
770 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
772 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
776 /* now calculate the session key for NTLMv2 */
777 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
778 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
780 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
785 rc = crypto_shash_init(ses->server->secmech.hmacmd5);
787 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
791 rc = crypto_shash_update(ses->server->secmech.hmacmd5,
793 CIFS_HMAC_MD5_HASH_SIZE);
795 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
799 rc = crypto_shash_final(ses->server->secmech.hmacmd5,
800 ses->auth_key.response);
802 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
805 cifs_server_unlock(ses->server);
806 setup_ntlmv2_rsp_ret:
807 kfree_sensitive(tiblob);
813 calc_seckey(struct cifs_ses *ses)
815 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
816 struct arc4_ctx *ctx_arc4;
821 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
823 ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL);
825 cifs_dbg(VFS, "Could not allocate arc4 context\n");
829 cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
830 cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
833 /* make secondary_key/nonce as session key */
834 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
835 /* and make len as that of session key only */
836 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
838 memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
839 kfree_sensitive(ctx_arc4);
844 cifs_crypto_secmech_release(struct TCP_Server_Info *server)
846 cifs_free_hash(&server->secmech.aes_cmac);
847 cifs_free_hash(&server->secmech.hmacsha256);
848 cifs_free_hash(&server->secmech.md5);
849 cifs_free_hash(&server->secmech.sha512);
850 cifs_free_hash(&server->secmech.hmacmd5);
852 if (server->secmech.enc) {
853 crypto_free_aead(server->secmech.enc);
854 server->secmech.enc = NULL;
857 if (server->secmech.dec) {
858 crypto_free_aead(server->secmech.dec);
859 server->secmech.dec = NULL;