^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Unix SMB/Netbios implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Version 1.9.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) SMB parameters and setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) Copyright (C) Andrew Tridgell 1992-2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Copyright (C) Luke Kenneth Casson Leighton 1996-2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) Modified by Jeremy Allison 1995.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) Modified by Steve French (sfrench@us.ibm.com) 2002-2003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <crypto/des.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "cifs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "cifs_unicode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "cifspdu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "cifsglob.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "cifs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "cifsproto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifndef false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define false 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifndef true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define true 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* following came from the other byteorder.h to avoid include conflicts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) str_to_key(unsigned char *str, unsigned char *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) key[0] = str[0] >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) key[7] = str[6] & 0x7F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) key[i] = (key[i] << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned char key2[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct des_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) str_to_key(key, key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (fips_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) des_expand_key(&ctx, key2, DES_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) des_encrypt(&ctx, out, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) memzero_explicit(&ctx, sizeof(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) E_P16(unsigned char *p14, unsigned char *p16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned char sp8[8] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) rc = smbhash(p16, sp8, p14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) rc = smbhash(p16 + 8, sp8, p14 + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) E_P24(unsigned char *p21, const unsigned char *c8, unsigned char *p24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rc = smbhash(p24, c8, p21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) rc = smbhash(p24 + 8, c8, p21 + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rc = smbhash(p24 + 16, c8, p21 + 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* produce a md4 message digest from data of length n bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct crypto_shash *md4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct sdesc *sdescmd4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rc = cifs_alloc_hash("md4", &md4, &sdescmd4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto mdfour_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rc = crypto_shash_init(&sdescmd4->shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto mdfour_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto mdfour_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mdfour_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) cifs_free_hash(&md4, &sdescmd4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) This implements the X/Open SMB password encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) It takes a password, a 8 byte "crypt key" and puts 24 bytes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) encrypted password into p24 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Note that password must be uppercased and null terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned char p14[14], p16[16], p21[21];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) memset(p14, '\0', 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) memset(p16, '\0', 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) memset(p21, '\0', 21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) memcpy(p14, passwd, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) rc = E_P16(p14, p16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) memcpy(p21, p16, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rc = E_P24(p21, c8, p24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Creates the MD4 Hash of the users password in NT UNICODE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) E_md4hash(const unsigned char *passwd, unsigned char *p16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const struct nls_table *codepage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) __le16 wpwd[129];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Password cannot be longer than 128 characters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (passwd) /* Password must be converted to NT unicode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *wpwd = 0; /* Ensure string is null terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) memzero_explicit(wpwd, sizeof(wpwd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Does the NT MD4 hash then des encryption. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const struct nls_table *codepage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned char p16[16], p21[21];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) memset(p16, '\0', 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) memset(p21, '\0', 21);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) rc = E_md4hash(passwd, p16, codepage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) memcpy(p21, p16, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) rc = E_P24(p21, c8, p24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }