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
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 "../smbfs_common/arc4.h"
25 #include <crypto/aead.h>
27 int __cifs_calc_signature(struct smb_rqst *rqst,
28 struct TCP_Server_Info *server, char *signature,
29 struct shash_desc *shash)
33 struct kvec *iov = rqst->rq_iov;
34 int n_vec = rqst->rq_nvec;
35 int is_smb2 = server->vals->header_preamble_size == 0;
37 /* iov[0] is actual data and not the rfc1002 length for SMB2+ */
39 if (iov[0].iov_len <= 4)
43 if (n_vec < 2 || iov[0].iov_len != 4)
45 i = 1; /* skip rfc1002 length */
48 for (; i < n_vec; i++) {
49 if (iov[i].iov_len == 0)
51 if (iov[i].iov_base == NULL) {
52 cifs_dbg(VFS, "null iovec entry\n");
56 rc = crypto_shash_update(shash,
57 iov[i].iov_base, iov[i].iov_len);
59 cifs_dbg(VFS, "%s: Could not update with payload\n",
65 /* now hash over the rq_pages array */
66 for (i = 0; i < rqst->rq_npages; i++) {
68 unsigned int len, offset;
70 rqst_page_get_length(rqst, i, &len, &offset);
72 kaddr = (char *) kmap(rqst->rq_pages[i]) + offset;
74 rc = crypto_shash_update(shash, kaddr, len);
76 cifs_dbg(VFS, "%s: Could not update with payload\n",
78 kunmap(rqst->rq_pages[i]);
82 kunmap(rqst->rq_pages[i]);
85 rc = crypto_shash_final(shash, signature);
87 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
93 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
94 * The 16 byte signature must be allocated by the caller. Note we only use the
95 * 1st eight bytes and that the smb header signature field on input contains
96 * the sequence number before this function is called. Also, this function
97 * should be called with the server->srv_mutex held.
99 static int cifs_calc_signature(struct smb_rqst *rqst,
100 struct TCP_Server_Info *server, char *signature)
104 if (!rqst->rq_iov || !signature || !server)
107 rc = cifs_alloc_hash("md5", &server->secmech.md5,
108 &server->secmech.sdescmd5);
112 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
114 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
118 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
119 server->session_key.response, server->session_key.len);
121 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
125 return __cifs_calc_signature(rqst, server, signature,
126 &server->secmech.sdescmd5->shash);
129 /* must be called with server->srv_mutex held */
130 int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
131 __u32 *pexpected_response_sequence_number)
134 char smb_signature[20];
135 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
137 if (rqst->rq_iov[0].iov_len != 4 ||
138 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
141 if ((cifs_pdu == NULL) || (server == NULL))
144 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
145 server->tcpStatus == CifsNeedNegotiate)
148 if (!server->session_estab) {
149 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
153 cifs_pdu->Signature.Sequence.SequenceNumber =
154 cpu_to_le32(server->sequence_number);
155 cifs_pdu->Signature.Sequence.Reserved = 0;
157 *pexpected_response_sequence_number = ++server->sequence_number;
158 ++server->sequence_number;
160 rc = cifs_calc_signature(rqst, server, smb_signature);
162 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
164 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
169 int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
170 __u32 *pexpected_response_sequence)
172 struct smb_rqst rqst = { .rq_iov = iov,
175 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
178 /* must be called with server->srv_mutex held */
179 int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
180 __u32 *pexpected_response_sequence_number)
184 iov[0].iov_base = cifs_pdu;
186 iov[1].iov_base = (char *)cifs_pdu + 4;
187 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
189 return cifs_sign_smbv(iov, 2, server,
190 pexpected_response_sequence_number);
193 int cifs_verify_signature(struct smb_rqst *rqst,
194 struct TCP_Server_Info *server,
195 __u32 expected_sequence_number)
198 char server_response_sig[8];
199 char what_we_think_sig_should_be[20];
200 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
202 if (rqst->rq_iov[0].iov_len != 4 ||
203 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
206 if (cifs_pdu == NULL || server == NULL)
209 if (!server->session_estab)
212 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
213 struct smb_com_lock_req *pSMB =
214 (struct smb_com_lock_req *)cifs_pdu;
215 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
219 /* BB what if signatures are supposed to be on for session but
220 server does not send one? BB */
222 /* Do not need to verify session setups with signature "BSRSPYL " */
223 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
224 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
227 /* save off the origiginal signature so we can modify the smb and check
228 its signature against what the server sent */
229 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
231 cifs_pdu->Signature.Sequence.SequenceNumber =
232 cpu_to_le32(expected_sequence_number);
233 cifs_pdu->Signature.Sequence.Reserved = 0;
235 mutex_lock(&server->srv_mutex);
236 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
237 mutex_unlock(&server->srv_mutex);
242 /* cifs_dump_mem("what we think it should be: ",
243 what_we_think_sig_should_be, 16); */
245 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
252 /* Build a proper attribute value/target info pairs blob.
253 * Fill in netbios and dns domain name and workstation name
254 * and client time (total five av pairs and + one end of fields indicator.
255 * Allocate domain name which gets freed when session struct is deallocated.
258 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
261 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
262 char *defdmname = "WORKGROUP";
263 unsigned char *blobptr;
264 struct ntlmssp2_name *attrptr;
266 if (!ses->domainName) {
267 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
268 if (!ses->domainName)
272 dlen = strlen(ses->domainName);
275 * The length of this blob is two times the size of a
276 * structure (av pair) which holds name/size
277 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
278 * unicode length of a netbios domain name
280 ses->auth_key.len = size + 2 * dlen;
281 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
282 if (!ses->auth_key.response) {
283 ses->auth_key.len = 0;
287 blobptr = ses->auth_key.response;
288 attrptr = (struct ntlmssp2_name *) blobptr;
291 * As defined in MS-NTLM 3.3.2, just this av pair field
292 * is sufficient as part of the temp
294 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
295 attrptr->length = cpu_to_le16(2 * dlen);
296 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
297 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
302 /* Server has provided av pairs/target info in the type 2 challenge
303 * packet and we have plucked it and stored within smb session.
304 * We parse that blob here to find netbios domain name to be used
305 * as part of ntlmv2 authentication (in Target String), if not already
306 * specified on the command line.
307 * If this function returns without any error but without fetching
308 * domain name, authentication may fail against some server but
309 * may not fail against other (those who are not very particular
310 * about target string i.e. for some, just user name might suffice.
313 find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
315 unsigned int attrsize;
317 unsigned int onesize = sizeof(struct ntlmssp2_name);
318 unsigned char *blobptr;
319 unsigned char *blobend;
320 struct ntlmssp2_name *attrptr;
322 if (!ses->auth_key.len || !ses->auth_key.response)
325 blobptr = ses->auth_key.response;
326 blobend = blobptr + ses->auth_key.len;
328 while (blobptr + onesize < blobend) {
329 attrptr = (struct ntlmssp2_name *) blobptr;
330 type = le16_to_cpu(attrptr->type);
331 if (type == NTLMSSP_AV_EOL)
333 blobptr += 2; /* advance attr type */
334 attrsize = le16_to_cpu(attrptr->length);
335 blobptr += 2; /* advance attr size */
336 if (blobptr + attrsize > blobend)
338 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
339 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
341 if (!ses->domainName) {
343 kmalloc(attrsize + 1, GFP_KERNEL);
344 if (!ses->domainName)
346 cifs_from_utf16(ses->domainName,
347 (__le16 *)blobptr, attrsize, attrsize,
348 nls_cp, NO_MAP_UNI_RSVD);
352 blobptr += attrsize; /* advance attr value */
358 /* Server has provided av pairs/target info in the type 2 challenge
359 * packet and we have plucked it and stored within smb session.
360 * We parse that blob here to find the server given timestamp
361 * as part of ntlmv2 authentication (or local current time as
362 * default in case of failure)
365 find_timestamp(struct cifs_ses *ses)
367 unsigned int attrsize;
369 unsigned int onesize = sizeof(struct ntlmssp2_name);
370 unsigned char *blobptr;
371 unsigned char *blobend;
372 struct ntlmssp2_name *attrptr;
373 struct timespec64 ts;
375 if (!ses->auth_key.len || !ses->auth_key.response)
378 blobptr = ses->auth_key.response;
379 blobend = blobptr + ses->auth_key.len;
381 while (blobptr + onesize < blobend) {
382 attrptr = (struct ntlmssp2_name *) blobptr;
383 type = le16_to_cpu(attrptr->type);
384 if (type == NTLMSSP_AV_EOL)
386 blobptr += 2; /* advance attr type */
387 attrsize = le16_to_cpu(attrptr->length);
388 blobptr += 2; /* advance attr size */
389 if (blobptr + attrsize > blobend)
391 if (type == NTLMSSP_AV_TIMESTAMP) {
392 if (attrsize == sizeof(u64))
393 return *((__le64 *)blobptr);
395 blobptr += attrsize; /* advance attr value */
398 ktime_get_real_ts64(&ts);
399 return cpu_to_le64(cifs_UnixTimeToNT(ts));
402 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
403 const struct nls_table *nls_cp)
407 char nt_hash[CIFS_NTHASH_SIZE];
412 if (!ses->server->secmech.sdeschmacmd5) {
413 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
417 /* calculate md4 hash of password */
418 E_md4hash(ses->password, nt_hash, nls_cp);
420 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
423 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
427 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
429 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
433 /* convert ses->user_name to unicode */
434 len = ses->user_name ? strlen(ses->user_name) : 0;
435 user = kmalloc(2 + (len * 2), GFP_KERNEL);
442 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
445 memset(user, '\0', 2);
448 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
449 (char *)user, 2 * len);
452 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
456 /* convert ses->domainName to unicode and uppercase */
457 if (ses->domainName) {
458 len = strlen(ses->domainName);
460 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
461 if (domain == NULL) {
465 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
468 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
469 (char *)domain, 2 * len);
472 cifs_dbg(VFS, "%s: Could not update with domain\n",
477 /* We use ses->ip_addr if no domain name available */
478 len = strlen(ses->ip_addr);
480 server = kmalloc(2 + (len * 2), GFP_KERNEL);
481 if (server == NULL) {
485 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len,
488 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
489 (char *)server, 2 * len);
492 cifs_dbg(VFS, "%s: Could not update with server\n",
498 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
501 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
507 CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
510 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
511 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
512 unsigned int hash_len;
514 /* The MD5 hash starts at challenge_key.key */
515 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
516 offsetof(struct ntlmv2_resp, challenge.key[0]));
518 if (!ses->server->secmech.sdeschmacmd5) {
519 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
523 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
524 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
526 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
531 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
533 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
537 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
538 memcpy(ntlmv2->challenge.key,
539 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
541 memcpy(ntlmv2->challenge.key,
542 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
543 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
544 ntlmv2->challenge.key, hash_len);
546 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
550 /* Note that the MD5 digest over writes anon.challenge_key.key */
551 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
552 ntlmv2->ntlmv2_hash);
554 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
560 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
565 struct ntlmv2_resp *ntlmv2;
566 char ntlmv2_hash[16];
567 unsigned char *tiblob = NULL; /* target info blob */
568 __le64 rsp_timestamp;
570 if (nls_cp == NULL) {
571 cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__);
575 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
576 if (!ses->domainName) {
577 if (ses->domainAuto) {
578 rc = find_domain_name(ses, nls_cp);
580 cifs_dbg(VFS, "error %d finding domain name\n",
582 goto setup_ntlmv2_rsp_ret;
585 ses->domainName = kstrdup("", GFP_KERNEL);
589 rc = build_avpair_blob(ses, nls_cp);
591 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
592 goto setup_ntlmv2_rsp_ret;
596 /* Must be within 5 minutes of the server (or in range +/-2h
597 * in case of Mac OS X), so simply carry over server timestamp
598 * (as Windows 7 does)
600 rsp_timestamp = find_timestamp(ses);
602 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
603 tilen = ses->auth_key.len;
604 tiblob = ses->auth_key.response;
606 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
607 if (!ses->auth_key.response) {
609 ses->auth_key.len = 0;
610 goto setup_ntlmv2_rsp_ret;
612 ses->auth_key.len += baselen;
614 ntlmv2 = (struct ntlmv2_resp *)
615 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
616 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
617 ntlmv2->reserved = 0;
618 ntlmv2->time = rsp_timestamp;
620 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
621 ntlmv2->reserved2 = 0;
623 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
625 mutex_lock(&ses->server->srv_mutex);
627 rc = cifs_alloc_hash("hmac(md5)",
628 &ses->server->secmech.hmacmd5,
629 &ses->server->secmech.sdeschmacmd5);
634 /* calculate ntlmv2_hash */
635 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
637 cifs_dbg(VFS, "Could not get v2 hash rc %d\n", rc);
641 /* calculate first part of the client response (CR1) */
642 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
644 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
648 /* now calculate the session key for NTLMv2 */
649 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
650 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
652 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
657 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
659 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
663 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
665 CIFS_HMAC_MD5_HASH_SIZE);
667 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
671 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
672 ses->auth_key.response);
674 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
677 mutex_unlock(&ses->server->srv_mutex);
678 setup_ntlmv2_rsp_ret:
685 calc_seckey(struct cifs_ses *ses)
687 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
688 struct arc4_ctx *ctx_arc4;
693 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
695 ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL);
697 cifs_dbg(VFS, "Could not allocate arc4 context\n");
701 cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
702 cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
705 /* make secondary_key/nonce as session key */
706 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
707 /* and make len as that of session key only */
708 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
710 memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
711 kfree_sensitive(ctx_arc4);
716 cifs_crypto_secmech_release(struct TCP_Server_Info *server)
718 if (server->secmech.cmacaes) {
719 crypto_free_shash(server->secmech.cmacaes);
720 server->secmech.cmacaes = NULL;
723 if (server->secmech.hmacsha256) {
724 crypto_free_shash(server->secmech.hmacsha256);
725 server->secmech.hmacsha256 = NULL;
728 if (server->secmech.md5) {
729 crypto_free_shash(server->secmech.md5);
730 server->secmech.md5 = NULL;
733 if (server->secmech.sha512) {
734 crypto_free_shash(server->secmech.sha512);
735 server->secmech.sha512 = NULL;
738 if (server->secmech.hmacmd5) {
739 crypto_free_shash(server->secmech.hmacmd5);
740 server->secmech.hmacmd5 = NULL;
743 if (server->secmech.ccmaesencrypt) {
744 crypto_free_aead(server->secmech.ccmaesencrypt);
745 server->secmech.ccmaesencrypt = NULL;
748 if (server->secmech.ccmaesdecrypt) {
749 crypto_free_aead(server->secmech.ccmaesdecrypt);
750 server->secmech.ccmaesdecrypt = NULL;
753 kfree(server->secmech.sdesccmacaes);
754 server->secmech.sdesccmacaes = NULL;
755 kfree(server->secmech.sdeschmacsha256);
756 server->secmech.sdeschmacsha256 = NULL;
757 kfree(server->secmech.sdeschmacmd5);
758 server->secmech.sdeschmacmd5 = NULL;
759 kfree(server->secmech.sdescmd5);
760 server->secmech.sdescmd5 = NULL;
761 kfree(server->secmech.sdescsha512);
762 server->secmech.sdescsha512 = NULL;