xref: /linux/fs/smb/client/cifsencrypt.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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  */
11 
12 #include <linux/fs.h>
13 #include <linux/slab.h>
14 #include "cifsglob.h"
15 #include "cifs_debug.h"
16 #include "cifs_unicode.h"
17 #include "cifsproto.h"
18 #include "ntlmssp.h"
19 #include <linux/ctype.h>
20 #include <linux/random.h>
21 #include <linux/highmem.h>
22 #include <linux/fips.h>
23 #include <linux/iov_iter.h>
24 #include <crypto/aead.h>
25 #include <crypto/aes-cbc-macs.h>
26 #include <crypto/arc4.h>
27 #include <crypto/md5.h>
28 #include <crypto/sha2.h>
29 
30 static size_t cifs_sig_step(void *iter_base, size_t progress, size_t len,
31 			    void *priv, void *priv2)
32 {
33 	struct cifs_calc_sig_ctx *ctx = priv;
34 
35 	if (ctx->md5)
36 		md5_update(ctx->md5, iter_base, len);
37 	else if (ctx->hmac)
38 		hmac_sha256_update(ctx->hmac, iter_base, len);
39 	else
40 		aes_cmac_update(ctx->cmac, iter_base, len);
41 	return 0; /* Return value is length *not* processed, i.e. 0. */
42 }
43 
44 static void cifs_sig_final(struct cifs_calc_sig_ctx *ctx, u8 *out)
45 {
46 	if (ctx->md5)
47 		md5_final(ctx->md5, out);
48 	else if (ctx->hmac)
49 		hmac_sha256_final(ctx->hmac, out);
50 	else
51 		aes_cmac_final(ctx->cmac, out);
52 }
53 
54 /*
55  * Pass the data from an iterator into a hash.
56  */
57 static int cifs_sig_iter(const struct iov_iter *iter, size_t maxsize,
58 			 struct cifs_calc_sig_ctx *ctx)
59 {
60 	struct iov_iter tmp_iter = *iter;
61 	size_t did;
62 
63 	did = iterate_and_advance_kernel(&tmp_iter, maxsize, ctx, NULL,
64 					 cifs_sig_step);
65 	if (did != maxsize)
66 		return smb_EIO2(smb_eio_trace_sig_iter, did, maxsize);
67 	return 0;
68 }
69 
70 int __cifs_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
71 			  char *signature, struct cifs_calc_sig_ctx *ctx)
72 {
73 	struct iov_iter iter;
74 	ssize_t rc;
75 	size_t size = 0;
76 
77 	for (int i = 0; i < rqst->rq_nvec; i++)
78 		size += rqst->rq_iov[i].iov_len;
79 
80 	iov_iter_kvec(&iter, ITER_SOURCE, rqst->rq_iov, rqst->rq_nvec, size);
81 
82 	if (iov_iter_count(&iter) <= 4)
83 		return smb_EIO2(smb_eio_trace_sig_data_too_small,
84 				iov_iter_count(&iter), 4);
85 
86 	rc = cifs_sig_iter(&iter, iov_iter_count(&iter), ctx);
87 	if (rc < 0)
88 		return rc;
89 
90 	rc = cifs_sig_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), ctx);
91 	if (rc < 0)
92 		return rc;
93 
94 	cifs_sig_final(ctx, signature);
95 	return 0;
96 }
97 
98 /* Build a proper attribute value/target info pairs blob.
99  * Fill in netbios and dns domain name and workstation name
100  * and client time (total five av pairs and + one end of fields indicator.
101  * Allocate domain name which gets freed when session struct is deallocated.
102  */
103 static int build_avpair_blob(struct cifs_ses *ses)
104 {
105 	unsigned int dlen;
106 	unsigned int size = 2 * sizeof(struct ntlmssp2_name);
107 	char *defdmname = "WORKGROUP";
108 	unsigned char *blobptr;
109 	struct ntlmssp2_name *attrptr;
110 
111 	if (!ses->domainName) {
112 		ses->domainName = kstrdup(defdmname, GFP_KERNEL);
113 		if (!ses->domainName)
114 			return -ENOMEM;
115 	}
116 
117 	dlen = strlen(ses->domainName);
118 
119 	/*
120 	 * The length of this blob is two times the size of a
121 	 * structure (av pair) which holds name/size
122 	 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
123 	 * unicode length of a netbios domain name
124 	 */
125 	kfree_sensitive(ses->auth_key.response);
126 	ses->auth_key.len = size + 2 * dlen;
127 	ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
128 	if (!ses->auth_key.response) {
129 		ses->auth_key.len = 0;
130 		return -ENOMEM;
131 	}
132 
133 	blobptr = ses->auth_key.response;
134 	attrptr = (struct ntlmssp2_name *) blobptr;
135 
136 	/*
137 	 * As defined in MS-NTLM 3.3.2, just this av pair field
138 	 * is sufficient as part of the temp
139 	 */
140 	attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
141 	attrptr->length = cpu_to_le16(2 * dlen);
142 	blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
143 	cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen,
144 			ses->local_nls);
145 
146 	return 0;
147 }
148 
149 #define AV_TYPE(av)		(le16_to_cpu(av->type))
150 #define AV_LEN(av)		(le16_to_cpu(av->length))
151 #define AV_DATA_PTR(av)	((void *)av->data)
152 
153 #define av_for_each_entry(ses, av) \
154 	for (av = NULL; (av = find_next_av(ses, av));)
155 
156 static struct ntlmssp2_name *find_next_av(struct cifs_ses *ses,
157 					  struct ntlmssp2_name *av)
158 {
159 	u16 len;
160 	u8 *end;
161 
162 	end = (u8 *)ses->auth_key.response + ses->auth_key.len;
163 	if (!av) {
164 		if (unlikely(!ses->auth_key.response || !ses->auth_key.len))
165 			return NULL;
166 		av = (void *)ses->auth_key.response;
167 	} else {
168 		av = (void *)((u8 *)av + sizeof(*av) + AV_LEN(av));
169 	}
170 
171 	if ((u8 *)av + sizeof(*av) > end)
172 		return NULL;
173 
174 	len = AV_LEN(av);
175 	if (AV_TYPE(av) == NTLMSSP_AV_EOL)
176 		return NULL;
177 	if ((u8 *)av + sizeof(*av) + len > end)
178 		return NULL;
179 	return av;
180 }
181 
182 /*
183  * Check if server has provided av pair of @type in the NTLMSSP
184  * CHALLENGE_MESSAGE blob.
185  */
186 static int find_av_name(struct cifs_ses *ses, u16 type, char **name, u16 maxlen)
187 {
188 	const struct nls_table *nlsc = ses->local_nls;
189 	struct ntlmssp2_name *av;
190 	u16 len, nlen;
191 
192 	if (*name)
193 		return 0;
194 
195 	av_for_each_entry(ses, av) {
196 		len = AV_LEN(av);
197 		if (AV_TYPE(av) != type || !len)
198 			continue;
199 		if (!IS_ALIGNED(len, sizeof(__le16))) {
200 			cifs_dbg(VFS | ONCE, "%s: bad length(%u) for type %u\n",
201 				 __func__, len, type);
202 			continue;
203 		}
204 		nlen = len / sizeof(__le16);
205 		if (nlen <= maxlen) {
206 			++nlen;
207 			*name = kmalloc(nlen, GFP_KERNEL);
208 			if (!*name)
209 				return -ENOMEM;
210 			cifs_from_utf16(*name, AV_DATA_PTR(av), nlen,
211 					len, nlsc, NO_MAP_UNI_RSVD);
212 			break;
213 		}
214 	}
215 	return 0;
216 }
217 
218 /* Server has provided av pairs/target info in the type 2 challenge
219  * packet and we have plucked it and stored within smb session.
220  * We parse that blob here to find the server given timestamp
221  * as part of ntlmv2 authentication (or local current time as
222  * default in case of failure)
223  */
224 static __le64 find_timestamp(struct cifs_ses *ses)
225 {
226 	struct ntlmssp2_name *av;
227 	struct timespec64 ts;
228 
229 	av_for_each_entry(ses, av) {
230 		if (AV_TYPE(av) == NTLMSSP_AV_TIMESTAMP &&
231 		    AV_LEN(av) == sizeof(u64))
232 			return *((__le64 *)AV_DATA_PTR(av));
233 	}
234 	ktime_get_real_ts64(&ts);
235 	return cpu_to_le64(cifs_UnixTimeToNT(ts));
236 }
237 
238 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash)
239 {
240 	int len;
241 	char nt_hash[CIFS_NTHASH_SIZE];
242 	struct hmac_md5_ctx hmac_ctx;
243 	__le16 *user;
244 	wchar_t *domain;
245 	wchar_t *server;
246 
247 	/* calculate md4 hash of password */
248 	E_md4hash(ses->password, nt_hash, ses->local_nls);
249 
250 	hmac_md5_init_usingrawkey(&hmac_ctx, nt_hash, CIFS_NTHASH_SIZE);
251 	memzero_explicit(nt_hash, sizeof(nt_hash));
252 
253 	/* convert ses->user_name to unicode */
254 	len = ses->user_name ? strlen(ses->user_name) : 0;
255 	user = kmalloc(2 + (len * 2), GFP_KERNEL);
256 	if (user == NULL)
257 		goto out_nomem;
258 
259 	if (len) {
260 		len = cifs_strtoUTF16(user, ses->user_name, len,
261 				      ses->local_nls);
262 		UniStrupr(user);
263 	} else {
264 		*(u16 *)user = 0;
265 	}
266 
267 	hmac_md5_update(&hmac_ctx, (const u8 *)user, 2 * len);
268 	kfree(user);
269 
270 	/* convert ses->domainName to unicode and uppercase */
271 	if (ses->domainName) {
272 		len = strlen(ses->domainName);
273 
274 		domain = kmalloc(2 + (len * 2), GFP_KERNEL);
275 		if (domain == NULL)
276 			goto out_nomem;
277 
278 		len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
279 				      ses->local_nls);
280 		hmac_md5_update(&hmac_ctx, (const u8 *)domain, 2 * len);
281 		kfree(domain);
282 	} else {
283 		/* We use ses->ip_addr if no domain name available */
284 		len = strlen(ses->ip_addr);
285 
286 		server = kmalloc(2 + (len * 2), GFP_KERNEL);
287 		if (server == NULL)
288 			goto out_nomem;
289 
290 		len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len,
291 				      ses->local_nls);
292 		hmac_md5_update(&hmac_ctx, (const u8 *)server, 2 * len);
293 		kfree(server);
294 	}
295 
296 	hmac_md5_final(&hmac_ctx, ntlmv2_hash);
297 	return 0;
298 
299 out_nomem:
300 	memzero_explicit(&hmac_ctx, sizeof(hmac_ctx));
301 	return -ENOMEM;
302 }
303 
304 static void CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
305 {
306 	struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
307 	    (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
308 	unsigned int hash_len;
309 
310 	/* The MD5 hash starts at challenge_key.key */
311 	hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
312 		offsetof(struct ntlmv2_resp, challenge.key[0]));
313 
314 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
315 		memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
316 	else
317 		memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
318 
319 	/* Note that the HMAC-MD5 value overwrites ntlmv2->challenge.key */
320 	hmac_md5_usingrawkey(ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE,
321 			     ntlmv2->challenge.key, hash_len,
322 			     ntlmv2->ntlmv2_hash);
323 }
324 
325 /*
326  * Set up NTLMv2 response blob with SPN (cifs/<hostname>) appended to the
327  * existing list of AV pairs.
328  */
329 static int set_auth_key_response(struct cifs_ses *ses)
330 {
331 	size_t baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
332 	size_t len, spnlen, tilen = 0, num_avs = 2 /* SPN + EOL */;
333 	struct TCP_Server_Info *server = ses->server;
334 	char *spn __free(kfree) = NULL;
335 	struct ntlmssp2_name *av;
336 	char *rsp = NULL;
337 	int rc;
338 
339 	spnlen = strlen(server->hostname);
340 	len = sizeof("cifs/") + spnlen;
341 	spn = kmalloc(len, GFP_KERNEL);
342 	if (!spn) {
343 		rc = -ENOMEM;
344 		goto out;
345 	}
346 
347 	spnlen = scnprintf(spn, len, "cifs/%.*s",
348 			   (int)spnlen, server->hostname);
349 
350 	av_for_each_entry(ses, av)
351 		tilen += sizeof(*av) + AV_LEN(av);
352 
353 	len = baselen + tilen + spnlen * sizeof(__le16) + num_avs * sizeof(*av);
354 	rsp = kmalloc(len, GFP_KERNEL);
355 	if (!rsp) {
356 		rc = -ENOMEM;
357 		goto out;
358 	}
359 
360 	memcpy(rsp + baselen, ses->auth_key.response, tilen);
361 	av = (void *)(rsp + baselen + tilen);
362 	av->type = cpu_to_le16(NTLMSSP_AV_TARGET_NAME);
363 	av->length = cpu_to_le16(spnlen * sizeof(__le16));
364 	cifs_strtoUTF16((__le16 *)av->data, spn, spnlen, ses->local_nls);
365 	av = (void *)((__u8 *)av + sizeof(*av) + AV_LEN(av));
366 	av->type = cpu_to_le16(NTLMSSP_AV_EOL);
367 	av->length = 0;
368 
369 	rc = 0;
370 	ses->auth_key.len = len;
371 out:
372 	ses->auth_key.response = rsp;
373 	return rc;
374 }
375 
376 int setup_ntlmv2_rsp(struct cifs_ses *ses)
377 {
378 	unsigned char *tiblob = NULL; /* target info blob */
379 	struct ntlmv2_resp *ntlmv2;
380 	char ntlmv2_hash[16];
381 	__le64 rsp_timestamp;
382 	__u64 cc;
383 	int rc;
384 
385 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
386 		if (!ses->domainName) {
387 			if (ses->domainAuto) {
388 				/*
389 				 * Domain (workgroup) hasn't been specified in
390 				 * mount options, so try to find it in
391 				 * CHALLENGE_MESSAGE message and then use it as
392 				 * part of NTLMv2 authentication.
393 				 */
394 				rc = find_av_name(ses, NTLMSSP_AV_NB_DOMAIN_NAME,
395 						  &ses->domainName,
396 						  CIFS_MAX_DOMAINNAME_LEN);
397 				if (rc)
398 					goto setup_ntlmv2_rsp_ret;
399 			} else {
400 				ses->domainName = kstrdup("", GFP_KERNEL);
401 				if (!ses->domainName) {
402 					rc = -ENOMEM;
403 					goto setup_ntlmv2_rsp_ret;
404 				}
405 			}
406 		}
407 		rc = find_av_name(ses, NTLMSSP_AV_DNS_DOMAIN_NAME,
408 				  &ses->dns_dom, CIFS_MAX_DOMAINNAME_LEN);
409 		if (rc)
410 			goto setup_ntlmv2_rsp_ret;
411 	} else {
412 		rc = build_avpair_blob(ses);
413 		if (rc) {
414 			cifs_dbg(VFS, "error %d building av pair blob\n", rc);
415 			goto setup_ntlmv2_rsp_ret;
416 		}
417 	}
418 
419 	/* Must be within 5 minutes of the server (or in range +/-2h
420 	 * in case of Mac OS X), so simply carry over server timestamp
421 	 * (as Windows 7 does)
422 	 */
423 	rsp_timestamp = find_timestamp(ses);
424 	get_random_bytes(&cc, sizeof(cc));
425 
426 	cifs_server_lock(ses->server);
427 
428 	tiblob = ses->auth_key.response;
429 	rc = set_auth_key_response(ses);
430 	if (rc) {
431 		ses->auth_key.len = 0;
432 		goto unlock;
433 	}
434 
435 	ntlmv2 = (struct ntlmv2_resp *)
436 			(ses->auth_key.response + CIFS_SESS_KEY_SIZE);
437 	ntlmv2->blob_signature = cpu_to_le32(0x00000101);
438 	ntlmv2->reserved = 0;
439 	ntlmv2->time = rsp_timestamp;
440 	ntlmv2->client_chal = cc;
441 	ntlmv2->reserved2 = 0;
442 
443 	if (fips_enabled) {
444 		cifs_dbg(VFS, "NTLMv2 support is disabled due to FIPS\n");
445 		rc = -EOPNOTSUPP;
446 		goto unlock;
447 	}
448 
449 	/* calculate ntlmv2_hash */
450 	rc = calc_ntlmv2_hash(ses, ntlmv2_hash);
451 	if (rc) {
452 		cifs_dbg(VFS, "Could not get NTLMv2 hash, rc=%d\n", rc);
453 		goto unlock;
454 	}
455 
456 	/* calculate first part of the client response (CR1) */
457 	CalcNTLMv2_response(ses, ntlmv2_hash);
458 
459 	/* now calculate the session key for NTLMv2 */
460 	hmac_md5_usingrawkey(ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE,
461 			     ntlmv2->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE,
462 			     ses->auth_key.response);
463 	rc = 0;
464 unlock:
465 	cifs_server_unlock(ses->server);
466 	memzero_explicit(ntlmv2_hash, sizeof(ntlmv2_hash));
467 setup_ntlmv2_rsp_ret:
468 	kfree_sensitive(tiblob);
469 
470 	return rc;
471 }
472 
473 int
474 calc_seckey(struct cifs_ses *ses)
475 {
476 	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
477 	struct arc4_ctx *ctx_arc4;
478 
479 	if (fips_enabled)
480 		return -ENODEV;
481 
482 	get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
483 
484 	ctx_arc4 = kmalloc_obj(*ctx_arc4);
485 	if (!ctx_arc4) {
486 		cifs_dbg(VFS, "Could not allocate arc4 context\n");
487 		return -ENOMEM;
488 	}
489 
490 	arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
491 	arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
492 		   CIFS_CPHTXT_SIZE);
493 
494 	/* make secondary_key/nonce as session key */
495 	memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
496 	/* and make len as that of session key only */
497 	ses->auth_key.len = CIFS_SESS_KEY_SIZE;
498 
499 	memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
500 	kfree_sensitive(ctx_arc4);
501 	return 0;
502 }
503 
504 void
505 cifs_crypto_secmech_release(struct TCP_Server_Info *server)
506 {
507 	if (server->secmech.enc) {
508 		crypto_free_aead(server->secmech.enc);
509 		server->secmech.enc = NULL;
510 	}
511 	if (server->secmech.dec) {
512 		crypto_free_aead(server->secmech.dec);
513 		server->secmech.dec = NULL;
514 	}
515 }
516