1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/kernel.h>
9 #include <linux/uaccess.h>
10 #include <linux/backing-dev.h>
11 #include <linux/writeback.h>
12 #include <linux/uio.h>
13 #include <linux/xattr.h>
14 #include <crypto/hash.h>
15 #include <crypto/aead.h>
16 #include <linux/random.h>
17 #include <linux/scatterlist.h>
22 #include <linux/fips.h>
23 #include <crypto/des.h>
26 #include "smb_common.h"
27 #include "connection.h"
28 #include "mgmt/user_session.h"
29 #include "mgmt/user_config.h"
30 #include "crypto_ctx.h"
31 #include "transport_ipc.h"
34 * Fixed format data defining GSS header and fixed string
35 * "not_defined_in_RFC4178@please_ignore".
36 * So sec blob data in neg phase could be generated statically.
38 static char NEGOTIATE_GSS_HEADER[AUTH_GSS_LENGTH] = {
39 #ifdef CONFIG_SMB_SERVER_KERBEROS5
40 0x60, 0x5e, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x05,
41 0x05, 0x02, 0xa0, 0x54, 0x30, 0x52, 0xa0, 0x24,
42 0x30, 0x22, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
43 0xf7, 0x12, 0x01, 0x02, 0x02, 0x06, 0x09, 0x2a,
44 0x86, 0x48, 0x82, 0xf7, 0x12, 0x01, 0x02, 0x02,
45 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
46 0x37, 0x02, 0x02, 0x0a, 0xa3, 0x2a, 0x30, 0x28,
47 0xa0, 0x26, 0x1b, 0x24, 0x6e, 0x6f, 0x74, 0x5f,
48 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x64, 0x5f,
49 0x69, 0x6e, 0x5f, 0x52, 0x46, 0x43, 0x34, 0x31,
50 0x37, 0x38, 0x40, 0x70, 0x6c, 0x65, 0x61, 0x73,
51 0x65, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65
53 0x60, 0x48, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x05,
54 0x05, 0x02, 0xa0, 0x3e, 0x30, 0x3c, 0xa0, 0x0e,
55 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
56 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a, 0xa3, 0x2a,
57 0x30, 0x28, 0xa0, 0x26, 0x1b, 0x24, 0x6e, 0x6f,
58 0x74, 0x5f, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65,
59 0x64, 0x5f, 0x69, 0x6e, 0x5f, 0x52, 0x46, 0x43,
60 0x34, 0x31, 0x37, 0x38, 0x40, 0x70, 0x6c, 0x65,
61 0x61, 0x73, 0x65, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
66 void ksmbd_copy_gss_neg_header(void *buf)
68 memcpy(buf, NEGOTIATE_GSS_HEADER, AUTH_GSS_LENGTH);
72 * ksmbd_gen_sess_key() - function to generate session key
73 * @sess: session of connection
74 * @hash: source hash value to be used for find session key
75 * @hmac: source hmac value to be used for finding session key
78 static int ksmbd_gen_sess_key(struct ksmbd_session *sess, char *hash,
81 struct ksmbd_crypto_ctx *ctx;
84 ctx = ksmbd_crypto_ctx_find_hmacmd5();
86 ksmbd_debug(AUTH, "could not crypto alloc hmacmd5\n");
90 rc = crypto_shash_setkey(CRYPTO_HMACMD5_TFM(ctx),
92 CIFS_HMAC_MD5_HASH_SIZE);
94 ksmbd_debug(AUTH, "hmacmd5 set key fail error %d\n", rc);
98 rc = crypto_shash_init(CRYPTO_HMACMD5(ctx));
100 ksmbd_debug(AUTH, "could not init hmacmd5 error %d\n", rc);
104 rc = crypto_shash_update(CRYPTO_HMACMD5(ctx),
106 SMB2_NTLMV2_SESSKEY_SIZE);
108 ksmbd_debug(AUTH, "Could not update with response error %d\n", rc);
112 rc = crypto_shash_final(CRYPTO_HMACMD5(ctx), sess->sess_key);
114 ksmbd_debug(AUTH, "Could not generate hmacmd5 hash error %d\n", rc);
119 ksmbd_release_crypto_ctx(ctx);
123 static int calc_ntlmv2_hash(struct ksmbd_session *sess, char *ntlmv2_hash,
126 int ret, len, conv_len;
127 wchar_t *domain = NULL;
128 __le16 *uniname = NULL;
129 struct ksmbd_crypto_ctx *ctx;
131 ctx = ksmbd_crypto_ctx_find_hmacmd5();
133 ksmbd_debug(AUTH, "can't generate ntlmv2 hash\n");
137 ret = crypto_shash_setkey(CRYPTO_HMACMD5_TFM(ctx),
138 user_passkey(sess->user),
141 ksmbd_debug(AUTH, "Could not set NT Hash as a key\n");
145 ret = crypto_shash_init(CRYPTO_HMACMD5(ctx));
147 ksmbd_debug(AUTH, "could not init hmacmd5\n");
151 /* convert user_name to unicode */
152 len = strlen(user_name(sess->user));
153 uniname = kzalloc(2 + UNICODE_LEN(len), GFP_KERNEL);
159 conv_len = smb_strtoUTF16(uniname, user_name(sess->user), len,
160 sess->conn->local_nls);
161 if (conv_len < 0 || conv_len > len) {
167 ret = crypto_shash_update(CRYPTO_HMACMD5(ctx),
169 UNICODE_LEN(conv_len));
171 ksmbd_debug(AUTH, "Could not update with user\n");
175 /* Convert domain name or conn name to unicode and uppercase */
177 domain = kzalloc(2 + UNICODE_LEN(len), GFP_KERNEL);
183 conv_len = smb_strtoUTF16((__le16 *)domain, dname, len,
184 sess->conn->local_nls);
185 if (conv_len < 0 || conv_len > len) {
190 ret = crypto_shash_update(CRYPTO_HMACMD5(ctx),
192 UNICODE_LEN(conv_len));
194 ksmbd_debug(AUTH, "Could not update with domain\n");
198 ret = crypto_shash_final(CRYPTO_HMACMD5(ctx), ntlmv2_hash);
200 ksmbd_debug(AUTH, "Could not generate md5 hash\n");
204 ksmbd_release_crypto_ctx(ctx);
209 * ksmbd_auth_ntlmv2() - NTLMv2 authentication handler
210 * @sess: session of connection
211 * @ntlmv2: NTLMv2 challenge response
212 * @blen: NTLMv2 blob length
213 * @domain_name: domain name
215 * Return: 0 on success, error number on error
217 int ksmbd_auth_ntlmv2(struct ksmbd_session *sess, struct ntlmv2_resp *ntlmv2,
218 int blen, char *domain_name)
220 char ntlmv2_hash[CIFS_ENCPWD_SIZE];
221 char ntlmv2_rsp[CIFS_HMAC_MD5_HASH_SIZE];
222 struct ksmbd_crypto_ctx *ctx;
223 char *construct = NULL;
226 ctx = ksmbd_crypto_ctx_find_hmacmd5();
228 ksmbd_debug(AUTH, "could not crypto alloc hmacmd5\n");
232 rc = calc_ntlmv2_hash(sess, ntlmv2_hash, domain_name);
234 ksmbd_debug(AUTH, "could not get v2 hash rc %d\n", rc);
238 rc = crypto_shash_setkey(CRYPTO_HMACMD5_TFM(ctx),
240 CIFS_HMAC_MD5_HASH_SIZE);
242 ksmbd_debug(AUTH, "Could not set NTLMV2 Hash as a key\n");
246 rc = crypto_shash_init(CRYPTO_HMACMD5(ctx));
248 ksmbd_debug(AUTH, "Could not init hmacmd5\n");
252 len = CIFS_CRYPTO_KEY_SIZE + blen;
253 construct = kzalloc(len, GFP_KERNEL);
259 memcpy(construct, sess->ntlmssp.cryptkey, CIFS_CRYPTO_KEY_SIZE);
260 memcpy(construct + CIFS_CRYPTO_KEY_SIZE, &ntlmv2->blob_signature, blen);
262 rc = crypto_shash_update(CRYPTO_HMACMD5(ctx), construct, len);
264 ksmbd_debug(AUTH, "Could not update with response\n");
268 rc = crypto_shash_final(CRYPTO_HMACMD5(ctx), ntlmv2_rsp);
270 ksmbd_debug(AUTH, "Could not generate md5 hash\n");
274 rc = ksmbd_gen_sess_key(sess, ntlmv2_hash, ntlmv2_rsp);
276 ksmbd_debug(AUTH, "Could not generate sess key\n");
280 if (memcmp(ntlmv2->ntlmv2_hash, ntlmv2_rsp, CIFS_HMAC_MD5_HASH_SIZE) != 0)
283 ksmbd_release_crypto_ctx(ctx);
289 * ksmbd_decode_ntlmssp_auth_blob() - helper function to construct
291 * @authblob: authenticate blob source pointer
293 * @sess: session of connection
295 * Return: 0 on success, error number on error
297 int ksmbd_decode_ntlmssp_auth_blob(struct authenticate_message *authblob,
298 int blob_len, struct ksmbd_session *sess)
301 unsigned int nt_off, dn_off;
302 unsigned short nt_len, dn_len;
305 if (blob_len < sizeof(struct authenticate_message)) {
306 ksmbd_debug(AUTH, "negotiate blob len %d too small\n",
311 if (memcmp(authblob->Signature, "NTLMSSP", 8)) {
312 ksmbd_debug(AUTH, "blob signature incorrect %s\n",
313 authblob->Signature);
317 nt_off = le32_to_cpu(authblob->NtChallengeResponse.BufferOffset);
318 nt_len = le16_to_cpu(authblob->NtChallengeResponse.Length);
319 dn_off = le32_to_cpu(authblob->DomainName.BufferOffset);
320 dn_len = le16_to_cpu(authblob->DomainName.Length);
322 if (blob_len < (u64)dn_off + dn_len || blob_len < (u64)nt_off + nt_len)
325 /* TODO : use domain name that imported from configuration file */
326 domain_name = smb_strndup_from_utf16((const char *)authblob + dn_off,
327 dn_len, true, sess->conn->local_nls);
328 if (IS_ERR(domain_name))
329 return PTR_ERR(domain_name);
331 /* process NTLMv2 authentication */
332 ksmbd_debug(AUTH, "decode_ntlmssp_authenticate_blob dname%s\n",
334 ret = ksmbd_auth_ntlmv2(sess, (struct ntlmv2_resp *)((char *)authblob + nt_off),
335 nt_len - CIFS_ENCPWD_SIZE,
342 * ksmbd_decode_ntlmssp_neg_blob() - helper function to construct
344 * @negblob: negotiate blob source pointer
345 * @rsp: response header pointer to be updated
346 * @sess: session of connection
349 int ksmbd_decode_ntlmssp_neg_blob(struct negotiate_message *negblob,
350 int blob_len, struct ksmbd_session *sess)
352 if (blob_len < sizeof(struct negotiate_message)) {
353 ksmbd_debug(AUTH, "negotiate blob len %d too small\n",
358 if (memcmp(negblob->Signature, "NTLMSSP", 8)) {
359 ksmbd_debug(AUTH, "blob signature incorrect %s\n",
364 sess->ntlmssp.client_flags = le32_to_cpu(negblob->NegotiateFlags);
369 * ksmbd_build_ntlmssp_challenge_blob() - helper function to construct
371 * @chgblob: challenge blob source pointer to initialize
372 * @rsp: response header pointer to be updated
373 * @sess: session of connection
377 ksmbd_build_ntlmssp_challenge_blob(struct challenge_message *chgblob,
378 struct ksmbd_session *sess)
380 struct target_info *tinfo;
383 unsigned int flags, blob_off, blob_len, type, target_info_len = 0;
384 int len, uni_len, conv_len;
385 int cflags = sess->ntlmssp.client_flags;
387 memcpy(chgblob->Signature, NTLMSSP_SIGNATURE, 8);
388 chgblob->MessageType = NtLmChallenge;
390 flags = NTLMSSP_NEGOTIATE_UNICODE |
391 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_TARGET_TYPE_SERVER |
392 NTLMSSP_NEGOTIATE_TARGET_INFO;
394 if (cflags & NTLMSSP_NEGOTIATE_SIGN) {
395 flags |= NTLMSSP_NEGOTIATE_SIGN;
396 flags |= cflags & (NTLMSSP_NEGOTIATE_128 |
397 NTLMSSP_NEGOTIATE_56);
400 if (cflags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)
401 flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
403 if (cflags & NTLMSSP_REQUEST_TARGET)
404 flags |= NTLMSSP_REQUEST_TARGET;
406 if (sess->conn->use_spnego &&
407 (cflags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
408 flags |= NTLMSSP_NEGOTIATE_EXTENDED_SEC;
410 chgblob->NegotiateFlags = cpu_to_le32(flags);
411 len = strlen(ksmbd_netbios_name());
412 name = kmalloc(2 + UNICODE_LEN(len), GFP_KERNEL);
416 conv_len = smb_strtoUTF16((__le16 *)name, ksmbd_netbios_name(), len,
417 sess->conn->local_nls);
418 if (conv_len < 0 || conv_len > len) {
423 uni_len = UNICODE_LEN(conv_len);
425 blob_off = sizeof(struct challenge_message);
426 blob_len = blob_off + uni_len;
428 chgblob->TargetName.Length = cpu_to_le16(uni_len);
429 chgblob->TargetName.MaximumLength = cpu_to_le16(uni_len);
430 chgblob->TargetName.BufferOffset = cpu_to_le32(blob_off);
432 /* Initialize random conn challenge */
433 get_random_bytes(sess->ntlmssp.cryptkey, sizeof(__u64));
434 memcpy(chgblob->Challenge, sess->ntlmssp.cryptkey,
435 CIFS_CRYPTO_KEY_SIZE);
437 /* Add Target Information to security buffer */
438 chgblob->TargetInfoArray.BufferOffset = cpu_to_le32(blob_len);
440 target_name = (__u8 *)chgblob + blob_off;
441 memcpy(target_name, name, uni_len);
442 tinfo = (struct target_info *)(target_name + uni_len);
444 chgblob->TargetInfoArray.Length = 0;
445 /* Add target info list for NetBIOS/DNS settings */
446 for (type = NTLMSSP_AV_NB_COMPUTER_NAME;
447 type <= NTLMSSP_AV_DNS_DOMAIN_NAME; type++) {
448 tinfo->Type = cpu_to_le16(type);
449 tinfo->Length = cpu_to_le16(uni_len);
450 memcpy(tinfo->Content, name, uni_len);
451 tinfo = (struct target_info *)((char *)tinfo + 4 + uni_len);
452 target_info_len += 4 + uni_len;
455 /* Add terminator subblock */
458 target_info_len += 4;
460 chgblob->TargetInfoArray.Length = cpu_to_le16(target_info_len);
461 chgblob->TargetInfoArray.MaximumLength = cpu_to_le16(target_info_len);
462 blob_len += target_info_len;
464 ksmbd_debug(AUTH, "NTLMSSP SecurityBufferLength %d\n", blob_len);
468 #ifdef CONFIG_SMB_SERVER_KERBEROS5
469 int ksmbd_krb5_authenticate(struct ksmbd_session *sess, char *in_blob,
470 int in_len, char *out_blob, int *out_len)
472 struct ksmbd_spnego_authen_response *resp;
473 struct ksmbd_user *user = NULL;
476 resp = ksmbd_ipc_spnego_authen_request(in_blob, in_len);
478 ksmbd_debug(AUTH, "SPNEGO_AUTHEN_REQUEST failure\n");
482 if (!(resp->login_response.status & KSMBD_USER_FLAG_OK)) {
483 ksmbd_debug(AUTH, "krb5 authentication failure\n");
488 if (*out_len <= resp->spnego_blob_len) {
489 ksmbd_debug(AUTH, "buf len %d, but blob len %d\n",
490 *out_len, resp->spnego_blob_len);
495 if (resp->session_key_len > sizeof(sess->sess_key)) {
496 ksmbd_debug(AUTH, "session key is too long\n");
501 user = ksmbd_alloc_user(&resp->login_response);
503 ksmbd_debug(AUTH, "login failure\n");
509 memcpy(sess->sess_key, resp->payload, resp->session_key_len);
510 memcpy(out_blob, resp->payload + resp->session_key_len,
511 resp->spnego_blob_len);
512 *out_len = resp->spnego_blob_len;
519 int ksmbd_krb5_authenticate(struct ksmbd_session *sess, char *in_blob,
520 int in_len, char *out_blob, int *out_len)
527 * ksmbd_sign_smb2_pdu() - function to generate packet signing
530 * @iov: buffer iov array
531 * @n_vec: number of iovecs
532 * @sig: signature value generated for client request packet
535 int ksmbd_sign_smb2_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
536 int n_vec, char *sig)
538 struct ksmbd_crypto_ctx *ctx;
541 ctx = ksmbd_crypto_ctx_find_hmacsha256();
543 ksmbd_debug(AUTH, "could not crypto alloc hmacmd5\n");
547 rc = crypto_shash_setkey(CRYPTO_HMACSHA256_TFM(ctx),
549 SMB2_NTLMV2_SESSKEY_SIZE);
553 rc = crypto_shash_init(CRYPTO_HMACSHA256(ctx));
555 ksmbd_debug(AUTH, "hmacsha256 init error %d\n", rc);
559 for (i = 0; i < n_vec; i++) {
560 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx),
564 ksmbd_debug(AUTH, "hmacsha256 update error %d\n", rc);
569 rc = crypto_shash_final(CRYPTO_HMACSHA256(ctx), sig);
571 ksmbd_debug(AUTH, "hmacsha256 generation error %d\n", rc);
573 ksmbd_release_crypto_ctx(ctx);
578 * ksmbd_sign_smb3_pdu() - function to generate packet signing
581 * @iov: buffer iov array
582 * @n_vec: number of iovecs
583 * @sig: signature value generated for client request packet
586 int ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
587 int n_vec, char *sig)
589 struct ksmbd_crypto_ctx *ctx;
592 ctx = ksmbd_crypto_ctx_find_cmacaes();
594 ksmbd_debug(AUTH, "could not crypto alloc cmac\n");
598 rc = crypto_shash_setkey(CRYPTO_CMACAES_TFM(ctx),
604 rc = crypto_shash_init(CRYPTO_CMACAES(ctx));
606 ksmbd_debug(AUTH, "cmaces init error %d\n", rc);
610 for (i = 0; i < n_vec; i++) {
611 rc = crypto_shash_update(CRYPTO_CMACAES(ctx),
615 ksmbd_debug(AUTH, "cmaces update error %d\n", rc);
620 rc = crypto_shash_final(CRYPTO_CMACAES(ctx), sig);
622 ksmbd_debug(AUTH, "cmaces generation error %d\n", rc);
624 ksmbd_release_crypto_ctx(ctx);
634 static int generate_key(struct ksmbd_session *sess, struct kvec label,
635 struct kvec context, __u8 *key, unsigned int key_size)
637 unsigned char zero = 0x0;
638 __u8 i[4] = {0, 0, 0, 1};
639 __u8 L128[4] = {0, 0, 0, 128};
640 __u8 L256[4] = {0, 0, 1, 0};
642 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
643 unsigned char *hashptr = prfhash;
644 struct ksmbd_crypto_ctx *ctx;
646 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
647 memset(key, 0x0, key_size);
649 ctx = ksmbd_crypto_ctx_find_hmacsha256();
651 ksmbd_debug(AUTH, "could not crypto alloc hmacmd5\n");
655 rc = crypto_shash_setkey(CRYPTO_HMACSHA256_TFM(ctx),
657 SMB2_NTLMV2_SESSKEY_SIZE);
659 goto smb3signkey_ret;
661 rc = crypto_shash_init(CRYPTO_HMACSHA256(ctx));
663 ksmbd_debug(AUTH, "hmacsha256 init error %d\n", rc);
664 goto smb3signkey_ret;
667 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx), i, 4);
669 ksmbd_debug(AUTH, "could not update with n\n");
670 goto smb3signkey_ret;
673 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx),
677 ksmbd_debug(AUTH, "could not update with label\n");
678 goto smb3signkey_ret;
681 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx), &zero, 1);
683 ksmbd_debug(AUTH, "could not update with zero\n");
684 goto smb3signkey_ret;
687 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx),
691 ksmbd_debug(AUTH, "could not update with context\n");
692 goto smb3signkey_ret;
695 if (sess->conn->cipher_type == SMB2_ENCRYPTION_AES256_CCM ||
696 sess->conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
697 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx), L256, 4);
699 rc = crypto_shash_update(CRYPTO_HMACSHA256(ctx), L128, 4);
701 ksmbd_debug(AUTH, "could not update with L\n");
702 goto smb3signkey_ret;
705 rc = crypto_shash_final(CRYPTO_HMACSHA256(ctx), hashptr);
707 ksmbd_debug(AUTH, "Could not generate hmacmd5 hash error %d\n",
709 goto smb3signkey_ret;
712 memcpy(key, hashptr, key_size);
715 ksmbd_release_crypto_ctx(ctx);
719 static int generate_smb3signingkey(struct ksmbd_session *sess,
720 struct ksmbd_conn *conn,
721 const struct derivation *signing)
724 struct channel *chann;
727 chann = lookup_chann_list(sess, conn);
731 if (sess->conn->dialect >= SMB30_PROT_ID && signing->binding)
732 key = chann->smb3signingkey;
734 key = sess->smb3signingkey;
736 rc = generate_key(sess, signing->label, signing->context, key,
741 if (!(sess->conn->dialect >= SMB30_PROT_ID && signing->binding))
742 memcpy(chann->smb3signingkey, key, SMB3_SIGN_KEY_SIZE);
744 ksmbd_debug(AUTH, "dumping generated AES signing keys\n");
745 ksmbd_debug(AUTH, "Session Id %llu\n", sess->id);
746 ksmbd_debug(AUTH, "Session Key %*ph\n",
747 SMB2_NTLMV2_SESSKEY_SIZE, sess->sess_key);
748 ksmbd_debug(AUTH, "Signing Key %*ph\n",
749 SMB3_SIGN_KEY_SIZE, key);
753 int ksmbd_gen_smb30_signingkey(struct ksmbd_session *sess,
754 struct ksmbd_conn *conn)
758 d.label.iov_base = "SMB2AESCMAC";
759 d.label.iov_len = 12;
760 d.context.iov_base = "SmbSign";
761 d.context.iov_len = 8;
762 d.binding = conn->binding;
764 return generate_smb3signingkey(sess, conn, &d);
767 int ksmbd_gen_smb311_signingkey(struct ksmbd_session *sess,
768 struct ksmbd_conn *conn)
772 d.label.iov_base = "SMBSigningKey";
773 d.label.iov_len = 14;
775 struct preauth_session *preauth_sess;
777 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
780 d.context.iov_base = preauth_sess->Preauth_HashValue;
782 d.context.iov_base = sess->Preauth_HashValue;
784 d.context.iov_len = 64;
785 d.binding = conn->binding;
787 return generate_smb3signingkey(sess, conn, &d);
790 struct derivation_twin {
791 struct derivation encryption;
792 struct derivation decryption;
795 static int generate_smb3encryptionkey(struct ksmbd_session *sess,
796 const struct derivation_twin *ptwin)
800 rc = generate_key(sess, ptwin->encryption.label,
801 ptwin->encryption.context, sess->smb3encryptionkey,
802 SMB3_ENC_DEC_KEY_SIZE);
806 rc = generate_key(sess, ptwin->decryption.label,
807 ptwin->decryption.context,
808 sess->smb3decryptionkey, SMB3_ENC_DEC_KEY_SIZE);
812 ksmbd_debug(AUTH, "dumping generated AES encryption keys\n");
813 ksmbd_debug(AUTH, "Cipher type %d\n", sess->conn->cipher_type);
814 ksmbd_debug(AUTH, "Session Id %llu\n", sess->id);
815 ksmbd_debug(AUTH, "Session Key %*ph\n",
816 SMB2_NTLMV2_SESSKEY_SIZE, sess->sess_key);
817 if (sess->conn->cipher_type == SMB2_ENCRYPTION_AES256_CCM ||
818 sess->conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM) {
819 ksmbd_debug(AUTH, "ServerIn Key %*ph\n",
820 SMB3_GCM256_CRYPTKEY_SIZE, sess->smb3encryptionkey);
821 ksmbd_debug(AUTH, "ServerOut Key %*ph\n",
822 SMB3_GCM256_CRYPTKEY_SIZE, sess->smb3decryptionkey);
824 ksmbd_debug(AUTH, "ServerIn Key %*ph\n",
825 SMB3_GCM128_CRYPTKEY_SIZE, sess->smb3encryptionkey);
826 ksmbd_debug(AUTH, "ServerOut Key %*ph\n",
827 SMB3_GCM128_CRYPTKEY_SIZE, sess->smb3decryptionkey);
832 int ksmbd_gen_smb30_encryptionkey(struct ksmbd_session *sess)
834 struct derivation_twin twin;
835 struct derivation *d;
837 d = &twin.encryption;
838 d->label.iov_base = "SMB2AESCCM";
839 d->label.iov_len = 11;
840 d->context.iov_base = "ServerOut";
841 d->context.iov_len = 10;
843 d = &twin.decryption;
844 d->label.iov_base = "SMB2AESCCM";
845 d->label.iov_len = 11;
846 d->context.iov_base = "ServerIn ";
847 d->context.iov_len = 10;
849 return generate_smb3encryptionkey(sess, &twin);
852 int ksmbd_gen_smb311_encryptionkey(struct ksmbd_session *sess)
854 struct derivation_twin twin;
855 struct derivation *d;
857 d = &twin.encryption;
858 d->label.iov_base = "SMBS2CCipherKey";
859 d->label.iov_len = 16;
860 d->context.iov_base = sess->Preauth_HashValue;
861 d->context.iov_len = 64;
863 d = &twin.decryption;
864 d->label.iov_base = "SMBC2SCipherKey";
865 d->label.iov_len = 16;
866 d->context.iov_base = sess->Preauth_HashValue;
867 d->context.iov_len = 64;
869 return generate_smb3encryptionkey(sess, &twin);
872 int ksmbd_gen_preauth_integrity_hash(struct ksmbd_conn *conn, char *buf,
876 struct smb2_hdr *rcv_hdr = (struct smb2_hdr *)buf;
877 char *all_bytes_msg = (char *)&rcv_hdr->ProtocolId;
878 int msg_size = be32_to_cpu(rcv_hdr->smb2_buf_length);
879 struct ksmbd_crypto_ctx *ctx = NULL;
881 if (conn->preauth_info->Preauth_HashId !=
882 SMB2_PREAUTH_INTEGRITY_SHA512)
885 ctx = ksmbd_crypto_ctx_find_sha512();
887 ksmbd_debug(AUTH, "could not alloc sha512\n");
891 rc = crypto_shash_init(CRYPTO_SHA512(ctx));
893 ksmbd_debug(AUTH, "could not init shashn");
897 rc = crypto_shash_update(CRYPTO_SHA512(ctx), pi_hash, 64);
899 ksmbd_debug(AUTH, "could not update with n\n");
903 rc = crypto_shash_update(CRYPTO_SHA512(ctx), all_bytes_msg, msg_size);
905 ksmbd_debug(AUTH, "could not update with n\n");
909 rc = crypto_shash_final(CRYPTO_SHA512(ctx), pi_hash);
911 ksmbd_debug(AUTH, "Could not generate hash err : %d\n", rc);
915 ksmbd_release_crypto_ctx(ctx);
919 int ksmbd_gen_sd_hash(struct ksmbd_conn *conn, char *sd_buf, int len,
923 struct ksmbd_crypto_ctx *ctx = NULL;
925 ctx = ksmbd_crypto_ctx_find_sha256();
927 ksmbd_debug(AUTH, "could not alloc sha256\n");
931 rc = crypto_shash_init(CRYPTO_SHA256(ctx));
933 ksmbd_debug(AUTH, "could not init shashn");
937 rc = crypto_shash_update(CRYPTO_SHA256(ctx), sd_buf, len);
939 ksmbd_debug(AUTH, "could not update with n\n");
943 rc = crypto_shash_final(CRYPTO_SHA256(ctx), pi_hash);
945 ksmbd_debug(AUTH, "Could not generate hash err : %d\n", rc);
949 ksmbd_release_crypto_ctx(ctx);
953 static int ksmbd_get_encryption_key(struct ksmbd_conn *conn, __u64 ses_id,
956 struct ksmbd_session *sess;
959 sess = ksmbd_session_lookup_all(conn, ses_id);
963 ses_enc_key = enc ? sess->smb3encryptionkey :
964 sess->smb3decryptionkey;
965 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
970 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
975 if (is_vmalloc_addr(buf))
976 addr = vmalloc_to_page(buf);
978 addr = virt_to_page(buf);
979 sg_set_page(sg, addr, buflen, offset_in_page(buf));
982 static struct scatterlist *ksmbd_init_sg(struct kvec *iov, unsigned int nvec,
985 struct scatterlist *sg;
986 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
987 int i, nr_entries[3] = {0}, total_entries = 0, sg_idx = 0;
992 for (i = 0; i < nvec - 1; i++) {
993 unsigned long kaddr = (unsigned long)iov[i + 1].iov_base;
995 if (is_vmalloc_addr(iov[i + 1].iov_base)) {
996 nr_entries[i] = ((kaddr + iov[i + 1].iov_len +
997 PAGE_SIZE - 1) >> PAGE_SHIFT) -
998 (kaddr >> PAGE_SHIFT);
1002 total_entries += nr_entries[i];
1005 /* Add two entries for transform header and signature */
1008 sg = kmalloc_array(total_entries, sizeof(struct scatterlist), GFP_KERNEL);
1012 sg_init_table(sg, total_entries);
1013 smb2_sg_set_buf(&sg[sg_idx++], iov[0].iov_base + 24, assoc_data_len);
1014 for (i = 0; i < nvec - 1; i++) {
1015 void *data = iov[i + 1].iov_base;
1016 int len = iov[i + 1].iov_len;
1018 if (is_vmalloc_addr(data)) {
1019 int j, offset = offset_in_page(data);
1021 for (j = 0; j < nr_entries[i]; j++) {
1022 unsigned int bytes = PAGE_SIZE - offset;
1030 sg_set_page(&sg[sg_idx++],
1031 vmalloc_to_page(data), bytes,
1032 offset_in_page(data));
1039 sg_set_page(&sg[sg_idx++], virt_to_page(data), len,
1040 offset_in_page(data));
1043 smb2_sg_set_buf(&sg[sg_idx], sign, SMB2_SIGNATURE_SIZE);
1047 int ksmbd_crypt_message(struct ksmbd_conn *conn, struct kvec *iov,
1048 unsigned int nvec, int enc)
1050 struct smb2_transform_hdr *tr_hdr =
1051 (struct smb2_transform_hdr *)iov[0].iov_base;
1052 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1054 struct scatterlist *sg;
1055 u8 sign[SMB2_SIGNATURE_SIZE] = {};
1056 u8 key[SMB3_ENC_DEC_KEY_SIZE];
1057 struct aead_request *req;
1059 unsigned int iv_len;
1060 struct crypto_aead *tfm;
1061 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
1062 struct ksmbd_crypto_ctx *ctx;
1064 rc = ksmbd_get_encryption_key(conn,
1065 le64_to_cpu(tr_hdr->SessionId),
1069 pr_err("Could not get %scryption key\n", enc ? "en" : "de");
1073 if (conn->cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
1074 conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
1075 ctx = ksmbd_crypto_ctx_find_gcm();
1077 ctx = ksmbd_crypto_ctx_find_ccm();
1079 pr_err("crypto alloc failed\n");
1083 if (conn->cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
1084 conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
1085 tfm = CRYPTO_GCM(ctx);
1087 tfm = CRYPTO_CCM(ctx);
1089 if (conn->cipher_type == SMB2_ENCRYPTION_AES256_CCM ||
1090 conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
1091 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
1093 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
1095 pr_err("Failed to set aead key %d\n", rc);
1099 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
1101 pr_err("Failed to set authsize %d\n", rc);
1105 req = aead_request_alloc(tfm, GFP_KERNEL);
1112 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
1113 crypt_len += SMB2_SIGNATURE_SIZE;
1116 sg = ksmbd_init_sg(iov, nvec, sign);
1118 pr_err("Failed to init sg\n");
1123 iv_len = crypto_aead_ivsize(tfm);
1124 iv = kzalloc(iv_len, GFP_KERNEL);
1130 if (conn->cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
1131 conn->cipher_type == SMB2_ENCRYPTION_AES256_GCM) {
1132 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
1135 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
1138 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
1139 aead_request_set_ad(req, assoc_data_len);
1140 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
1143 rc = crypto_aead_encrypt(req);
1145 rc = crypto_aead_decrypt(req);
1150 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
1159 ksmbd_release_crypto_ctx(ctx);