xref: /linux/fs/smb/client/cifsencrypt.c (revision 39f1c201b93f4ff71631bac72cff6eb155f976a4)
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
104 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
105 {
106 	unsigned int dlen;
107 	unsigned int size = 2 * sizeof(struct ntlmssp2_name);
108 	char *defdmname = "WORKGROUP";
109 	unsigned char *blobptr;
110 	struct ntlmssp2_name *attrptr;
111 
112 	if (!ses->domainName) {
113 		ses->domainName = kstrdup(defdmname, GFP_KERNEL);
114 		if (!ses->domainName)
115 			return -ENOMEM;
116 	}
117 
118 	dlen = strlen(ses->domainName);
119 
120 	/*
121 	 * The length of this blob is two times the size of a
122 	 * structure (av pair) which holds name/size
123 	 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
124 	 * unicode length of a netbios domain name
125 	 */
126 	kfree_sensitive(ses->auth_key.response);
127 	ses->auth_key.len = size + 2 * dlen;
128 	ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
129 	if (!ses->auth_key.response) {
130 		ses->auth_key.len = 0;
131 		return -ENOMEM;
132 	}
133 
134 	blobptr = ses->auth_key.response;
135 	attrptr = (struct ntlmssp2_name *) blobptr;
136 
137 	/*
138 	 * As defined in MS-NTLM 3.3.2, just this av pair field
139 	 * is sufficient as part of the temp
140 	 */
141 	attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
142 	attrptr->length = cpu_to_le16(2 * dlen);
143 	blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
144 	cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
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 			    const struct nls_table *nls_cp)
240 {
241 	int len;
242 	char nt_hash[CIFS_NTHASH_SIZE];
243 	struct hmac_md5_ctx hmac_ctx;
244 	__le16 *user;
245 	wchar_t *domain;
246 	wchar_t *server;
247 
248 	/* calculate md4 hash of password */
249 	E_md4hash(ses->password, nt_hash, nls_cp);
250 
251 	hmac_md5_init_usingrawkey(&hmac_ctx, nt_hash, CIFS_NTHASH_SIZE);
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 		return -ENOMEM;
258 
259 	if (len) {
260 		len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
261 		UniStrupr(user);
262 	} else {
263 		*(u16 *)user = 0;
264 	}
265 
266 	hmac_md5_update(&hmac_ctx, (const u8 *)user, 2 * len);
267 	kfree(user);
268 
269 	/* convert ses->domainName to unicode and uppercase */
270 	if (ses->domainName) {
271 		len = strlen(ses->domainName);
272 
273 		domain = kmalloc(2 + (len * 2), GFP_KERNEL);
274 		if (domain == NULL)
275 			return -ENOMEM;
276 
277 		len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
278 				      nls_cp);
279 		hmac_md5_update(&hmac_ctx, (const u8 *)domain, 2 * len);
280 		kfree(domain);
281 	} else {
282 		/* We use ses->ip_addr if no domain name available */
283 		len = strlen(ses->ip_addr);
284 
285 		server = kmalloc(2 + (len * 2), GFP_KERNEL);
286 		if (server == NULL)
287 			return -ENOMEM;
288 
289 		len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp);
290 		hmac_md5_update(&hmac_ctx, (const u8 *)server, 2 * len);
291 		kfree(server);
292 	}
293 
294 	hmac_md5_final(&hmac_ctx, ntlmv2_hash);
295 	return 0;
296 }
297 
298 static void CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
299 {
300 	struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
301 	    (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
302 	unsigned int hash_len;
303 
304 	/* The MD5 hash starts at challenge_key.key */
305 	hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
306 		offsetof(struct ntlmv2_resp, challenge.key[0]));
307 
308 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
309 		memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
310 	else
311 		memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
312 
313 	/* Note that the HMAC-MD5 value overwrites ntlmv2->challenge.key */
314 	hmac_md5_usingrawkey(ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE,
315 			     ntlmv2->challenge.key, hash_len,
316 			     ntlmv2->ntlmv2_hash);
317 }
318 
319 /*
320  * Set up NTLMv2 response blob with SPN (cifs/<hostname>) appended to the
321  * existing list of AV pairs.
322  */
323 static int set_auth_key_response(struct cifs_ses *ses)
324 {
325 	size_t baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
326 	size_t len, spnlen, tilen = 0, num_avs = 2 /* SPN + EOL */;
327 	struct TCP_Server_Info *server = ses->server;
328 	char *spn __free(kfree) = NULL;
329 	struct ntlmssp2_name *av;
330 	char *rsp = NULL;
331 	int rc;
332 
333 	spnlen = strlen(server->hostname);
334 	len = sizeof("cifs/") + spnlen;
335 	spn = kmalloc(len, GFP_KERNEL);
336 	if (!spn) {
337 		rc = -ENOMEM;
338 		goto out;
339 	}
340 
341 	spnlen = scnprintf(spn, len, "cifs/%.*s",
342 			   (int)spnlen, server->hostname);
343 
344 	av_for_each_entry(ses, av)
345 		tilen += sizeof(*av) + AV_LEN(av);
346 
347 	len = baselen + tilen + spnlen * sizeof(__le16) + num_avs * sizeof(*av);
348 	rsp = kmalloc(len, GFP_KERNEL);
349 	if (!rsp) {
350 		rc = -ENOMEM;
351 		goto out;
352 	}
353 
354 	memcpy(rsp + baselen, ses->auth_key.response, tilen);
355 	av = (void *)(rsp + baselen + tilen);
356 	av->type = cpu_to_le16(NTLMSSP_AV_TARGET_NAME);
357 	av->length = cpu_to_le16(spnlen * sizeof(__le16));
358 	cifs_strtoUTF16((__le16 *)av->data, spn, spnlen, ses->local_nls);
359 	av = (void *)((__u8 *)av + sizeof(*av) + AV_LEN(av));
360 	av->type = cpu_to_le16(NTLMSSP_AV_EOL);
361 	av->length = 0;
362 
363 	rc = 0;
364 	ses->auth_key.len = len;
365 out:
366 	ses->auth_key.response = rsp;
367 	return rc;
368 }
369 
370 int
371 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
372 {
373 	unsigned char *tiblob = NULL; /* target info blob */
374 	struct ntlmv2_resp *ntlmv2;
375 	char ntlmv2_hash[16];
376 	__le64 rsp_timestamp;
377 	__u64 cc;
378 	int rc;
379 
380 	if (nls_cp == NULL) {
381 		cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__);
382 		return -EINVAL;
383 	}
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, nls_cp);
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, nls_cp);
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 setup_ntlmv2_rsp_ret:
467 	kfree_sensitive(tiblob);
468 
469 	return rc;
470 }
471 
472 int
473 calc_seckey(struct cifs_ses *ses)
474 {
475 	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
476 	struct arc4_ctx *ctx_arc4;
477 
478 	if (fips_enabled)
479 		return -ENODEV;
480 
481 	get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
482 
483 	ctx_arc4 = kmalloc_obj(*ctx_arc4);
484 	if (!ctx_arc4) {
485 		cifs_dbg(VFS, "Could not allocate arc4 context\n");
486 		return -ENOMEM;
487 	}
488 
489 	arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
490 	arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
491 		   CIFS_CPHTXT_SIZE);
492 
493 	/* make secondary_key/nonce as session key */
494 	memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
495 	/* and make len as that of session key only */
496 	ses->auth_key.len = CIFS_SESS_KEY_SIZE;
497 
498 	memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
499 	kfree_sensitive(ctx_arc4);
500 	return 0;
501 }
502 
503 void
504 cifs_crypto_secmech_release(struct TCP_Server_Info *server)
505 {
506 	if (server->secmech.enc) {
507 		crypto_free_aead(server->secmech.enc);
508 		server->secmech.enc = NULL;
509 	}
510 	if (server->secmech.dec) {
511 		crypto_free_aead(server->secmech.dec);
512 		server->secmech.dec = NULL;
513 	}
514 }
515